Skip to content

Commit

Permalink
fix: fix dockerfile and pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmann committed Aug 27, 2024
1 parent c6b7e8b commit 5242e5d
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 141 deletions.
31 changes: 12 additions & 19 deletions .docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#############################################
# Preparer go
#############################################
FROM golang:1.22-alpine AS preparer_go
FROM golang:1.23-alpine AS preparer_go

ARG MOCKER_VERSION="v2.43.2"

WORKDIR /app/build

# install build dependencies
RUN go install github.com/vektra/mockery/v2@${MOCKER_VERSION}
RUN go install github.com/swaggo/swag/cmd/swag@latest

COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./Makefile ./go.mod ./go.sum ./
RUN apk add --no-cache make git
RUN make setup/ci

COPY . .

RUN go generate


#############################################
# Builder go
#############################################
Expand All @@ -30,15 +25,12 @@ ARG APP_GIT_BRANCH="develop"
ARG APP_GIT_REPOSITORY="https://github.com/green-ecolution/green-ecolution-backend"
ARG APP_BUILD_TIME="unknown"

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags " \
-X main.version=$APP_VERSION \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.version=$APP_VERSION \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitCommit=$APP_GIT_COMMIT \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitBranch=$APP_GIT_BRANCH \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitRepository=$APP_GIT_REPOSITORY \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.buildTime=$APP_BUILD_TIME \
" -o /app/build/backend

RUN make build \
APP_VERSION=${APP_VERSION} \
APP_GIT_COMMIT=${APP_GIT_COMMIT} \
APP_GIT_BRANCH=${APP_GIT_BRANCH} \
APP_GIT_REPOSITORY=${APP_GIT_REPOSITORY} \
APP_BUILD_TIME=${APP_BUILD_TIME}

#############################################
# Runner go
Expand All @@ -54,6 +46,7 @@ RUN adduser -D gorunner
USER gorunner
WORKDIR /app

COPY --chown=gorunner:gorunner --from=builder /app/build/backend /app/backend
COPY --chown=gorunner:gorunner --from=builder /app/build/.docker/config.default.yaml /app/config.yaml
COPY --chown=gorunner:gorunner --from=builder /app/build/bin/green-ecolution-backend /app/backend

ENTRYPOINT [ "/app/backend" ]
28 changes: 11 additions & 17 deletions .docker/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ ARG MOCKER_VERSION="v2.43.2"
WORKDIR /app/build

# install build dependencies
RUN go install github.com/vektra/mockery/v2@${MOCKER_VERSION}
RUN go install github.com/swaggo/swag/cmd/swag@latest

COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./Makefile ./go.mod ./go.sum ./
RUN apk add --no-cache make git
RUN make setup/ci

COPY . .

RUN go generate


#############################################
# Builder go
#############################################
Expand All @@ -30,14 +25,12 @@ ARG APP_GIT_BRANCH="main"
ARG APP_GIT_REPOSITORY="https://github.com/green-ecolution/green-ecolution-backend"
ARG APP_BUILD_TIME="unknown"

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags " \
-X main.version=$APP_VERSION \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.version=$APP_VERSION \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitCommit=$APP_GIT_COMMIT \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitBranch=$APP_GIT_BRANCH \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitRepository=$APP_GIT_REPOSITORY \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.buildTime=$APP_BUILD_TIME \
" -o /app/build/backend
RUN make build \
APP_VERSION=${APP_VERSION} \
APP_GIT_COMMIT=${APP_GIT_COMMIT} \
APP_GIT_BRANCH=${APP_GIT_BRANCH} \
APP_GIT_REPOSITORY=${APP_GIT_REPOSITORY} \
APP_BUILD_TIME=${APP_BUILD_TIME}


#############################################
Expand All @@ -53,6 +46,7 @@ RUN adduser -D gorunner
USER gorunner
WORKDIR /app

COPY --chown=gorunner:gorunner --from=builder /app/build/backend /app/backend
COPY --chown=gorunner:gorunner --from=builder /app/build/.docker/config.default.yaml /app/config.yaml
COPY --chown=gorunner:gorunner --from=builder /app/build/bin/green-ecolution-backend /app/backend

ENTRYPOINT [ "/app/backend" ]
31 changes: 12 additions & 19 deletions .docker/Dockerfile.stage
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#############################################
# Preparer go
#############################################
FROM golang:1.22-alpine AS preparer_go
FROM golang:1.23-alpine AS preparer_go

