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.

64 lines
1.5 KiB

3 years ago
  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. api.Items(server.Group("/api/scope/:scope_id/items"), db)
  34. api.Projects(server.Group("/api/scope/:scope_id/projects"), db)
  35. api.Stats(server.Group("/api/scope/:scope_id/stats"), db)
  36. exitSignal := make(chan os.Signal)
  37. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  38. errCh := make(chan error)
  39. go func() {
  40. err := server.Run(":8239")
  41. if err != nil {
  42. errCh <- err
  43. }
  44. }()
  45. select {
  46. case sig := <-exitSignal:
  47. {
  48. log.Println("Received signal", sig)
  49. os.Exit(0)
  50. }
  51. case err := <-errCh:
  52. {
  53. log.Println("Server run failed:", err)
  54. os.Exit(2)
  55. }
  56. }
  57. }