Loggest thy stuff
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.

61 lines
1.3 KiB

  1. package main
  2. import (
  3. "git.aiterp.net/stufflog3/stufflog3-api/api"
  4. "git.aiterp.net/stufflog3/stufflog3-api/internal/auth"
  5. "git.aiterp.net/stufflog3/stufflog3-api/internal/database/mysql"
  6. "git.aiterp.net/stufflog3/stufflog3-api/internal/envvars"
  7. "github.com/gin-gonic/gin"
  8. "log"
  9. "os"
  10. "os/signal"
  11. "syscall"
  12. )
  13. func main() {
  14. db, err := mysql.Connect(
  15. envvars.String("STUFFLOG3_MYSQL_HOST"),
  16. envvars.Int("STUFFLOG3_MYSQL_PORT"),
  17. envvars.String("STUFFLOG3_MYSQL_USERNAME"),
  18. envvars.String("STUFFLOG3_MYSQL_PASSWORD"),
  19. envvars.String("STUFFLOG3_MYSQL_SCHEMA"),
  20. )
  21. if err != nil {
  22. log.Println("Failed to open database:", err)
  23. os.Exit(1)
  24. }
  25. server := gin.New()
  26. if envvars.String("STUFFLOG3_USE_DUMMY_USER") != "" {
  27. log.Println("Using dummy UUID")
  28. server.Use(auth.DummyMiddleware("c11230be-4912-4313-83b0-410a248b5bd1"))
  29. } else {
  30. server.Use(auth.TrustingJwtParserMiddleware())
  31. }
  32. api.Scopes(server.Group("/api/scopes"), db)
  33. exitSignal := make(chan os.Signal)
  34. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  35. errCh := make(chan error)
  36. go func() {
  37. err := server.Run(":8239")
  38. if err != nil {
  39. errCh <- err
  40. }
  41. }()
  42. select {
  43. case sig := <-exitSignal:
  44. {
  45. log.Println("Received signal", sig)
  46. os.Exit(0)
  47. }
  48. case err := <-errCh:
  49. {
  50. log.Println("Server run failed:", err)
  51. os.Exit(2)
  52. }
  53. }
  54. }