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: implement donut as a service #1

Merged
merged 11 commits into from
Dec 10, 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
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DATABASE_HOST="localhost"
DATABASE_PORT="5432"
DATABASE_USERNAME="postgres"
DATABASE_PASSWORD="postgres"
DATABASE_SCHEMA="donut"
DATABASE_DEBUG=TRUE
DATABASE_DIALECT="postgres"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
postgres-data/

# Ignore .env files as they are usually used to configure the app
*.env
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": "0.2.0",
"configurations": [
// Use this for production build
{
"name": "Launch - Production",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["."],
"stackTraceDepth": 100,
"console": "integratedTerminal",
"showLog": true,
"internalConsoleOptions": "openOnFirstSessionStart",
"showGlobalVariables": true,
"envFile": "${workspaceFolder}/.production.env"
},

// Use this for development build
{
"name": "Launch - Development",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["."],
"stackTraceDepth": 100,
"console": "integratedTerminal",
"showLog": true,
"internalConsoleOptions": "openOnFirstSessionStart",
"showGlobalVariables": true,
"envFile": "${workspaceFolder}/.development.env"
}
]
}
14 changes: 14 additions & 0 deletions application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

type ApplicationConfig struct {
Host string `env:"APPLICATION_HOST" envDefault:"localhost"`
Port int `env:"APPLICATION_PORT" envDefault:"8080"`
TZ string `env:"APPLICATION_TZ" envDefault:"Asia/Jakarta"`
GracefulShutdownTimeout int `env:"APPLICATION_GRACEFUL_SHUTDOWN_TIMEOUT" envDefault:"10"`
}

func (cfg ApplicationConfig) Address() string {
return fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
}
16 changes: 16 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"github.com/caarlos0/env/v6"
)

type Config struct {
ApplicationConfig ApplicationConfig
DatabaseConfig DatabaseConfig
}

func Get() (*Config, error) {
cfg := Config{}
err := env.Parse(&cfg)
return &cfg, err
}
67 changes: 67 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"fmt"

"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

type DatabaseDialect string

const (
DialectMySQL DatabaseDialect = "mysql"
DialectPostgres DatabaseDialect = "postgres"
)

type DatabaseConfig struct {
Host string `env:"DATABASE_HOST"`
Port int `env:"DATABASE_PORT" envDefault:"3306"`
Username string `env:"DATABASE_USERNAME" envDefault:"root"`
Password string `env:"DATABASE_PASSWORD"`
Schema string `env:"DATABASE_SCHEMA"`
Debug bool `env:"DATABASE_DEBUG" envDefault:"false"`
LogLevel string `env:"DATABASE_LOG_LEVEL" envDefault:"info" enum:"silent,error,warn,info"`
Dialect string `env:"DATABASE_DIALECT"`
}

func (d DatabaseConfig) GetDialector() (gorm.Dialector, error) {
switch d.Dialect {
case string(DialectMySQL):
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
d.Username,
d.Password,
d.Host,
d.Port,
d.Schema,
)
return mysql.Open(dsn), nil
case string(DialectPostgres):
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Jakarta",
d.Host,
d.Username,
d.Password,
d.Schema,
d.Port,
)
return postgres.Open(dsn), nil
default:
return nil, fmt.Errorf("unsupported database dialect: %s", d.Dialect)
}
}

func (d DatabaseConfig) GetLogLevel() logger.LogLevel {
switch d.LogLevel {
case "error":
return logger.Error
case "warn":
return logger.Warn
case "info":
return logger.Info
default:
return logger.Silent
}
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.3'

services:
db:
image: postgres:9.6
container_name: donut
restart: always
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
Loading