ARG MOCKER_VERSION="v2.43.2"

WORKDIR /app/build

# install build dependencies
RUN go install github.com/vektra/mockery/v2@${MOCKER_VERSION}
RUN go install github.com/swaggo/swag/cmd/swag@latest

COPY ./go.mod ./go.sum ./
RUN go mod download
COPY ./Makefile ./go.mod ./go.sum ./
RUN apk add --no-cache make git
RUN make setup/ci

COPY . .

RUN go generate


#############################################
# Builder go
#############################################
Expand All @@ -30,15 +25,12 @@ ARG APP_GIT_BRANCH="stage-deployment"
ARG APP_GIT_REPOSITORY="https://github.com/green-ecolution/green-ecolution-backend"
ARG APP_BUILD_TIME="unknown"

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags " \
-X main.version=$APP_VERSION \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.version=$APP_VERSION \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitCommit=$APP_GIT_COMMIT \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitBranch=$APP_GIT_BRANCH \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.gitRepository=$APP_GIT_REPOSITORY \
-X github.com/green-ecolution/green-ecolution-backend/internal/storage/local/info.buildTime=$APP_BUILD_TIME \
" -o /app/build/backend

RUN make build \
APP_VERSION=${APP_VERSION} \
APP_GIT_COMMIT=${APP_GIT_COMMIT} \
APP_GIT_BRANCH=${APP_GIT_BRANCH} \
APP_GIT_REPOSITORY=${APP_GIT_REPOSITORY} \
APP_BUILD_TIME=${APP_BUILD_TIME}

#############################################
# Runner go
Expand All @@ -53,6 +45,7 @@ RUN adduser -D gorunner
USER gorunner
WORKDIR /app

COPY --chown=gorunner:gorunner --from=builder /app/build/backend /app/backend
COPY --chown=gorunner:gorunner --from=builder /app/build/.docker/config.default.yaml /app/config.yaml
COPY --chown=gorunner:gorunner --from=builder /app/build/bin/green-ecolution-backend /app/backend

ENTRYPOINT [ "/app/backend" ]
36 changes: 36 additions & 0 deletions .docker/config.default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
dashboard:
title: Green Ecolution Dashboard

server:
port: 3000
development: false
app_url: http://localhost:3030
logs:
level: info
format: text
database:
host: localhost
port: 5432
username: postgres
password: super_secret_password
name: green_ecolution_db

auth:
keycloak:
baseurl: https://auth.green-ecolution.de
realm: green-ecolution-dev
client_id: green-ecolution-backend
client_secret: secret_secret_secret
realm_public_key: secret_secret_secret
frontend:
auth_url: https://auth.green-ecolution.de/realms/green-ecolution-dev/protocol/openid-connect/auth
token_url: https://auth.green-ecolution.de/realms/green-ecolution-dev/protocol/openid-connect/token
client_id: green-ecolution-frontend
client_secret: secret_secret_secret

mqtt:
broker: eu1.cloud.thethings.industries:1883
client_id: tree-sensor
username: sgr-students@zde
password: secret_secret_secret
topic: v3/sgr-students@zde/devices/tree-sensor/up
9 changes: 3 additions & 6 deletions .github/workflows/build-and-push-develop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,11 @@ jobs:
with:
go-version: stable

- name: Install mockery
run: go install github.com/vektra/mockery/[email protected]

- name: Install swagger
run: go install github.com/swaggo/swag/cmd/swag@latest
- name: Install dependencies
run: make setup/ci

- name: Go generate
run: go generate
run: make generate

- name: Move swagger docs
run: mv ./docs/swagger.json ./docs/api-docs.json
Expand Down
81 changes: 32 additions & 49 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package config

import (
"errors"
"log/slog"
"time"

"github.com/green-ecolution/green-ecolution-backend/internal/logger"
"github.com/spf13/viper"
)

type DatabaseConfig struct {
Host string `env:"HOST" envDefault:"localhost"`
Port int `env:"PORT" envDefault:"27017"`
Username string `env:"USER" envDefault:"root"`
Password string `env:"PASSWORD" envDefault:"example"`
Name string `env:"NAME" envDefault:"green-space-management"`
Timeout time.Duration `env:"TIMEOUT" envDefault:"10s"`
Host string
Port int
Username string
Password string
Name string
Timeout time.Duration
}

