2024-02-01 22:09:09 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-02-02 14:39:13 +00:00
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
migrate "github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
|
|
|
"github.com/mentos1386/golang-rest-example/pkg/config"
|
2024-02-01 22:09:09 +00:00
|
|
|
"github.com/mentos1386/golang-rest-example/pkg/openapi"
|
2024-02-02 14:39:13 +00:00
|
|
|
"go.uber.org/zap"
|
2024-02-01 22:09:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ApiService struct {
|
2024-02-02 14:39:13 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
db *sql.DB
|
2024-02-01 22:09:09 +00:00
|
|
|
|
2024-02-02 14:39:13 +00:00
|
|
|
Config *config.Config
|
|
|
|
|
2024-02-01 22:09:09 +00:00
|
|
|
openapi.UnimplementedHandler
|
|
|
|
}
|
2024-02-02 14:39:13 +00:00
|
|
|
|
|
|
|
func NewApiService() *ApiService {
|
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
|
|
|
|
config, err := config.NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("Failed to load the config", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", config.DatabaseURL)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("Failed to connect to the database", zap.Error(err))
|
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(10)
|
|
|
|
db.SetMaxIdleConns(10)
|
|
|
|
db.SetConnMaxLifetime(time.Hour)
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("Failed to ping the database", zap.Error(err))
|
|
|
|
}
|
|
|
|
logger.Info("Connected to the database")
|
|
|
|
|
|
|
|
driver, err := postgres.WithInstance(db, &postgres.Config{})
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("Failed to create the driver", zap.Error(err))
|
|
|
|
}
|
|
|
|
m, err := migrate.NewWithDatabaseInstance(
|
2024-02-06 22:34:17 +00:00
|
|
|
config.DatabaseMigrations,
|
2024-02-02 14:39:13 +00:00
|
|
|
"postgres", driver)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("Failed to create the migration", zap.Error(err))
|
|
|
|
}
|
|
|
|
err = m.Up()
|
|
|
|
if err != nil && err != migrate.ErrNoChange {
|
|
|
|
logger.Fatal("Failed to apply the migrations", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("Migrations applied")
|
|
|
|
|
|
|
|
return &ApiService{
|
|
|
|
logger: logger,
|
|
|
|
db: db,
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
}
|