Move ENV imports to config package
This commit is contained in:
@@ -3,7 +3,6 @@ package database
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
db "zardzul/music-index/sqlc"
|
db "zardzul/music-index/sqlc"
|
||||||
@@ -11,16 +10,11 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Connect() (*pgxpool.Pool, *db.Queries, error) {
|
func Connect(databaseurl string) (*pgxpool.Pool, *db.Queries, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
databaseURL := os.Getenv("DATABASE_URL")
|
pool, err := pgxpool.New(ctx, databaseurl)
|
||||||
if databaseURL == "" {
|
|
||||||
panic("DATABASE_URL environment variable not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
pool, err := pgxpool.New(ctx, databaseURL)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("create pool: %w", err)
|
return nil, nil, fmt.Errorf("create pool: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-27
@@ -1,17 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
"zardzul/music-index/database"
|
"zardzul/music-index/database"
|
||||||
_ "zardzul/music-index/docs"
|
_ "zardzul/music-index/docs"
|
||||||
"zardzul/music-index/handlers"
|
"zardzul/music-index/handlers"
|
||||||
"zardzul/music-index/repository"
|
"zardzul/music-index/repository"
|
||||||
"zardzul/music-index/routes"
|
"zardzul/music-index/routes"
|
||||||
|
"zardzul/music-index/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// @title Music Index API
|
// @title Music Index API
|
||||||
@@ -20,43 +18,25 @@ import (
|
|||||||
// @BasePath /api/v1/
|
// @BasePath /api/v1/
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := godotenv.Load(".env")
|
|
||||||
|
config, err := utils.LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Error loading .env file")
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, queries, databaseError := database.Connect()
|
pool, queries, databaseError := database.Connect(config.DBURL)
|
||||||
if databaseError != nil {
|
if databaseError != nil {
|
||||||
panic(databaseError)
|
panic(databaseError)
|
||||||
}
|
}
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
jwtSecret := os.Getenv("JWT_SECRET")
|
|
||||||
if jwtSecret == "" {
|
|
||||||
panic("JWT_SECRET is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
jwtIssuer := os.Getenv("JWT_ISSUER")
|
|
||||||
if jwtIssuer == "" {
|
|
||||||
jwtIssuer = "music-index-api"
|
|
||||||
}
|
|
||||||
|
|
||||||
jwtTTLMinutes := 60
|
|
||||||
if envTTL := os.Getenv("JWT_TTL_MINUTES"); envTTL != "" {
|
|
||||||
parsed, parseErr := strconv.Atoi(envTTL)
|
|
||||||
if parseErr != nil || parsed <= 0 {
|
|
||||||
panic("JWT_TTL_MINUTES must be a positive integer")
|
|
||||||
}
|
|
||||||
jwtTTLMinutes = parsed
|
|
||||||
}
|
|
||||||
|
|
||||||
userRepo := repository.NewUserRepository(queries)
|
userRepo := repository.NewUserRepository(queries)
|
||||||
userHandler := handlers.NewUserHandler(userRepo, jwtSecret, jwtIssuer, time.Duration(jwtTTLMinutes)*time.Minute)
|
userHandler := handlers.NewUserHandler(userRepo, config.JWTsecret, config.JWTIssuer, time.Duration(config.JWTTTL)*time.Minute)
|
||||||
artistRepo := repository.NewArtistRepository(queries)
|
artistRepo := repository.NewArtistRepository(queries)
|
||||||
artistHandler := handlers.NewArtistHandler(artistRepo)
|
artistHandler := handlers.NewArtistHandler(artistRepo)
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
routes.Routes(router, userHandler, artistHandler, jwtSecret)
|
routes.Routes(router, userHandler, artistHandler, config.JWTsecret)
|
||||||
|
|
||||||
if routerError := router.Run(":8080"); routerError != nil {
|
if routerError := router.Run(":8080"); routerError != nil {
|
||||||
panic(routerError)
|
panic(routerError)
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
JWTsecret string
|
||||||
|
JWTIssuer string
|
||||||
|
JWTTTL int
|
||||||
|
DBURL string
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadConfig() (*Config, error) {
|
||||||
|
Config := new(Config)
|
||||||
|
|
||||||
|
err := godotenv.Load(".env")
|
||||||
|
if err != nil {
|
||||||
|
panic("Error loading .env file")
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.JWTsecret = os.Getenv("JWT_SECRET")
|
||||||
|
if Config.JWTsecret == "" {
|
||||||
|
panic("JWT_SECRET is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.JWTIssuer = os.Getenv("JWT_ISSUER")
|
||||||
|
if Config.JWTIssuer == "" {
|
||||||
|
Config.JWTIssuer = "music-index-api"
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.JWTTTL = 60
|
||||||
|
if envTTL := os.Getenv("JWT_TTL_MINUTES"); envTTL != "" {
|
||||||
|
parsed, parseErr := strconv.Atoi(envTTL)
|
||||||
|
if parseErr != nil || parsed <= 0 {
|
||||||
|
panic("JWT_TTL_MINUTES must be a positive integer")
|
||||||
|
}
|
||||||
|
Config.JWTTTL = parsed
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.DBURL = os.Getenv("DATABASE_URL")
|
||||||
|
if Config.DBURL == "" {
|
||||||
|
panic("DATABASE_URL environment variable not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
return Config, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user