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.
62 lines
1.4 KiB
62 lines
1.4 KiB
package main
|
|
|
|
import (
|
|
"git.aiterp.net/stufflog3/stufflog3-api/api"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/auth"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/database/mysql"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/envvars"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
func main() {
|
|
db, err := mysql.Connect(
|
|
envvars.String("STUFFLOG3_MYSQL_HOST"),
|
|
envvars.Int("STUFFLOG3_MYSQL_PORT"),
|
|
envvars.String("STUFFLOG3_MYSQL_USERNAME"),
|
|
envvars.String("STUFFLOG3_MYSQL_PASSWORD"),
|
|
envvars.String("STUFFLOG3_MYSQL_SCHEMA"),
|
|
)
|
|
if err != nil {
|
|
log.Println("Failed to open database:", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
server := gin.New()
|
|
if envvars.String("STUFFLOG3_USE_DUMMY_USER") != "" {
|
|
log.Println("Using dummy UUID")
|
|
server.Use(auth.DummyMiddleware("c11230be-4912-4313-83b0-410a248b5bd1"))
|
|
} else {
|
|
server.Use(auth.TrustingJwtParserMiddleware())
|
|
}
|
|
|
|
api.Scopes(server.Group("/api/scopes"), db)
|
|
api.Items(server.Group("/api/scope/:scope_id/items"), db)
|
|
|
|
exitSignal := make(chan os.Signal)
|
|
signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
errCh := make(chan error)
|
|
go func() {
|
|
err := server.Run(":8239")
|
|
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)
|
|
}
|
|
}
|
|
}
|