identity-service/cmd/main.go

49 lines
1.2 KiB
Go

package main
import (
"context"
"git.pbiernat.dev/egommerce/application/services/identity/internal/app"
"git.pbiernat.dev/egommerce/application/services/identity/internal/app/config"
"git.pbiernat.dev/egommerce/application/services/identity/internal/app/database"
"git.pbiernat.dev/egommerce/application/services/identity/internal/app/handler"
"net"
"os"
"os/signal"
"time"
)
const (
defHttpIp = "127.0.0.1"
defHttpPort = "8080"
defDbUrl = "postgres://postgres:postgres@127.0.0.1:5432/egommerce" // FIXME: use env
)
func main() {
if config.ErrLoadingEnvs != nil {
app.Panicf("Error loading .env file")
}
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), config.GetEnv("SERVER_PORT", defHttpPort))
dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
dbc, err := database.Connect(dbConnStr)
if err != nil {
app.Panicf("Unable to connect to database: %v\n", err)
}
env := &handler.Env{httpAddr, dbc}
srv := app.NewServer(env)
go srv.Start()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
srv.Shutdown(ctx)
os.Exit(0)
}