Skip to content
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
8 changes: 0 additions & 8 deletions build/package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ FROM golang:1.20-alpine AS build
ARG APP
WORKDIR /go/src

RUN go install github.com/pressly/goose/v3/cmd/goose@latest

# Copy go.mod and go.sum
COPY go.* ./

Expand Down Expand Up @@ -36,17 +34,11 @@ FROM alpine:3 as prod

WORKDIR /app

ENV GOOSE_DRIVER=mysql
ENV GOOSE_MIGRATION_DIR=/app/migrations

RUN apk add --no-cache tzdata

COPY --from=build /go/bin/goose /app/goose

COPY scripts/docker-entrypoint.sh /docker-entrypoint.sh

COPY --from=mkdocs /docs/site /app/static
COPY db/migrations /app/migrations
COPY --from=build /go/src/app /app

# Exposes port 3000 because our program listens on that port
Expand Down
1 change: 1 addition & 0 deletions db/migrations
26 changes: 14 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ require (
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/nyaruka/phonenumbers v1.1.8
github.com/pressly/goose/v3 v3.16.0
github.com/valyala/fasthttp v1.50.0
go.uber.org/fx v1.20.1
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.15.0
google.golang.org/api v0.148.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.2
Expand All @@ -25,11 +26,11 @@ require (

require (
cloud.google.com/go v0.110.9 // indirect
cloud.google.com/go/compute v1.23.1 // indirect
cloud.google.com/go/compute v1.23.2 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/firestore v1.14.0 // indirect
cloud.google.com/go/iam v1.1.3 // indirect
cloud.google.com/go/longrunning v0.5.2 // indirect
cloud.google.com/go/iam v1.1.4 // indirect
cloud.google.com/go/longrunning v0.5.3 // indirect
cloud.google.com/go/storage v1.33.0 // indirect
github.com/MicahParks/keyfunc v1.9.0 // indirect
github.com/andybalholm/brotli v1.0.6 // indirect
Expand All @@ -40,7 +41,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
Expand All @@ -52,25 +53,26 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/appengine/v2 v2.0.5 // indirect
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
114 changes: 90 additions & 24 deletions go.sum

Large diffs are not rendered by default.

36 changes: 2 additions & 34 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package config

import (
"errors"
"os"

"github.com/joho/godotenv"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)

type Config struct {
HTTP HTTP `yaml:"http"`
Database Database `yaml:"database"`
Expand All @@ -20,6 +11,7 @@ type HTTP struct {
}

type Database struct {
Dialect string `yaml:"dialect" envconfig:"DATABASE__DIALECT"`
Host string `yaml:"host" envconfig:"DATABASE__HOST"`
Port int `yaml:"port" envconfig:"DATABASE__PORT"`
User string `yaml:"user" envconfig:"DATABASE__USER"`
Expand All @@ -37,6 +29,7 @@ var defaultConfig = Config{
Listen: ":3000",
},
Database: Database{
Dialect: "mysql",
Host: "localhost",
Port: 3306,
User: "sms",
Expand All @@ -48,28 +41,3 @@ var defaultConfig = Config{
CredentialsJSON: "",
},
}

func GetConfig(logger *zap.Logger) Config {
err := godotenv.Load()
if err != nil && !errors.Is(err, os.ErrNotExist) {
logger.Error("Error loading .env file", zap.Error(err))
}

configPath := "config.yml"
if envPath := os.Getenv("CONFIG_PATH"); envPath != "" {
configPath = envPath
}

yamlFile, err := os.ReadFile(configPath)
if err != nil {
logger.Error("Error reading config file", zap.Error(err))
}

var config Config
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
logger.Error("Error unmarshalling config file", zap.Error(err))
}

return config
}
1 change: 1 addition & 0 deletions internal/config/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var Module = fx.Module(
}),
fx.Provide(func(cfg Config) db.Config {
return db.Config{
Dialect: cfg.Database.Dialect,
Host: cfg.Database.Host,
Port: cfg.Database.Port,
User: cfg.Database.User,
Expand Down
2 changes: 1 addition & 1 deletion internal/infra/db/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewCommandMigrate(params CommandMigrateParams) *CommandMigrate {
}

func (c *CommandMigrate) Cmd() string {
return "db:migrate"
return "db:auto-migrate"
}

func (c *CommandMigrate) Run(args ...string) error {
Expand Down
5 changes: 5 additions & 0 deletions internal/infra/db/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

var ConfigDefault = Config{
Dialect: "mysql",
Host: "localhost",
Port: 3306,
User: "root",
Expand All @@ -9,6 +10,7 @@ var ConfigDefault = Config{
}

type Config struct {
Dialect string
Host string
Port int
User string
Expand All @@ -20,6 +22,9 @@ type Config struct {
// Helper function to set default values
func configDefault(config Config) Config {
// Override default config
if config.Dialect == "" {
config.Dialect = ConfigDefault.Dialect
}
if config.Host == "" {
config.Host = ConfigDefault.Host
}
Expand Down
18 changes: 1 addition & 17 deletions internal/infra/db/db.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package db

import (
"fmt"
"log"
"net/url"

sql "github.com/go-sql-driver/mysql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"moul.io/zapgorm2"
)

func New(params Params) (*gorm.DB, error) {
dsn := makeDSN(params.Config)
cfgGorm := makeConfig(params)

sql.SetLogger(log.Default())

return gorm.Open(mysql.Open(dsn), cfgGorm)
return gorm.Open(mysql.New(mysql.Config{Conn: params.SQL}), cfgGorm)
}

func makeConfig(params Params) *gorm.Config {
Expand All @@ -30,11 +22,3 @@ func makeConfig(params Params) *gorm.Config {
Logger: log,
}
}

func makeDSN(cfg Config) string {
cfg = configDefault(cfg)
return fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4,utf8&parseTime=true&loc=%s&tls=preferred",
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database, url.QueryEscape(cfg.Timezone),
)
}
77 changes: 77 additions & 0 deletions internal/infra/db/goose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package db

import (
"database/sql"
"io/fs"

"github.com/pressly/goose/v3"
"go.uber.org/fx"
"go.uber.org/zap"
)

type GooseStorage struct {
FS fs.FS
}

type GooseMigrateParams struct {
fx.In

Config Config
Storage GooseStorage

Logger *zap.Logger
DB *sql.DB
Shut fx.Shutdowner
}

type GooseMigrate struct {
Config Config
Storage GooseStorage
DB *sql.DB
Logger *zap.Logger
Shut fx.Shutdowner
}

func NewGooseMigrate(params GooseMigrateParams) *GooseMigrate {
return &GooseMigrate{
Config: params.Config,
Logger: params.Logger,
DB: params.DB,
Storage: params.Storage,
Shut: params.Shut,
}
}

func (c *GooseMigrate) Cmd() string {
return "db:migrate"
}

func (c *GooseMigrate) Run(args ...string) error {
goose.SetBaseFS(c.Storage.FS)

cmd := "up"
if len(args) > 0 {
cmd = args[0]
}

if err := goose.SetDialect(c.Config.Dialect); err != nil {
return err
}

migrationsPath := "migrations/" + c.Config.Dialect

switch cmd {
case "up":
if err := goose.Up(c.DB, migrationsPath); err != nil {
return err
}
case "down":
if err := goose.Down(c.DB, migrationsPath); err != nil {
return err
}
}

c.Logger.Info("Migrations completed")

return c.Shut.Shutdown()
}
2 changes: 2 additions & 0 deletions internal/infra/db/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var Module = fx.Module(
}),
fx.Provide(
New,
NewSQL,
cli.AsCommand(NewCommandMigrate),
cli.AsCommand(NewGooseMigrate),
),
)
6 changes: 4 additions & 2 deletions internal/infra/db/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import (
"database/sql"

"go.uber.org/fx"
"go.uber.org/zap"
)
Expand All @@ -10,6 +12,6 @@ type Params struct {

Logger *zap.Logger
Config Config
// Migrations []Migrator `group:"migrations"`
LC fx.Lifecycle
SQL *sql.DB
LC fx.Lifecycle
}
35 changes: 35 additions & 0 deletions internal/infra/db/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package db

import (
"database/sql"
"fmt"
"log"
"net/url"
"time"

mysql "github.com/go-sql-driver/mysql"
)

func NewSQL(config Config) (*sql.DB, error) {
dsn := makeDSN(config)

mysql.SetLogger(log.Default())
db, err := sql.Open(config.Dialect, dsn)
if err != nil {
return nil, err
}

db.SetConnMaxLifetime(3 * time.Minute)
db.SetMaxOpenConns(16)
db.SetMaxIdleConns(16)

return db, nil
}

func makeDSN(cfg Config) string {
cfg = configDefault(cfg)
return fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4,utf8&parseTime=true&loc=%s&tls=preferred",
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database, url.QueryEscape(cfg.Timezone),
)
}
2 changes: 1 addition & 1 deletion internal/sms-gateway/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

var Module = fx.Module(
"server",
logger.Module,
cli.Module,
appconfig.Module,
logger.Module,
http.Module,
validator.Module,
handlers.Module,
Expand Down
Loading