type MQTTConfig struct {
Broker string `env:"BROKER" envDefault:"eu1.cloud.thethings.network:1883"`
ClientID string `mapstructure:"client_id" env:"CLIENT_ID"`
Username string `env:"USERNAME"`
Password string `env:"PASSWORD"`
Topic string `env:"TOPIC"`
Broker string
ClientID string `mapstructure:"client_id"`
Username string
Password string
Topic string
}

type LogConfig struct {
Expand All @@ -32,65 +30,50 @@ type LogConfig struct {
}

type ServerConfig struct {
Logs LogConfig `envPrefix:"LOGS"`
Database DatabaseConfig `envPrefix:"DATABASE"`
Port int `env:"PORT" envDefault:"3000"`
Development bool `env:"DEVELOPMENT" envDefault:"false"`
AppURL string `mapstructure:"app_url" env:"APP_URL" envDefault:"http://localhost:$PORT"`
Logs LogConfig
Database DatabaseConfig
Port int
Development bool
AppURL string `mapstructure:"app_url"`
}

type DashboardConfig struct {
Title string `env:"TITLE" envDefault:"Green Ecolution Dashboard"`
Title string
}

type KeyCloakConfig struct {
BaseURL string `env:"BASE_URL"`
Realm string `env:"REALM"`
ClientID string `mapstructure:"client_id" env:"CLIENT_ID"`
ClientSecret string `mapstructure:"client_secret" env:"CLIENT_SECRET"`
RealmPublicKey string `mapstructure:"realm_public_key" env:"REALM_PUBLIC_KEY"`
BaseURL string
Realm string
ClientID string `mapstructure:"client_id"`
ClientSecret string `mapstructure:"client_secret"`
RealmPublicKey string `mapstructure:"realm_public_key"`
Frontend KeyCloakFrontendConfig
}

type KeyCloakFrontendConfig struct {
ClientID string `mapstructure:"client_id" env:"FRONTEND_CLIENT_ID"`
ClientSecret string `mapstructure:"client_secret" env:"FRONTEND_CLIENT_SECRET"`
AuthURL string `mapstructure:"auth_url" env:"FRONTEND_AUTH_URL"`
TokenURL string `mapstructure:"token_url" env:"FRONTEND_TOKEN_URL"`
ClientID string `mapstructure:"client_id"`
ClientSecret string `mapstructure:"client_secret"`
AuthURL string `mapstructure:"auth_url"`
TokenURL string `mapstructure:"token_url"`
}

type IdentityAuthConfig struct {
KeyCloak KeyCloakConfig `envPrefix:"KEYCLOAK_"`
KeyCloak KeyCloakConfig
}

type Config struct {
Server ServerConfig `envPrefix:"GE_SERVER_"`
Dashboard DashboardConfig `envPrefix:"GE_DASHBOARD_"`
MQTT MQTTConfig `envPrefix:"GE_MQTT_"`
IdentityAuth IdentityAuthConfig `mapstructure:"auth" envPrefix:"GE_AUTH_"`
Server ServerConfig
Dashboard DashboardConfig
MQTT MQTTConfig
IdentityAuth IdentityAuthConfig `mapstructure:"auth"`
}

var (
ErrViperConfigFileNotFound = viper.ConfigFileNotFoundError{}
ErrViperConfigFileError = errors.New("error loading config file with viper")
ErrEnvConfigError = errors.New("error loading config from environment variables")
)

func InitConfig() (*Config, error) {
slog.Info("Loading config...")

cfg, err := InitViper()
if err != nil {
if errors.Is(err, ErrViperConfigFileNotFound) {
slog.Info("Config file not found, trying to load from environment variables")
cfg, err = InitEnv()
if err != nil {
slog.Warn("Error loading config from environment variables", "error", err)
}
} else {
slog.Error("Error loading config file", "error", err)
return nil, err
}
return nil, err
}

slog.Info("Config loaded successfully")
Expand Down
19 changes: 0 additions & 19 deletions config/env.go

This file was deleted.

7 changes: 1 addition & 6 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ func InitViper() (*Config, error) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return nil, ErrViperConfigFileNotFound
} else {
slog.Error("Error reading config file", "error", err)
return nil, ErrViperConfigFileError
}
return nil, err
}

var cfg Config
Expand Down
Loading

0 comments on commit 5242e5d

Please sign in to comment.