Skip to content

Commit 57e3778

Browse files
feat: implement donut as a service (#1)
* feat: setup to database * feat: implement repo * feat: add example test pairing * feat: enhance pair & start behaviour * feat: finish call behaviour * chore: rename method * feat: implement grpc handler * Update user pair serial Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update trx commit error return Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * refactor: change from coderabbit review * refactor: review batch size for get users --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 8f58810 commit 57e3778

18 files changed

+1409
-257
lines changed

.env.example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DATABASE_HOST="localhost"
2+
DATABASE_PORT="5432"
3+
DATABASE_USERNAME="postgres"
4+
DATABASE_PASSWORD="postgres"
5+
DATABASE_SCHEMA="donut"
6+
DATABASE_DEBUG=TRUE
7+
DATABASE_DIALECT="postgres"

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
postgres-data/
2+
3+
# Ignore .env files as they are usually used to configure the app
4+
*.env

.vscode/launch.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
// Use this for production build
5+
{
6+
"name": "Launch - Production",
7+
"type": "go",
8+
"request": "launch",
9+
"mode": "auto",
10+
"program": "${workspaceFolder}",
11+
"args": ["."],
12+
"stackTraceDepth": 100,
13+
"console": "integratedTerminal",
14+
"showLog": true,
15+
"internalConsoleOptions": "openOnFirstSessionStart",
16+
"showGlobalVariables": true,
17+
"envFile": "${workspaceFolder}/.production.env"
18+
},
19+
20+
// Use this for development build
21+
{
22+
"name": "Launch - Development",
23+
"type": "go",
24+
"request": "launch",
25+
"mode": "auto",
26+
"program": "${workspaceFolder}",
27+
"args": ["."],
28+
"stackTraceDepth": 100,
29+
"console": "integratedTerminal",
30+
"showLog": true,
31+
"internalConsoleOptions": "openOnFirstSessionStart",
32+
"showGlobalVariables": true,
33+
"envFile": "${workspaceFolder}/.development.env"
34+
}
35+
]
36+
}

application.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type ApplicationConfig struct {
6+
Host string `env:"APPLICATION_HOST" envDefault:"localhost"`
7+
Port int `env:"APPLICATION_PORT" envDefault:"8080"`
8+
TZ string `env:"APPLICATION_TZ" envDefault:"Asia/Jakarta"`
9+
GracefulShutdownTimeout int `env:"APPLICATION_GRACEFUL_SHUTDOWN_TIMEOUT" envDefault:"10"`
10+
}
11+
12+
func (cfg ApplicationConfig) Address() string {
13+
return fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
14+
}

config.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"github.com/caarlos0/env/v6"
5+
)
6+
7+
type Config struct {
8+
ApplicationConfig ApplicationConfig
9+
DatabaseConfig DatabaseConfig
10+
}
11+
12+
func Get() (*Config, error) {
13+
cfg := Config{}
14+
err := env.Parse(&cfg)
15+
return &cfg, err
16+
}

database.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"gorm.io/driver/mysql"
7+
"gorm.io/driver/postgres"
8+
"gorm.io/gorm"
9+
"gorm.io/gorm/logger"
10+
)
11+
12+
type DatabaseDialect string
13+
14+
const (
15+
DialectMySQL DatabaseDialect = "mysql"
16+
DialectPostgres DatabaseDialect = "postgres"
17+
)
18+
19+
type DatabaseConfig struct {
20+
Host string `env:"DATABASE_HOST"`
21+
Port int `env:"DATABASE_PORT" envDefault:"3306"`
22+
Username string `env:"DATABASE_USERNAME" envDefault:"root"`
23+
Password string `env:"DATABASE_PASSWORD"`
24+
Schema string `env:"DATABASE_SCHEMA"`
25+
Debug bool `env:"DATABASE_DEBUG" envDefault:"false"`
26+
LogLevel string `env:"DATABASE_LOG_LEVEL" envDefault:"info" enum:"silent,error,warn,info"`
27+
Dialect string `env:"DATABASE_DIALECT"`
28+
}
29+
30+
func (d DatabaseConfig) GetDialector() (gorm.Dialector, error) {
31+
switch d.Dialect {
32+
case string(DialectMySQL):
33+
dsn := fmt.Sprintf(
34+
"%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
35+
d.Username,
36+
d.Password,
37+
d.Host,
38+
d.Port,
39+
d.Schema,
40+
)
41+
return mysql.Open(dsn), nil
42+
case string(DialectPostgres):
43+
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Jakarta",
44+
d.Host,
45+
d.Username,
46+
d.Password,
47+
d.Schema,
48+
d.Port,
49+
)
50+
return postgres.Open(dsn), nil
51+
default:
52+
return nil, fmt.Errorf("unsupported database dialect: %s", d.Dialect)
53+
}
54+
}
55+
56+
func (d DatabaseConfig) GetLogLevel() logger.LogLevel {
57+
switch d.LogLevel {
58+
case "error":
59+
return logger.Error
60+
case "warn":
61+
return logger.Warn
62+
case "info":
63+
return logger.Info
64+
default:
65+
return logger.Silent
66+
}
67+
}

docker-compose.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.3'
2+
3+
services:
4+
db:
5+
image: postgres:9.6
6+
container_name: donut
7+
restart: always
8+
environment:
9+
POSTGRES_PASSWORD: postgres
10+
POSTGRES_USER: postgres
11+
POSTGRES_DB: postgres
12+
ports:
13+
- 5432:5432

0 commit comments

Comments
 (0)