2025-02-22 12:50:26 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-02-23 22:34:26 +08:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2025-02-27 22:32:38 +08:00
|
|
|
"os"
|
2025-02-23 22:34:26 +08:00
|
|
|
|
2025-02-27 07:25:25 +08:00
|
|
|
"legalgo-BE-go/config"
|
|
|
|
|
"legalgo-BE-go/database"
|
|
|
|
|
repository "legalgo-BE-go/internal/accessor"
|
|
|
|
|
internalhttp "legalgo-BE-go/internal/api/http"
|
|
|
|
|
pkgconfig "legalgo-BE-go/internal/config"
|
|
|
|
|
"legalgo-BE-go/internal/services"
|
|
|
|
|
|
2025-02-23 13:04:30 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2025-02-23 22:34:26 +08:00
|
|
|
"github.com/joho/godotenv"
|
2025-02-22 12:50:26 +08:00
|
|
|
"go.uber.org/fx"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-23 22:34:26 +08:00
|
|
|
func init() {
|
2025-02-27 22:32:38 +08:00
|
|
|
// Check if the .env file exists
|
|
|
|
|
if _, err := os.Stat(".env"); err == nil {
|
|
|
|
|
// Load environment variables from .env file if it exists
|
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
|
log.Println("Error loading .env file, continuing without it")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.Println(".env file not found, skipping load")
|
2025-02-23 22:34:26 +08:00
|
|
|
}
|
2025-02-26 22:28:19 +08:00
|
|
|
|
|
|
|
|
config.InitEnv()
|
2025-02-23 22:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run(lc fx.Lifecycle, db *database.DB, apiRouter chi.Router) {
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
|
fmt.Println("Application has started...")
|
|
|
|
|
pkgconfig.Router(apiRouter)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
|
fmt.Println("Shutting down...")
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-22 12:50:26 +08:00
|
|
|
func main() {
|
2025-02-23 22:34:26 +08:00
|
|
|
app := fx.New(
|
2025-03-03 18:59:25 +07:00
|
|
|
config.Module,
|
2025-02-23 22:34:26 +08:00
|
|
|
database.Module,
|
|
|
|
|
repository.Module,
|
|
|
|
|
services.Module,
|
2025-03-03 18:59:25 +07:00
|
|
|
internalhttp.Module,
|
2025-02-23 22:34:26 +08:00
|
|
|
fx.Invoke(run),
|
2025-02-22 16:15:38 +08:00
|
|
|
)
|
2025-02-23 22:34:26 +08:00
|
|
|
|
|
|
|
|
if err := app.Start(context.Background()); err != nil {
|
|
|
|
|
log.Fatal("Error starting app:", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for the app to stop gracefully
|
|
|
|
|
if err := app.Stop(context.Background()); err != nil {
|
|
|
|
|
log.Fatal("Error stopping app:", err)
|
|
|
|
|
}
|
2025-02-22 12:50:26 +08:00
|
|
|
}
|