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.

76 lines
1.7 KiB

4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "context"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gissleh/stufflog/api"
  6. "github.com/gissleh/stufflog/database"
  7. "github.com/gissleh/stufflog/internal/auth"
  8. "log"
  9. "os"
  10. "os/signal"
  11. "syscall"
  12. )
  13. func env(key string, defaultValue string) string {
  14. e := os.Getenv(key)
  15. if e == "" {
  16. return defaultValue
  17. }
  18. return e
  19. }
  20. func main() {
  21. dbDriver := env("DB_DRIVER", "postgres")
  22. dbConnect := env("DB_CONNECT", "host=localhost user=stufflog2 password=stufflog2_dev_password dbname=stufflog2 sslmode=disable")
  23. serverListen := env("SERVER_LISTEN", ":8000")
  24. useDummyUuid := env("USE_DUMMY_UUID", "no")
  25. dummyUuid := env("DUMMY_UUID", "9d3214f1-6321-403e-ab87-764ad1a1252d")
  26. db, err := database.Open(context.Background(), dbDriver, dbConnect)
  27. if err != nil {
  28. log.Println("Failed to open database:", err)
  29. os.Exit(1)
  30. }
  31. server := gin.New()
  32. if useDummyUuid == "yes" {
  33. log.Println("Using dummy UUID")
  34. server.Use(auth.DummyMiddleware(dummyUuid))
  35. } else {
  36. server.Use(auth.TrustingJwtParserMiddleware())
  37. }
  38. api.Group(server.Group("/api/group"), db)
  39. api.Item(server.Group("/api/item"), db)
  40. api.Project(server.Group("/api/project"), db)
  41. api.ProjectGroup(server.Group("/api/projectgroup"), db)
  42. api.Task(server.Group("/api/task"), db)
  43. api.Log(server.Group("/api/log"), db)
  44. api.Goal(server.Group("/api/goal"), db)
  45. exitSignal := make(chan os.Signal)
  46. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  47. errCh := make(chan error)
  48. go func() {
  49. err := server.Run(serverListen)
  50. if err != nil {
  51. errCh <- err
  52. }
  53. }()
  54. select {
  55. case sig := <-exitSignal:
  56. {
  57. log.Println("Received signal", sig)
  58. os.Exit(0)
  59. }
  60. case err := <-errCh:
  61. {
  62. log.Println("Server run failed:", err)
  63. os.Exit(2)
  64. }
  65. }
  66. }