Commit 603f30f0 by mhmdhaekal

fix(initialize): move config to domain

1 parent 5e441d8e
......@@ -2,12 +2,11 @@ package main
import (
"log"
"os"
"ui.ac.id/dtd/go-webserver-boilerplate/internal/domain"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/healthcheck"
"github.com/gofiber/fiber/v3/middleware/logger"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"ui.ac.id/dtd/go-webserver-boilerplate/internal/handler"
......@@ -15,49 +14,12 @@ import (
"ui.ac.id/dtd/go-webserver-boilerplate/internal/service"
)
type Config struct {
DatabaseUrl string
AppEnvironment string
Port string
}
func initConfig() Config {
environment := os.Getenv("APP_ENVIRONMENT")
if environment == "" {
environment = "production"
}
if environment == "development" {
err := godotenv.Load()
if err != nil {
panic(err)
}
}
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
log.Printf("Environment: %s", environment)
return Config{
DatabaseUrl: os.Getenv("DATABASE_URL"),
AppEnvironment: environment,
Port: port,
}
}
func main() {
config := initConfig()
config := domain.InitConfig()
db, err := gorm.Open(postgres.Open(config.DatabaseUrl), &gorm.Config{})
if err != nil {
log.Fatal(err)
os.Exit(1)
}
userRepository := postgresql.NewPostgresUserRepository(db)
......@@ -82,5 +44,9 @@ func main() {
user.Get("/:id", userHandler.GetUserDetail)
user.Get("/", userHandler.GetUserList)
app.Listen(":" + config.Port)
err = app.Listen(":" + config.Port)
if err != nil {
return
}
}
package domain
import (
"github.com/joho/godotenv"
"log"
"os"
)
type Config struct {
DatabaseUrl string
AppEnvironment string
Port string
}
func InitConfig() Config {
environment := os.Getenv("APP_ENVIRONMENT")
if environment == "" {
environment = "production"
}
if environment == "development" {
err := godotenv.Load()
if err != nil {
panic(err)
}
}
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
log.Printf("Environment: %s", environment)
return Config{
DatabaseUrl: os.Getenv("DATABASE_URL"),
AppEnvironment: environment,
Port: port,
}
}
package handler
import (
"errors"
"github.com/gofiber/fiber/v3"
"ui.ac.id/dtd/go-webserver-boilerplate/internal/domain" // Adjust import path to your project
)
......@@ -34,7 +35,8 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
}
func formatError(err error) ErrorResponse {
if customErr, ok := err.(*domain.Error); ok {
var customErr *domain.Error
if errors.As(err, &customErr) {
errResp := ErrorResponse{
Message: customErr.Message,
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!