62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/ardeman/project-legalgo-go/database"
|
|
repository "github.com/ardeman/project-legalgo-go/internal/accessor"
|
|
internalhttp "github.com/ardeman/project-legalgo-go/internal/api/http"
|
|
pkgconfig "github.com/ardeman/project-legalgo-go/internal/config"
|
|
"github.com/ardeman/project-legalgo-go/internal/services"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/joho/godotenv"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
func init() {
|
|
if err := godotenv.Load(); err != nil {
|
|
log.Fatal("cannot load environment file")
|
|
}
|
|
}
|
|
|
|
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...")
|
|
// Run migration
|
|
if err := db.Migrate(); err != nil {
|
|
log.Fatal("Migration failed: ", err)
|
|
}
|
|
|
|
pkgconfig.Router(apiRouter)
|
|
|
|
return nil
|
|
},
|
|
OnStop: func(ctx context.Context) error {
|
|
fmt.Println("Shutting down...")
|
|
return nil
|
|
},
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
app := fx.New(
|
|
database.Module,
|
|
repository.Module,
|
|
internalhttp.Module,
|
|
services.Module,
|
|
fx.Invoke(run),
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|