rest-api-prototype/cmd/server/main.go

52 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"context"
"net"
"os"
"os/signal"
"time"
"git.pbiernat.dev/golang/rest-api-prototype/internal/app"
2022-03-11 18:08:03 +01:00
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/config"
2022-06-19 14:45:38 +02:00
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/database"
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler"
)
2022-03-11 18:08:03 +01:00
const (
defHttpIp = "127.0.0.1"
defHttpPort = "8080"
2022-06-18 13:51:37 +02:00
defDbUrl = "postgres://postgres:postgres@127.0.0.1:5432/Api" // FIXME: use env
)
func main() {
2022-03-11 18:08:03 +01:00
if config.ErrLoadingEnvs != nil {
app.Panicf("Error loading .env file")
}
2022-03-11 18:08:03 +01:00
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), config.GetEnv("SERVER_PORT", defHttpPort))
2022-06-18 13:51:37 +02:00
dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
2022-06-19 14:45:38 +02:00
//fmt.Println(dbConnStr)
//os.Exit(1)
2022-06-18 13:51:37 +02:00
dbc, err := database.Connect(dbConnStr)
if err != nil {
app.Panicf("Unable to connect to database: %v\n", err)
2022-06-18 13:51:37 +02:00
}
2022-06-18 13:51:37 +02:00
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)
}