Skip to content

Commit

Permalink
chore: Refactor Makefile to remove unnecessary build step
Browse files Browse the repository at this point in the history
  • Loading branch information
vaayne committed Jul 23, 2024
1 parent fc9ed13 commit 1319c39
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ generate:
@go generate ./...
@go-bindata -prefix "database/migrations/" -pkg migrations -o database/bindata.go database/migrations/

build: generate lint
build: lint
@echo "Building..."
@go build -o bin/app main.go

test: lint
@echo "Testing..."
@go test ./...

run: build
run: build db-up
@echo "Running..."
@./bin/app

ngrok:
@echo "Running ngrok..."
@ngrok http 1323

db-up:
@echo "Starting database..."
@docker compose up -d postgres

docker-build:
@echo "Building with docker"
@docker compose --build
Expand Down
4 changes: 4 additions & 0 deletions database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func Migrate(ctx context.Context, databaseURL string) {
logger.Default.Fatal("Error while creating migrate instance", err, "err")
}
if err := m.Up(); err != nil {
if err.Error() == "no change" {
logger.Default.Info("No migration needed")
return
}
logger.Default.Fatal("Error while migrating", err, "err")
}
logger.Default.Info("Migration successful")
Expand Down
29 changes: 24 additions & 5 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"vibrain/internal/pkg/logger"

Expand All @@ -24,17 +25,35 @@ type ServiceConfig struct {
Port int `env:"PORT" envDefault:"1323"`
}

type TelegramConfig struct {
Token string `env:"TOKEN,required"`
Name string `env:"NAME"`
Description string `env:"DESCRIPTION"`
Webhook bool `env:"WEBHOOK" envDefault:"false"`
}

type DatabaseConfig struct {
Driver string `env:"DRIVER" envDefault:"postgres"`
Host string `env:"HOST" envDefault:"localhost"`
Port int `env:"PORT" envDefault:"5432"`
User string `env:"USER" envDefault:"postgres"`
Password string `env:"PASSWORD" envDefault:"postgres"`
Database string `env:"DATABASE" envDefault:"postgres"`
}

func (db DatabaseConfig) URL() string {
return fmt.Sprintf("%s://%s:%s@%s:%d/%s?sslmode=disable", db.Driver, db.User, db.Password, db.Host, db.Port, db.Database)
}

type Config struct {
Debug bool `env:"DEBUG" envDefault:"false"`
Service ServiceConfig `envPrefix:"SERVICE_"`

DatabaseURL string `env:"DATABASE_URL,required"`
QueueDatabaseURL string `env:"QUEUE_DATABASE_URL,expand" envDefault:"${DATABASE_URL}"`

TelegramToken string `env:"TELEGRAM_TOKEN"`
Database DatabaseConfig `envPrefix:"DATABASE_"`
Telegram TelegramConfig `envPrefix:"TELEGRAM_"`

JWTSecret string `env:"JWT_SECRET,required"`
OAuths []OAuthConfig `envPrefix:"OAUTH"`
OAuths []OAuthConfig `envPrefix:"OAUTH_"`
}

func init() {
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()

migrations.Migrate(ctx, config.Settings.DatabaseURL)
migrations.Migrate(ctx, config.Settings.Database.URL())

logger.Default.Info("starting service")

services := make([]Service, 0)

// init basic services
// init db pool
pool, err := db.NewPool(ctx, config.Settings.DatabaseURL)
pool, err := db.NewPool(ctx, config.Settings.Database.URL())
if err != nil {
logger.Default.Fatal("failed to create new database pool", "error", err)
}
Expand All @@ -45,8 +45,8 @@ func main() {
cacheService := cache.NewDBCache(pool)

// start services
if config.Settings.TelegramToken != "" {
botService, err := bots.NewServer(config.Settings.TelegramToken, pool, botsHandlers.WithCache(cacheService))
if config.Settings.Telegram.Token != "" {
botService, err := bots.NewServer(config.Settings.Telegram.Token, pool, botsHandlers.WithCache(cacheService))
if err != nil {
logger.Default.Fatal("failed to create new bot service", "error", err)
}
Expand Down

0 comments on commit 1319c39

Please sign in to comment.