You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.7 KiB
75 lines
1.7 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gissleh/stufflog/api"
|
|
"github.com/gissleh/stufflog/database"
|
|
"github.com/gissleh/stufflog/internal/auth"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
func env(key string, defaultValue string) string {
|
|
e := os.Getenv(key)
|
|
if e == "" {
|
|
return defaultValue
|
|
}
|
|
|
|
return e
|
|
}
|
|
|
|
func main() {
|
|
dbDriver := env("DB_DRIVER", "postgres")
|
|
dbConnect := env("DB_CONNECT", "host=localhost user=stufflog2 password=stufflog2_dev_password dbname=stufflog2 sslmode=disable")
|
|
serverListen := env("SERVER_LISTEN", ":8000")
|
|
useDummyUuid := env("USE_DUMMY_UUID", "no")
|
|
dummyUuid := env("DUMMY_UUID", "9d3214f1-6321-403e-ab87-764ad1a1252d")
|
|
|
|
db, err := database.Open(context.Background(), dbDriver, dbConnect)
|
|
if err != nil {
|
|
log.Println("Failed to open database:", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
server := gin.New()
|
|
if useDummyUuid == "yes" {
|
|
log.Println("Using dummy UUID")
|
|
server.Use(auth.DummyMiddleware(dummyUuid))
|
|
} else {
|
|
server.Use(auth.TrustingJwtParserMiddleware())
|
|
}
|
|
|
|
api.Group(server.Group("/api/group"), db)
|
|
api.Item(server.Group("/api/item"), db)
|
|
api.Project(server.Group("/api/project"), db)
|
|
api.Task(server.Group("/api/task"), db)
|
|
api.Log(server.Group("/api/log"), db)
|
|
api.Goal(server.Group("/api/goal"), db)
|
|
|
|
exitSignal := make(chan os.Signal)
|
|
signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
errCh := make(chan error)
|
|
go func() {
|
|
err := server.Run(serverListen)
|
|
if err != nil {
|
|
errCh <- err
|
|
}
|
|
}()
|
|
|
|
select {
|
|
case sig := <-exitSignal:
|
|
{
|
|
log.Println("Received signal", sig)
|
|
os.Exit(0)
|
|
}
|
|
case err := <-errCh:
|
|
{
|
|
log.Println("Server run failed:", err)
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
}
|