Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions cmd/api/main.go

This file was deleted.

82 changes: 82 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion Dockerfile → configs/api.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
ENTRYPOINT CompileDaemon --build="go build -o ./bin/api ./cmd/app/main.go" --command="./bin/api api"
8 changes: 8 additions & 0 deletions configs/data.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions configs/delivery.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions configs/production.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
35 changes: 30 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions examples/with-yaml-config/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
28 changes: 28 additions & 0 deletions examples/with-yaml-config/README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions examples/with-yaml-config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PORT: "4000"
REDIS_HOST: "127.0.0.1"
REDIS_PORT: "6379"
REDIS_PASSWORD: "password"
REDIS_DATABASE: "0"
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
Loading