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

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.Task(server.Group("/api/task"), db)
  42. api.Log(server.Group("/api/log"), db)
  43. api.Goal(server.Group("/api/goal"), db)
  44. exitSignal := make(chan os.Signal)
  45. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  46. errCh := make(chan error)
  47. go func() {
  48. err := server.Run(serverListen)
  49. if err != nil {
  50. errCh <- err
  51. }
  52. }()
  53. select {
  54. case sig := <-exitSignal:
  55. {
  56. log.Println("Received signal", sig)
  57. os.Exit(0)
  58. }
  59. case err := <-errCh:
  60. {
  61. log.Println("Server run failed:", err)
  62. os.Exit(2)
  63. }
  64. }
  65. }