Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Dockerfile and docker-compose #112

Merged
merged 3 commits into from
May 28, 2023
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
22 changes: 22 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
power-sso
powersso
.vscode
.public
.github
ui/
*.out
*.proto
nginx/
.git
migrations/
*.sql
**/*_test.go
vendor/
.*ignore
LICENSE
golangci.yml
Makefile
*.md
docker-compose.*.yml
.env
Dockerfile*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.so
*.dylib
power-sso
powersso

# Test binary, built with `go test -c`
*.test
Expand Down
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:1.19.4-alpine3.17 AS builder

WORKDIR /app

ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64

COPY . .

RUN go get -d -v ./...
RUN go install -v ./...
RUN go build -v -o /bin/powersso .

FROM alpine

ENV TZ=America/Fortaleza

WORKDIR /app

COPY --from=builder /bin/powersso .

RUN apk add tzdata ca-certificates && \
adduser -D --uid 1000 userpowersso userpowersso && \
mkdir -p /var/log/powersso && \
chown userpowersso:userpowersso /var/log/powersso -R && \
chown userpowersso:userpowersso /app

EXPOSE 5000 5555

USER userpowersso

CMD ["./powersso"]
41 changes: 32 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,55 @@
# Use of this source code is governed by MIT style
# license that can be found in the LICENSE file.

.PHONY: run local down-local docker-clean logs-local migrate-version migrate-up migrate-down migrate-force
.PHONY: build run local down-local docker-clean logs-local migrate-version migrate-up migrate-down migrate-force

FILES := $(shell docker ps -aq)
DB_LOCAL := "postgres://postgres:postgres@localhost:5432/power-sso?sslmode=disable"

# - trimpath - will remove the filepathes from the reports, good to same money on network trafic,
# focus on bug reports, and find issues fast.
# - race - adds a racedetector, in case of racecondition, you can catch report with sentry.
# https://golang.org/doc/articles/race_detector.html
build: ## Builds binary
@ printf "Building aplication... "
@ go build \
-trimpath \
-o powersso \
./
@ echo "done"

.ONESHELL:
image-build: ## Docker Build
@ echo "Docker Build"
@ DOCKER_BUILDKIT=0 docker build \
--file Dockerfile \
--tag powersso \
.

run:
go run main.go
@ go run main.go

test:
go test ./...
@ go test ./...

local:
docker compose -f docker-compose.local.yml up -d --build
@ docker compose -f ./docker-compose.local.yml up -d

local-build:
@ docker compose -f ./docker-compose.local.yml up -d --build

down-local:
docker stop $(FILES)
docker rm $(FILES)
@ docker stop $(FILES)
@ docker rm $(FILES)

docker-clean:
docker system prune -f
@ docker system prune -f

logs-local:
docker logs -f $(FILES)
@ docker logs -f $(FILES)

migrate-force:
migrate -source file://migrations -database $(DB_LOCAL) force 1
@ migrate -source file://migrations -database $(DB_LOCAL) force 1

migrate-version:
migrate -source file://migrations -database $(DB_LOCAL) version
Expand Down
62 changes: 62 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"meta": {
"project_name": "PowerSSO",
"project_url": "http://localhost:3000"
},
"server": {
"version": "1.0.0",
"port": ":5000",
"pprof_port": ":5555",
"mode": "dev",
"jwt_secret_key": "power-sso-secret",
"cookie_name": "jwt-power-sso",
"ssl": false,
"ctx_default_timeout": 12,
"read_timeout": 5,
"write_timeout": 5,
"srf": true,
"debug": false,
"start_http": true,
"start_grpc": true,
"access_log_directory": "/var/log/powersso/access.log",
"error_log_directory": "/var/log/powersso/error.log",
"permission_base": "github.com/isaqueveras/power-sso"
},
"database": {
"host": "postgesql",
"port": 5432,
"user": "postgres",
"password": "postgres",
"dbname": "power-sso",
"sslmode": false,
"driver": "pgx",
"max_open_conns": 60,
"max_idle_conns": 30,
"conn_max_life_time": 120,
"conn_max_idle_time": 20,
"timeout": 2
},
"logger": {
"development": true,
"disable_caller": false,
"disable_stacktrace": false,
"encoding": "json",
"level": "info"
},
"mailer": {
"host": "localhost",
"port": 1025,
"email": "[email protected]",
"username": "PowerSSO",
"password": "password",
"tls": false
},
"user_auth_token": {
"secret_key": "kjnfdjksdbfsdhfbdskjfnamkndkn",
"duration": 2592000
}
}




65 changes: 0 additions & 65 deletions config/config-dev.yml

This file was deleted.

65 changes: 0 additions & 65 deletions config/config-prod.yml

This file was deleted.

35 changes: 10 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,28 @@
package config

import (
"encoding/json"
"io/ioutil"
"log"
"os"

"github.com/spf13/viper"
)

var config *Config

// LoadConfig config file from given path
func LoadConfig(path ...string) {
v := viper.New()

if path == nil {
path[0] = "."
func LoadConfig() {
path := "../app.json"
if val, set := os.LookupEnv("CONFIG_POWER_SSO"); set && val != "" {
path = val
}

v.SetConfigName(getConfigPath(os.Getenv("CONFIG_POWER_SSO")))
v.AddConfigPath(path[0])
v.AutomaticEnv()

if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Fatal("Config file not found")
}
raw, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}

if err := v.Unmarshal(&config); err != nil {
log.Fatalf("unable to decode into struct, %v", err)
if err = json.Unmarshal(raw, &config); err != nil {
log.Fatal(err)
}
}

Expand All @@ -45,11 +38,3 @@ func Get() *Config {
}
return config
}

// Get config path for dev or production
func getConfigPath(configPath string) string {
if configPath == modeProduction {
return "./config/config-prod"
}
return "./config/config-dev"
}
Loading