Skip to content

Commit f3b45ad

Browse files
committed
feat: implement reacttv-server service
1 parent 81fd233 commit f3b45ad

File tree

15 files changed

+142
-30
lines changed

15 files changed

+142
-30
lines changed

.releaserc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[
1515
"@semantic-release/exec",
1616
{
17-
"prepareCmd": "make build version=${nextRelease.version}",
17+
"prepareCmd": "make build version=${nextRelease.version} commit=${nextRelease.gitHead}",
1818
"publishCmd": "make push version=${nextRelease.version} username=$CONTAINER_REGISTRY_USERNAME password=$CONTAINER_REGISTRY_PASSWORD"
1919
}
2020
]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ version: ## Version prints out the next semantic version
1111
login: ## Authenticates with Docker Registry (example: make login username=$DOCKER_USER password=$DOCKER_PASSWORD)
1212
@echo "Authenticating with docker registry (ghcr.io)..." ; docker login ghcr.io --username $(username) --password $(password)
1313

14-
build: ## Builds all services with a tagged version (example: make build version=v1.0.0)
15-
@cd deploy/scripts ; source docker.sh ; build $(version)
14+
build: ## Builds all services with a tagged version (example: make build version=v1.0.0 commit=`git rev-parse HEAD` )
15+
@cd deploy/scripts ; source docker.sh ; build $(version) $(commit)
1616

1717
push: login ## Push all services with a tagged version (example: make build version=v1.0.0 username=$DOCKER_USER password=$DOCKER_PASSWORD)
1818
@cd deploy/scripts ; source docker.sh ; push $(version)

deploy/scripts/docker.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ registry=ghcr.io/reacttv
1212
# Run "docker build" against the services/* dirs.
1313
function build() {
1414
version=$1
15+
commit=$2
16+
time=$(date +%FT%T%z)
1517

1618
cd $reacttv_base
1719
for service in "${services[@]}"; do
1820
service_image_name=$registry/$(basename $service):$version
1921
echo "docker build: $service_image_name"
20-
docker build -f $service/Dockerfile . -t $service_image_name --build-arg VERSION="itworks" --target prod
22+
docker build \
23+
-f $service/Dockerfile . \
24+
-t $service_image_name \
25+
--build-arg VERSION=$version \
26+
--build-arg GITCOMMIT=$commit \
27+
--build-arg DATE=$time \
28+
--target prod
2129
done
2230
}
2331

services/example/cmd/main.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

services/example/go.mod

Lines changed: 0 additions & 3 deletions
This file was deleted.

services/example/Dockerfile renamed to services/server/Dockerfile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ ENV CGO_ENABLED=0
99
ENV GOOS=linux
1010
ENV GOPROXY=https://proxy.golang.org,direct
1111

12-
ARG VERSION
13-
ARG ServiceDir=services/example
12+
ARG ServiceDir=services/server
1413

1514
RUN mkdir -p /app
1615
WORKDIR /app
@@ -21,7 +20,7 @@ WORKDIR $ServiceDir
2120

2221
RUN go mod vendor
2322

24-
RUN IMAGE_VERSION=$VERSION && go build -mod=vendor -o /app/example ./cmd/main.go
23+
RUN go build -mod=vendor -o /app/server ./cmd/main.go
2524

2625
# ---------- PRODUCTION BUILD -------------------
2726

@@ -35,7 +34,10 @@ ENV GOOS=linux
3534
ENV GOPROXY=https://proxy.golang.org,direct
3635

3736
ARG VERSION
38-
ARG ServiceDir=services/example
37+
ARG GITCOMMIT
38+
ARG DATE
39+
40+
ARG ServiceDir=services/server
3941

4042
RUN mkdir -p /app
4143
WORKDIR /app
@@ -46,13 +48,17 @@ WORKDIR $ServiceDir
4648

4749
RUN go mod vendor
4850

49-
RUN IMAGE_VERSION=$VERSION && go build -ldflags="-X example.Version=$IMAGE_VERSION" -o /app/example ./cmd/main.go
51+
RUN \
52+
VERSION=$VERSION \
53+
DATE=$DATE \
54+
GITCOMMIT=$GITCOMMIT \
55+
go build -mod=vendor -ldflags="-X 'server/pkg/version.BinVersion=${VERSION}' -X 'server/pkg/version.GitCommit=${GITCOMMIT}' -X 'server/pkg/version.BuildTime=${DATE}'" -o /app/server ./cmd/main.go
5056

5157
FROM alpine:$ALPINE_VERSION as prod
5258

53-
ARG ServiceDir=services/example
59+
ARG ServiceDir=services/server
5460

5561
WORKDIR /app
56-
COPY --from=builder /app/example .
62+
COPY --from=builder /app/server .
5763

58-
CMD ["/app/example"]
64+
CMD ["/app/server"]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package commands
2+
3+
const (
4+
Name = "reacttv-server"
5+
)
6+
7+
type Globals struct {
8+
LogLevel string `enum:"panic,fatal,error,warn,info,debug,trace" default:"info"`
9+
LogFormat string `enum:"text,json" default:"text"`
10+
}
11+
12+
type DataMangementCLI struct {
13+
Globals
14+
Server Server `cmd:"" help:"Start Server with the given args"`
15+
Version Version `cmd:"" help:"Current Server version"`
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package commands
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
)
6+
7+
type Server struct {
8+
DBHost string `required help:"Postgres hostname or IP address"`
9+
DBUser string `required help:"Postgres username"`
10+
DBPassword string `required help"Postgres password"`
11+
DBName string `required help"Postgres database name"`
12+
Port string `required help:"Port the server listens on"`
13+
}
14+
15+
func (s *Server) Run(globals *Globals) error {
16+
l := log.New()
17+
lvl, err := log.ParseLevel(globals.LogLevel)
18+
if err != nil {
19+
l.Fatal(err)
20+
}
21+
l.SetLevel(lvl)
22+
return nil
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package commands
2+
3+
import (
4+
"server/pkg/version"
5+
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
type Version struct{}
10+
11+
func (s *Version) Run(globals *Globals) error {
12+
log.WithFields(log.Fields{
13+
"BuildTime": version.BuildTime,
14+
"Commit": version.GitCommit,
15+
"Runtime": version.Runtime(),
16+
}).Println(version.BinVersion)
17+
return nil
18+
}

services/server/cmd/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"server/cmd/commands"
5+
6+
"github.com/alecthomas/kong"
7+
)
8+
9+
func main() {
10+
cli := commands.DataMangementCLI{
11+
Globals: commands.Globals{},
12+
}
13+
14+
ctx := kong.Parse(&cli,
15+
kong.Name(commands.Name),
16+
kong.UsageOnError(),
17+
kong.ConfigureHelp(kong.HelpOptions{Compact: true}))
18+
err := ctx.Run(&cli.Globals)
19+
ctx.FatalIfErrorf(err)
20+
}

0 commit comments

Comments
 (0)