diff --git a/cmd/api/main.go b/cmd/api/main.go deleted file mode 100644 index 2dd76a1f..00000000 --- a/cmd/api/main.go +++ /dev/null @@ -1,56 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - "net/http" - "os" - "os/signal" - "sync" - "time" - - "github.com/hookdeck/EventKit/internal/config" - "github.com/hookdeck/EventKit/internal/router" -) - -func run(ctx context.Context) error { - ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) - defer cancel() - - r := router.New() - httpServer := &http.Server{ - Addr: fmt.Sprintf(":%d", config.Port), - Handler: r, - } - 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() - // make a new context for Shutdown - shutdownCtx := context.Background() - shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second) - defer cancel() - if err := httpServer.Shutdown(shutdownCtx); err != nil { - fmt.Fprintf(os.Stderr, "error shutting down http server: %s\n", err) - } - }() - wg.Wait() - - 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/cmd/app/main.go b/cmd/app/main.go new file mode 100644 index 00000000..c9237df8 --- /dev/null +++ b/cmd/app/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "context" + "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" +) + +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 + } + + // Set up cancellation context and waitgroup + ctx, cancel := context.WithCancel(context.Background()) + wg := &sync.WaitGroup{} + + services := []Service{} + + switch flags.Service { + case "api": + services = append(services, api.NewService(ctx, wg)) + case "data": + services = append(services, data.NewService(ctx, wg)) + case "delivery": + services = append(services, delivery.NewService(ctx, wg)) + case "": + 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)) + } + + // 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 all workers are done + + return nil +} 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/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"] 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/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..4a24f2cd --- /dev/null +++ b/examples/with-yaml-config/README.md @@ -0,0 +1,28 @@ +# 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 # run all 3 services in one process +# or +$ docker run -p 4000:4000 myeventkit --service api +$ docker run myeventkit --service data +$ docker run myeventkit --service delivery +``` 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" diff --git a/go.mod b/go.mod index 0574db8f..f7a32932 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 ) require ( @@ -15,26 +16,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 4a88d9e2..d8a0c190 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/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -88,12 +128,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..f2943abf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,32 +2,55 @@ package config import ( "log" - "os" "strconv" + + "github.com/spf13/viper" ) var ( - Port = 0 + Port int - RedisHost = "" - RedisPort = "" - RedisPassword = "" - RedisDatabase = 0 + RedisHost string + RedisPort int + RedisPassword string + RedisDatabase int ) -func init() { - Port = mustInt(os.Getenv("PORT")) +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 { + return err + } + } + + viper.AutomaticEnv() + + Port = mustInt("PORT") + RedisHost = viper.GetString("REDIS_HOST") + RedisPort = mustInt("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, + } +} diff --git a/internal/services/api/api.go b/internal/services/api/api.go new file mode 100644 index 00000000..cd4934b1 --- /dev/null +++ b/internal/services/api/api.go @@ -0,0 +1,52 @@ +package api + +import ( + "context" + "fmt" + "log" + "net/http" + "os" + "sync" + "time" + + "github.com/hookdeck/EventKit/internal/config" +) + +type APIService struct { + server *http.Server +} + +func NewService(ctx context.Context, wg *sync.WaitGroup) *APIService { + service := &APIService{} + service.server = &http.Server{ + Addr: fmt.Sprintf(":%d", config.Port), + Handler: NewRouter(), + } + + go func() { + defer wg.Done() + <-ctx.Done() + log.Println("shutting down api service") + // make a new context for Shutdown + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + 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") + }() + + 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/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..f4acd1eb --- /dev/null +++ b/internal/services/data/data.go @@ -0,0 +1,43 @@ +package data + +import ( + "context" + "fmt" + "log" + "os" + "sync" + "time" + + "github.com/hookdeck/EventKit/internal/redis" +) + +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" { + 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..4dfd77db --- /dev/null +++ b/internal/services/delivery/delivery.go @@ -0,0 +1,23 @@ +package delivery + +import ( + "context" + "log" + "sync" +) + +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 +}