Skip to content

Commit

Permalink
Added Dockerfile and GitHub workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
gapidobri committed Aug 12, 2024
1 parent d7d039a commit 6693255
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 11 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Create and publish a Docker image

on:
push:
branches: ['master']

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.22 AS build

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -tags=viper_bind_struct -o prizer


FROM alpine:latest

COPY --from=build /app/prizer /prizer

CMD ["/prizer"]
4 changes: 3 additions & 1 deletion internal/api/admin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gapidobri/prizer/internal/api"
"github.com/gapidobri/prizer/internal/service"
"github.com/gin-gonic/gin"
"log"
log "github.com/sirupsen/logrus"
)

type Server struct {
Expand Down Expand Up @@ -42,6 +42,8 @@ func (s *Server) Run(address string) {
s.wonPrizeRoutes()
s.participationMethodRoutes()

log.Infof("Admin API listening on %s", address)

err := s.engine.Run(address)
if err != nil {
log.Panic(err)
Expand Down
4 changes: 3 additions & 1 deletion internal/api/public/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gapidobri/prizer/internal/api"
"github.com/gapidobri/prizer/internal/service"
"github.com/gin-gonic/gin"
"log"
log "github.com/sirupsen/logrus"
)

type Server struct {
Expand All @@ -24,6 +24,8 @@ func (s *Server) Run(address string) {

s.participationMethodRoutes()

log.Infof("Public API listening on %s", address)

err := s.engine.Run(address)
if err != nil {
log.Panic(err)
Expand Down
16 changes: 8 additions & 8 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/mattbaird/gochimp"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"log"
"os"
"os/signal"
"syscall"
Expand All @@ -23,23 +23,23 @@ func Run() {

var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatalf("failed to decode config, %v", err)
log.WithError(err).Fatal("Failed to decode config")
}

db, err := sqlx.Connect("postgres", cfg.Database.ConnectionString)
if err != nil {
log.Fatalf("failed to connect to database, %v", err)
log.WithError(err).Fatal("Failed to connect to database")
}

// Clients
addressValidationClient, err := addressvalidation.NewClient(ctx, cfg.AddressValidation.ApiKey)
if err != nil {
log.Fatal(err)
log.WithError(err).Fatal("Failed to create address validation client")
}

mandrillClient, err := gochimp.NewMandrill(cfg.Mandrill.ApiKey)
if err != nil {
log.Fatalf("failed to create mandrill client, %v", err)
log.WithError(err).Fatal("Failed to create mandrill client")
}

// Repositories
Expand Down Expand Up @@ -80,12 +80,12 @@ func Run() {
participationMethodService,
)

go publicApi.Run(":8080")
go adminApi.Run(":8081")
go publicApi.Run(cfg.Http.Public.Address)
go adminApi.Run(cfg.Http.Admin.Address)

sign := make(chan os.Signal, 1)
signal.Notify(sign, syscall.SIGINT, syscall.SIGTERM)
<-sign

log.Println("Shutting down...")
log.Info("Shutting down...")
}
11 changes: 10 additions & 1 deletion internal/pkg/models/config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package config

type Config struct {
Http Http `mapstructure:"http"`
Database Database `mapstructure:"database"`
AddressValidation AddressValidation `mapstructure:"address_validation"`
Mailchimp Mailchimp `mapstructure:"mailchimp"`
Mandrill Mandrill `mapstructure:"mandrill"`
}

type Http struct {
Public Api `mapstructure:"public"`
Admin Api `mapstructure:"admin"`
}

type Api struct {
Address string `mapstructure:"address"`
}

type Database struct {
ConnectionString string `mapstructure:"connection_string"`
}
Expand All @@ -20,6 +30,5 @@ type Mailchimp struct {
}

type Mandrill struct {
User string `mapstructure:"user"`
ApiKey string `mapstructure:"api_key"`
}

0 comments on commit 6693255

Please sign in to comment.