Skip to content

Commit

Permalink
Rework cmds into single binary
Browse files Browse the repository at this point in the history
- Move igor commands into single binary
- Adds DB migrate tool
- Respeced DB setup into migrations for https://github.com/golang-migrate/migrate
- Moved User/Password setting to env vars for easier integration
- Fixed flaky unit test
  • Loading branch information
voidshard committed Mar 17, 2024
1 parent 0fa2f37 commit a70a694
Show file tree
Hide file tree
Showing 28 changed files with 518 additions and 433 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cmd/db_migrate/*.sql
cmd/db_migrate/00.sql
**/__pycache__
release.sh
39 changes: 39 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Build the application
FROM golang:1.21

WORKDIR /go/src/github.com/voidshard/igor
COPY go.mod ./
COPY go.sum ./
COPY cmd cmd
COPY pkg pkg
COPY internal internal
RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -o /igor ./cmd/igor/*.go

# Create a minimal image
FROM alpine

ARG USER=app
ARG GROUPNAME=$USER
ARG UID=12345
ARG GID=23456

RUN addgroup \
--gid "$GID" \
"$GROUPNAME" \
&& adduser \
--disabled-password \
--gecos "" \
--home "$(pwd)" \
--ingroup "$GROUPNAME" \
--no-create-home \
--uid "$UID" \
$USER

COPY --from=0 /igor /igor
COPY migrations migrations
RUN chown -R $USER:$GROUPNAME /igor migrations

USER $USER
ENTRYPOINT ["/igor"]
22 changes: 0 additions & 22 deletions Dockerfile.apiserver

This file was deleted.

20 changes: 0 additions & 20 deletions Dockerfile.worker

This file was deleted.

90 changes: 0 additions & 90 deletions cmd/apiserver/main.go

This file was deleted.

110 changes: 0 additions & 110 deletions cmd/db_migrate/01.tmpl

This file was deleted.

40 changes: 0 additions & 40 deletions cmd/db_migrate/run.sh

This file was deleted.

56 changes: 56 additions & 0 deletions cmd/igor/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"github.com/voidshard/igor/internal/utils"
"github.com/voidshard/igor/pkg/api"
"github.com/voidshard/igor/pkg/api/http/server"
"github.com/voidshard/igor/pkg/database"
"github.com/voidshard/igor/pkg/queue"
)

const (
docApi = `Run the API server`
)

type optsAPI struct {
optsGeneral
optsDatabase
optsQueue

Addr string `long:"addr" env:"ADDR" description:"Address to bind to" default:"localhost:8100"`
TLSCert string `long:"cert" env:"CERT" description:"Path to TLS certificate"`
TLSKey string `long:"key" env:"KEY" description:"Path to TLS key"`

StaticDir string `long:"static-dir" env:"STATIC_DIR" default:"" description:"Serve static files from this directory"`
}

func (c *optsAPI) Execute(args []string) error {
// This main runs an API server (in this case, http) so that callers can interact with Igor over HTTP.
// Since this is configured with OptionsClientDefault it does not run any background routines
// that Igor needs to function (ie, to process events, queue tasks etc).
//
// This is intended purely to serve Igor's service API to clients over the network. Though you could
// have one server type do both if you wanted.
//
// If you wished to interact with Igor via. importing the pkg libraries, then you don't need to run this.
//
// Alternatively, you could add more servers under pkg/api/ to serve Igor's API over other protocols like
// gRPC, thrift or whatever you like and modifiy this to serve them all.
tlsCfg, err := utils.TLSConfig(c.QueueTLSCaCert, c.QueueTLSCert, c.QueueTLSKey)
if err != nil {
panic(err)
}
qOpts := &queue.Options{URL: c.QueueURL, TLSConfig: tlsCfg}

api, err := api.New(
&database.Options{URL: c.DatabaseURL},
qOpts,
api.OptionsClientDefault(),
)
if err != nil {
panic(err)
}

s := server.NewServer(c.Addr, c.StaticDir, c.TLSCert, c.TLSKey, c.Debug)
return s.ServeForever(api)
}
Loading

0 comments on commit a70a694

Please sign in to comment.