diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..8e2076a --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -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 }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..56263cf --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/internal/api/admin/server.go b/internal/api/admin/server.go index 448e9ea..8ec4584 100644 --- a/internal/api/admin/server.go +++ b/internal/api/admin/server.go @@ -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 { @@ -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) diff --git a/internal/api/public/server.go b/internal/api/public/server.go index 4dfc380..e50c3f4 100644 --- a/internal/api/public/server.go +++ b/internal/api/public/server.go @@ -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 { @@ -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) diff --git a/internal/app.go b/internal/app.go index 87cf040..d104ac5 100644 --- a/internal/app.go +++ b/internal/app.go @@ -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" @@ -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 @@ -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...") } diff --git a/internal/pkg/models/config/config.go b/internal/pkg/models/config/config.go index 1590dd8..401844b 100644 --- a/internal/pkg/models/config/config.go +++ b/internal/pkg/models/config/config.go @@ -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"` } @@ -20,6 +30,5 @@ type Mailchimp struct { } type Mandrill struct { - User string `mapstructure:"user"` ApiKey string `mapstructure:"api_key"` }