From 24223136797098fa29a7f692349df24e5562b81d Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Thu, 22 Aug 2024 20:33:04 +0700 Subject: [PATCH 01/10] feat: Set up singular binary that can run different services --- cmd/app/main.go | 61 +++++++++++++++++++ Dockerfile => configs/api.Dockerfile | 2 +- configs/data.Dockerfile | 8 +++ configs/delivery.Dockerfile | 8 +++ docker-compose.yml | 35 +++++++++-- go.mod | 1 + go.sum | 2 + .../main.go => internal/services/api/api.go | 16 ++--- internal/{router => services/api}/router.go | 4 +- internal/services/data/data.go | 31 ++++++++++ internal/services/delivery/delivery.go | 11 ++++ 11 files changed, 159 insertions(+), 20 deletions(-) create mode 100644 cmd/app/main.go rename Dockerfile => configs/api.Dockerfile (51%) create mode 100644 configs/data.Dockerfile create mode 100644 configs/delivery.Dockerfile rename cmd/api/main.go => internal/services/api/api.go (78%) rename internal/{router => services/api}/router.go (92%) create mode 100644 internal/services/data/data.go create mode 100644 internal/services/delivery/delivery.go diff --git a/cmd/app/main.go b/cmd/app/main.go new file mode 100644 index 00000000..51de80d2 --- /dev/null +++ b/cmd/app/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io" + "os" + + "github.com/hookdeck/EventKit/internal/services/api" + "github.com/hookdeck/EventKit/internal/services/data" + "github.com/hookdeck/EventKit/internal/services/delivery" + "golang.org/x/sync/errgroup" +) + +type Service interface { + Run(ctx context.Context) error +} + +func run(ctx context.Context, w io.Writer, args []string) error { + serviceName := "" + if len(args) > 1 { + serviceName = args[1] + } + + if serviceName == "" { + // Run all services. + // TODO: Investigate how goroutine affect graceful shutdown, fatal, restart, etc. + // @see https://github.com/gin-gonic/gin/issues/346 + + var g errgroup.Group + g.Go(func() error { + return api.Run(ctx) + }) + g.Go(func() error { + return delivery.Run(ctx) + }) + g.Go(func() error { + return data.Run(ctx) + }) + + err := g.Wait() + return err + } else if serviceName == "api" { + return api.Run(ctx) + } else if serviceName == "delivery" { + return delivery.Run(ctx) + } else if serviceName == "data" { + return data.Run(ctx) + } else { + return errors.New(fmt.Sprintf("unknown service: %s", serviceName)) + } +} + +func main() { + ctx := context.Background() + if err := run(ctx, os.Stdout, os.Args); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } +} diff --git a/Dockerfile b/configs/api.Dockerfile similarity index 51% rename from Dockerfile rename to configs/api.Dockerfile index c365231b..a5a9ea43 100644 --- a/Dockerfile +++ b/configs/api.Dockerfile @@ -5,4 +5,4 @@ COPY . . RUN go install -mod=mod github.com/githubnemo/CompileDaemon -ENTRYPOINT CompileDaemon --build="go build -o ./bin/api/main ./cmd/api/main.go" --command=./bin/api/main \ No newline at end of file +ENTRYPOINT CompileDaemon --build="go build -o ./bin/api ./cmd/app/main.go" --command="./bin/api api" \ No newline at end of file diff --git a/configs/data.Dockerfile b/configs/data.Dockerfile new file mode 100644 index 00000000..3fae2c60 --- /dev/null +++ b/configs/data.Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.23-alpine + +WORKDIR /app +COPY . . + +RUN go install -mod=mod github.com/githubnemo/CompileDaemon + +ENTRYPOINT CompileDaemon --build="go build -o ./bin/data ./cmd/app/main.go" --command="./bin/data data" \ No newline at end of file diff --git a/configs/delivery.Dockerfile b/configs/delivery.Dockerfile new file mode 100644 index 00000000..d163cce3 --- /dev/null +++ b/configs/delivery.Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.23-alpine + +WORKDIR /app +COPY . . + +RUN go install -mod=mod github.com/githubnemo/CompileDaemon + +ENTRYPOINT CompileDaemon --build="go build -o ./bin/delivery ./cmd/app/main.go" --command="./bin/delivery delivery" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 44a7bc99..0d5d695a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,19 +2,44 @@ name: "eventkit" services: api: - build: . + build: + context: . + dockerfile: configs/api.Dockerfile volumes: - .:/app depends_on: - redis ports: - "${PORT}:${PORT}" + env_file: .env environment: - PORT: ${PORT} REDIS_HOST: redis - REDIS_PORT: ${REDIS_PORT} - REDIS_PASSWORD: ${REDIS_PASSWORD} - REDIS_DATABASE: ${REDIS_DATABASE} + + delivery: + build: + context: . + dockerfile: configs/delivery.Dockerfile + volumes: + - .:/app + depends_on: + - redis + env_file: .env + environment: + REDIS_HOST: redis + DISABLED: true # Temporary env variable to disable the service + + data: + build: + context: . + dockerfile: configs/data.Dockerfile + volumes: + - .:/app + depends_on: + - redis + env_file: .env + environment: + REDIS_HOST: redis + DISABLED: true # Temporary env variable to disable the service redis: image: redis:7.4-alpine diff --git a/go.mod b/go.mod index 0574db8f..c6fbcc8a 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gin-gonic/gin v1.10.0 github.com/google/uuid v1.6.0 github.com/redis/go-redis/v9 v9.6.1 + golang.org/x/sync v0.8.0 ) require ( diff --git a/go.sum b/go.sum index 4a88d9e2..b14e4ed6 100644 --- a/go.sum +++ b/go.sum @@ -82,6 +82,8 @@ golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= diff --git a/cmd/api/main.go b/internal/services/api/api.go similarity index 78% rename from cmd/api/main.go rename to internal/services/api/api.go index 2dd76a1f..deae5e13 100644 --- a/cmd/api/main.go +++ b/internal/services/api/api.go @@ -1,4 +1,4 @@ -package main +package api import ( "context" @@ -11,14 +11,14 @@ import ( "time" "github.com/hookdeck/EventKit/internal/config" - "github.com/hookdeck/EventKit/internal/router" ) -func run(ctx context.Context) error { +func Run(ctx context.Context) error { + log.Println("running api service") ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) defer cancel() - r := router.New() + r := NewRouter() httpServer := &http.Server{ Addr: fmt.Sprintf(":%d", config.Port), Handler: r, @@ -46,11 +46,3 @@ func run(ctx context.Context) error { return nil } - -func main() { - ctx := context.Background() - if err := run(ctx); err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) - } -} diff --git a/internal/router/router.go b/internal/services/api/router.go similarity index 92% rename from internal/router/router.go rename to internal/services/api/router.go index 2a6f5922..77391b02 100644 --- a/internal/router/router.go +++ b/internal/services/api/router.go @@ -1,4 +1,4 @@ -package router +package api import ( "net/http" @@ -7,7 +7,7 @@ import ( "github.com/hookdeck/EventKit/internal/destination" ) -func New() http.Handler { +func NewRouter() http.Handler { r := gin.Default() r.GET("/healthz", func(c *gin.Context) { diff --git a/internal/services/data/data.go b/internal/services/data/data.go new file mode 100644 index 00000000..ad2a68f0 --- /dev/null +++ b/internal/services/data/data.go @@ -0,0 +1,31 @@ +package data + +import ( + "context" + "fmt" + "log" + "os" + "time" + + "github.com/hookdeck/EventKit/internal/redis" +) + +func Run(ctx context.Context) error { + log.Println("running data service") + + if os.Getenv("DISABLED") == "true" { + log.Println("data service is disabled") + return nil + } + + for range time.Tick(time.Second * 1) { + keys, err := redis.Client().Keys(ctx, "destination:*").Result() + if err != nil { + log.Println(err) + continue + } + log.Println(fmt.Sprintf("%d destination(s)", len(keys))) + } + + return nil +} diff --git a/internal/services/delivery/delivery.go b/internal/services/delivery/delivery.go new file mode 100644 index 00000000..a12675a0 --- /dev/null +++ b/internal/services/delivery/delivery.go @@ -0,0 +1,11 @@ +package delivery + +import ( + "context" + "log" +) + +func Run(ctx context.Context) error { + log.Println("running delivery service") + return nil +} From de3ce9f8712441c6f480b0ab0d8a7a9a262f7c7d Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 01:36:51 +0700 Subject: [PATCH 02/10] feat: Accept flags for service name and config file --- cmd/app/main.go | 36 ++++++++++++++----------- go.mod | 16 ++++++++++++ go.sum | 55 ++++++++++++++++++++++++++++++++++----- internal/config/config.go | 45 +++++++++++++++++++++----------- internal/flag/flag.go | 26 ++++++++++++++++++ 5 files changed, 141 insertions(+), 37 deletions(-) create mode 100644 internal/flag/flag.go diff --git a/cmd/app/main.go b/cmd/app/main.go index 51de80d2..86e80d8d 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -4,9 +4,10 @@ import ( "context" "errors" "fmt" - "io" "os" + "github.com/hookdeck/EventKit/internal/config" + "github.com/hookdeck/EventKit/internal/flag" "github.com/hookdeck/EventKit/internal/services/api" "github.com/hookdeck/EventKit/internal/services/data" "github.com/hookdeck/EventKit/internal/services/delivery" @@ -17,13 +18,24 @@ type Service interface { Run(ctx context.Context) error } -func run(ctx context.Context, w io.Writer, args []string) error { - serviceName := "" - if len(args) > 1 { - serviceName = args[1] +func run(ctx context.Context) error { + flags := flag.Parse() + if err := config.Parse(flags.Config); err != nil { + return err } - if serviceName == "" { + switch flags.Service { + + case "api": + return api.Run(ctx) + + case "delivery": + return delivery.Run(ctx) + + case "data": + return data.Run(ctx) + + case "": // Run all services. // TODO: Investigate how goroutine affect graceful shutdown, fatal, restart, etc. // @see https://github.com/gin-gonic/gin/issues/346 @@ -41,20 +53,14 @@ func run(ctx context.Context, w io.Writer, args []string) error { err := g.Wait() return err - } else if serviceName == "api" { - return api.Run(ctx) - } else if serviceName == "delivery" { - return delivery.Run(ctx) - } else if serviceName == "data" { - return data.Run(ctx) - } else { - return errors.New(fmt.Sprintf("unknown service: %s", serviceName)) + default: + return errors.New(fmt.Sprintf("unknown service: %s", flags.Service)) } } func main() { ctx := context.Background() - if err := run(ctx, os.Stdout, os.Args); err != nil { + if err := run(ctx); err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } diff --git a/go.mod b/go.mod index c6fbcc8a..ca64d54b 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gin-gonic/gin v1.10.0 github.com/google/uuid v1.6.0 github.com/redis/go-redis/v9 v9.6.1 + github.com/spf13/viper v1.19.0 golang.org/x/sync v0.8.0 ) @@ -16,26 +17,41 @@ require ( github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.22.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect golang.org/x/arch v0.8.0 // indirect golang.org/x/crypto v0.23.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index b14e4ed6..36e8ef49 100644 --- a/go.sum +++ b/go.sum @@ -13,10 +13,15 @@ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -33,21 +38,31 @@ github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4 github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -55,10 +70,27 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4= github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -71,15 +103,23 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= @@ -90,12 +130,13 @@ golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go index e68eaf22..f43655eb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,32 +2,47 @@ package config import ( "log" - "os" "strconv" + + "github.com/spf13/viper" ) var ( - Port = 0 + Port int - RedisHost = "" - RedisPort = "" - RedisPassword = "" - RedisDatabase = 0 + RedisHost string + RedisPort string + RedisPassword string + RedisDatabase int ) -func init() { - Port = mustInt(os.Getenv("PORT")) +func Parse(configFile string) error { + if configFile != "" { + viper.SetConfigFile(configFile) + if err := viper.ReadInConfig(); err != nil { + return err + } + } + + viper.BindEnv("PORT") + Port = mustInt("PORT") + + viper.BindEnv("REDIS_HOST") + viper.BindEnv("REDIS_PORT") + viper.BindEnv("REDIS_PASSWORD") + viper.BindEnv("REDIS_DATABASE") + RedisHost = viper.GetString("REDIS_HOST") + RedisPort = viper.GetString("REDIS_PORT") + RedisPassword = viper.GetString("REDIS_PASSWORD") + RedisDatabase = mustInt("REDIS_DATABASE") - RedisHost = os.Getenv("REDIS_HOST") - RedisPort = os.Getenv("REDIS_PORT") - RedisPassword = os.Getenv("REDIS_PASSWORD") - RedisDatabase = mustInt(os.Getenv("REDIS_DATABASE")) + return nil } -func mustInt(s string) int { - i, err := strconv.Atoi(s) +func mustInt(configName string) int { + i, err := strconv.Atoi(viper.GetString(configName)) if err != nil { - log.Fatalf("failed to parse %s as int", s) + log.Fatalf("%s = '%s' is not int", configName, viper.GetString(configName)) } return i } diff --git a/internal/flag/flag.go b/internal/flag/flag.go new file mode 100644 index 00000000..573d81b1 --- /dev/null +++ b/internal/flag/flag.go @@ -0,0 +1,26 @@ +package flag + +import "flag" + +var ( + service string + config string +) + +func init() { + flag.StringVar(&service, "service", "", "service (e.g. api, data, delivery). If empty, all services will run.") + flag.StringVar(&config, "config", "", "config file (e.g. .env, config.yaml)") +} + +type Flags struct { + Service string + Config string // Config file path +} + +func Parse() Flags { + flag.Parse() + return Flags{ + Service: service, + Config: config, + } +} From e21635634ddab53aaed0c73874d1219e5a1cadaa Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 02:50:10 +0700 Subject: [PATCH 03/10] refactor: Services & graceful shutdown --- cmd/app/main.go | 79 +++++++++++++++----------- internal/services/api/api.go | 44 +++++++------- internal/services/api/router.go | 4 ++ internal/services/data/data.go | 14 ++++- internal/services/delivery/delivery.go | 14 ++++- 5 files changed, 101 insertions(+), 54 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 86e80d8d..5cc0a7c3 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -5,63 +5,78 @@ import ( "errors" "fmt" "os" + "os/signal" + "sync" + "syscall" "github.com/hookdeck/EventKit/internal/config" "github.com/hookdeck/EventKit/internal/flag" "github.com/hookdeck/EventKit/internal/services/api" "github.com/hookdeck/EventKit/internal/services/data" "github.com/hookdeck/EventKit/internal/services/delivery" - "golang.org/x/sync/errgroup" ) type Service interface { Run(ctx context.Context) error } +func main() { + ctx := context.Background() + if err := run(ctx); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } +} + func run(ctx context.Context) error { flags := flag.Parse() if err := config.Parse(flags.Config); err != nil { return err } - switch flags.Service { - - case "api": - return api.Run(ctx) + // Set up cancellation context and waitgroup + ctx, cancel := context.WithCancel(context.Background()) + wg := &sync.WaitGroup{} - case "delivery": - return delivery.Run(ctx) + services := []Service{} + switch flags.Service { + case "api": + services = append(services, api.NewService(ctx, wg)) case "data": - return data.Run(ctx) - + services = append(services, data.NewService(ctx, wg)) + case "delivery": + services = append(services, delivery.NewService(ctx, wg)) case "": - // Run all services. - // TODO: Investigate how goroutine affect graceful shutdown, fatal, restart, etc. - // @see https://github.com/gin-gonic/gin/issues/346 - - var g errgroup.Group - g.Go(func() error { - return api.Run(ctx) - }) - g.Go(func() error { - return delivery.Run(ctx) - }) - g.Go(func() error { - return data.Run(ctx) - }) - - err := g.Wait() - return err + services = append(services, + api.NewService(ctx, wg), + data.NewService(ctx, wg), + delivery.NewService(ctx, wg), + ) default: return errors.New(fmt.Sprintf("unknown service: %s", flags.Service)) } -} -func main() { - ctx := context.Background() - if err := run(ctx); err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) + // Register services with waitgroup. + // Once all services are done, we can exit. + // Each service will wait for the context to be cancelled before shutting down. + wg.Add(len(services)) + + // Start services + for _, service := range services { + go service.Run(ctx) } + + // Handle sigterm and await termChan signal + termChan := make(chan os.Signal) + signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM) + + <-termChan // Blocks here until interrupted + + // Handle shutdown + fmt.Println("*********************************\nShutdown signal received\n*********************************") + cancel() // Signal cancellation to context.Context + wg.Wait() // Block here until are workers are done + + return nil } diff --git a/internal/services/api/api.go b/internal/services/api/api.go index deae5e13..cd4934b1 100644 --- a/internal/services/api/api.go +++ b/internal/services/api/api.go @@ -6,43 +6,47 @@ import ( "log" "net/http" "os" - "os/signal" "sync" "time" "github.com/hookdeck/EventKit/internal/config" ) -func Run(ctx context.Context) error { - log.Println("running api service") - ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) - defer cancel() +type APIService struct { + server *http.Server +} - r := NewRouter() - httpServer := &http.Server{ +func NewService(ctx context.Context, wg *sync.WaitGroup) *APIService { + service := &APIService{} + service.server = &http.Server{ Addr: fmt.Sprintf(":%d", config.Port), - Handler: r, + Handler: NewRouter(), } - go func() { - log.Printf("listening on %s\n", httpServer.Addr) - if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - fmt.Fprintf(os.Stderr, "error listening and serving: %s\n", err) - } - }() - var wg sync.WaitGroup - wg.Add(1) + go func() { defer wg.Done() <-ctx.Done() + log.Println("shutting down api service") // make a new context for Shutdown - shutdownCtx := context.Background() - shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second) + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if err := httpServer.Shutdown(shutdownCtx); err != nil { + if err := service.server.Shutdown(shutdownCtx); err != nil { fmt.Fprintf(os.Stderr, "error shutting down http server: %s\n", err) } + log.Println("http server shutted down") }() - wg.Wait() + return service +} + +func (s *APIService) Run(ctx context.Context) error { + log.Println("running api service") + log.Printf("listening on %s\n", s.server.Addr) + go func() { + if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + fmt.Fprintf(os.Stderr, "error listening and serving: %s\n", err) + // return err + } + }() return nil } diff --git a/internal/services/api/router.go b/internal/services/api/router.go index 77391b02..b8946b99 100644 --- a/internal/services/api/router.go +++ b/internal/services/api/router.go @@ -1,7 +1,9 @@ package api import ( + "log" "net/http" + "time" "github.com/gin-gonic/gin" "github.com/hookdeck/EventKit/internal/destination" @@ -11,6 +13,8 @@ func NewRouter() http.Handler { r := gin.Default() r.GET("/healthz", func(c *gin.Context) { + time.Sleep(5 * time.Second) + log.Println("health") c.Status(http.StatusOK) }) diff --git a/internal/services/data/data.go b/internal/services/data/data.go index ad2a68f0..f4acd1eb 100644 --- a/internal/services/data/data.go +++ b/internal/services/data/data.go @@ -5,12 +5,24 @@ import ( "fmt" "log" "os" + "sync" "time" "github.com/hookdeck/EventKit/internal/redis" ) -func Run(ctx context.Context) error { +type DataService struct{} + +func NewService(ctx context.Context, wg *sync.WaitGroup) *DataService { + go func() { + defer wg.Done() + <-ctx.Done() + log.Println("shutting down data service") + }() + return &DataService{} +} + +func (s *DataService) Run(ctx context.Context) error { log.Println("running data service") if os.Getenv("DISABLED") == "true" { diff --git a/internal/services/delivery/delivery.go b/internal/services/delivery/delivery.go index a12675a0..4dfd77db 100644 --- a/internal/services/delivery/delivery.go +++ b/internal/services/delivery/delivery.go @@ -3,9 +3,21 @@ package delivery import ( "context" "log" + "sync" ) -func Run(ctx context.Context) error { +type DeliveryService struct{} + +func NewService(ctx context.Context, wg *sync.WaitGroup) *DeliveryService { + go func() { + defer wg.Done() + <-ctx.Done() + log.Println("shutting down data service") + }() + return &DeliveryService{} +} + +func (s *DeliveryService) Run(ctx context.Context) error { log.Println("running delivery service") return nil } From 9639c0048a18a6cd21cc230af3af1e376ed996b2 Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 03:08:09 +0700 Subject: [PATCH 04/10] chore: Remove sleep timer in healthz endpoint --- internal/services/api/router.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/services/api/router.go b/internal/services/api/router.go index b8946b99..77391b02 100644 --- a/internal/services/api/router.go +++ b/internal/services/api/router.go @@ -1,9 +1,7 @@ package api import ( - "log" "net/http" - "time" "github.com/gin-gonic/gin" "github.com/hookdeck/EventKit/internal/destination" @@ -13,8 +11,6 @@ func NewRouter() http.Handler { r := gin.Default() r.GET("/healthz", func(c *gin.Context) { - time.Sleep(5 * time.Second) - log.Println("health") c.Status(http.StatusOK) }) From 31dd073e9f83cbcb645a03260fc8ea8fbed64b1f Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 03:17:32 +0700 Subject: [PATCH 05/10] feat: Add production Dockerfile --- configs/production.Dockerfile | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 configs/production.Dockerfile diff --git a/configs/production.Dockerfile b/configs/production.Dockerfile new file mode 100644 index 00000000..389cd658 --- /dev/null +++ b/configs/production.Dockerfile @@ -0,0 +1,13 @@ +# Stage 0 +# Build the binary +FROM golang:1.23-alpine +WORKDIR /app +COPY . . + +RUN go build -o ./bin/eventkit ./cmd/app/main.go + +# Stage 1 +# Copy binary to a new image +FROM scratch +COPY --from=0 /app/bin/eventkit /bin/eventkit +ENTRYPOINT ["/bin/eventkit"] From 3073e744d1494afd252414c2c69fcb0d818de06a Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 03:18:04 +0700 Subject: [PATCH 06/10] chore: Add example to run EventKit with a local config --- examples/with-yaml-config/Dockerfile | 7 +++++++ examples/with-yaml-config/README.md | 27 +++++++++++++++++++++++++++ examples/with-yaml-config/config.yaml | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 examples/with-yaml-config/Dockerfile create mode 100644 examples/with-yaml-config/README.md create mode 100644 examples/with-yaml-config/config.yaml diff --git a/examples/with-yaml-config/Dockerfile b/examples/with-yaml-config/Dockerfile new file mode 100644 index 00000000..4f064030 --- /dev/null +++ b/examples/with-yaml-config/Dockerfile @@ -0,0 +1,7 @@ +FROM eventkit + +# Copy local config file to the container +COPY config.yaml config.yaml + +# Run EventKit with the copied config file +ENTRYPOINT ["/bin/eventkit", "-config", "config.yaml"] diff --git a/examples/with-yaml-config/README.md b/examples/with-yaml-config/README.md new file mode 100644 index 00000000..c4e46c49 --- /dev/null +++ b/examples/with-yaml-config/README.md @@ -0,0 +1,27 @@ +# Example Docker deployment with custom YAML config + +## 0. Make sure you have EventKit's Docker image locally + +During development when EventKit's image is not live, you may need to manually build the Docker image from source. + +```sh +# from hookdeck/EventKit directory +$ docker build -t eventkit . +``` + +## 1. Build your image + +```sh +# from hookdeck/EventKit/examples/with-yaml-config +$ docker build -t myeventkit . +``` + +## 2. Run EventKit with your custom config + +```sh +# from hookdeck/EventKit/examples/with-yaml-config +$ docker run -p 4000:4000 myeventkit --service api +$ docker run -p 4000:4000 myeventkit --service data +$ docker run -p 4000:4000 myeventkit --service delivery +$ docker run -p 4000:4000 myeventkit # or run all 3 services in one process +``` diff --git a/examples/with-yaml-config/config.yaml b/examples/with-yaml-config/config.yaml new file mode 100644 index 00000000..e7f0d4c6 --- /dev/null +++ b/examples/with-yaml-config/config.yaml @@ -0,0 +1,5 @@ +PORT: "4000" +REDIS_HOST: "127.0.0.1" +REDIS_PORT: "6379" +REDIS_PASSWORD: "password" +REDIS_DATABASE: "0" From 9bccfd9f2cd91cf59c7d84440e907df00a498058 Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 03:22:17 +0700 Subject: [PATCH 07/10] chore: Update example README with better Docker run commands --- examples/with-yaml-config/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/with-yaml-config/README.md b/examples/with-yaml-config/README.md index c4e46c49..4a24f2cd 100644 --- a/examples/with-yaml-config/README.md +++ b/examples/with-yaml-config/README.md @@ -20,8 +20,9 @@ $ docker build -t myeventkit . ```sh # from hookdeck/EventKit/examples/with-yaml-config +$ docker run -p 4000:4000 myeventkit # run all 3 services in one process +# or $ docker run -p 4000:4000 myeventkit --service api -$ docker run -p 4000:4000 myeventkit --service data -$ docker run -p 4000:4000 myeventkit --service delivery -$ docker run -p 4000:4000 myeventkit # or run all 3 services in one process +$ docker run myeventkit --service data +$ docker run myeventkit --service delivery ``` From 2fa279757174c624b04756585b0f0bc5f9c0a883 Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 04:20:07 +0700 Subject: [PATCH 08/10] feat: Support default config --- internal/config/config.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index f43655eb..f2943abf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,12 +11,24 @@ var ( Port int RedisHost string - RedisPort string + RedisPort int RedisPassword string RedisDatabase int ) +var defaultConfig = map[string]any{ + "PORT": 3333, + "REDIS_HOST": "127.0.0.1", + "REDIS_PORT": 6379, + "REDIS_PASSWORD": "", + "REDIS_DATABASE": 0, +} + func Parse(configFile string) error { + for key, value := range defaultConfig { + viper.SetDefault(key, value) + } + if configFile != "" { viper.SetConfigFile(configFile) if err := viper.ReadInConfig(); err != nil { @@ -24,15 +36,11 @@ func Parse(configFile string) error { } } - viper.BindEnv("PORT") - Port = mustInt("PORT") + viper.AutomaticEnv() - viper.BindEnv("REDIS_HOST") - viper.BindEnv("REDIS_PORT") - viper.BindEnv("REDIS_PASSWORD") - viper.BindEnv("REDIS_DATABASE") + Port = mustInt("PORT") RedisHost = viper.GetString("REDIS_HOST") - RedisPort = viper.GetString("REDIS_PORT") + RedisPort = mustInt("REDIS_PORT") RedisPassword = viper.GetString("REDIS_PASSWORD") RedisDatabase = mustInt("REDIS_DATABASE") From 245d52cc750bbd5df6922592824e43c327e82af4 Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 04:24:47 +0700 Subject: [PATCH 09/10] chore: Typo --- cmd/app/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 5cc0a7c3..c9237df8 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -76,7 +76,7 @@ func run(ctx context.Context) error { // Handle shutdown fmt.Println("*********************************\nShutdown signal received\n*********************************") cancel() // Signal cancellation to context.Context - wg.Wait() // Block here until are workers are done + wg.Wait() // Block here until all workers are done return nil } From f171339ec65a36707fc04d08f7452fcbc7e5dcc5 Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 23 Aug 2024 04:25:10 +0700 Subject: [PATCH 10/10] chore: go mod tidy --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index ca64d54b..f7a32932 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/google/uuid v1.6.0 github.com/redis/go-redis/v9 v9.6.1 github.com/spf13/viper v1.19.0 - golang.org/x/sync v0.8.0 ) require ( diff --git a/go.sum b/go.sum index 36e8ef49..d8a0c190 100644 --- a/go.sum +++ b/go.sum @@ -122,8 +122,6 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjs golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=