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.

74 lines
1.6 KiB

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