Loggest thine 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.

140 lines
3.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package main
  2. import (
  3. "git.aiterp.net/stufflog3/stufflog3/ports/cognitoauth"
  4. "git.aiterp.net/stufflog3/stufflog3/ports/httpapi"
  5. "git.aiterp.net/stufflog3/stufflog3/ports/mysql"
  6. "git.aiterp.net/stufflog3/stufflog3/usecases/auth"
  7. "git.aiterp.net/stufflog3/stufflog3/usecases/items"
  8. "git.aiterp.net/stufflog3/stufflog3/usecases/projects"
  9. "git.aiterp.net/stufflog3/stufflog3/usecases/scopes"
  10. "git.aiterp.net/stufflog3/stufflog3/usecases/sprints"
  11. "git.aiterp.net/stufflog3/stufflog3/usecases/stats"
  12. "github.com/gin-gonic/gin"
  13. "log"
  14. "os"
  15. "os/signal"
  16. "strconv"
  17. "syscall"
  18. )
  19. func main() {
  20. db, err := mysql.Connect(
  21. os.Getenv("STUFFLOG3_MYSQL_HOST"),
  22. envInt("STUFFLOG3_MYSQL_PORT"),
  23. os.Getenv("STUFFLOG3_MYSQL_USERNAME"),
  24. os.Getenv("STUFFLOG3_MYSQL_PASSWORD"),
  25. os.Getenv("STUFFLOG3_MYSQL_SCHEMA"),
  26. )
  27. if err != nil {
  28. log.Println("Failed to open database:", err)
  29. os.Exit(1)
  30. }
  31. cognitoAuth, err := cognitoauth.New(
  32. os.Getenv("STUFFLOG3_AWS_REGION"),
  33. os.Getenv("STUFFLOG3_AWS_CLIENT_ID"),
  34. os.Getenv("STUFFLOG3_AWS_CLIENT_SECRET"),
  35. os.Getenv("STUFFLOG3_AWS_POOL_ID"),
  36. os.Getenv("STUFFLOG3_AWS_POOL_CLIENT_ID"),
  37. os.Getenv("STUFFLOG3_AWS_POOL_CLIENT_SECRET"),
  38. )
  39. if err != nil {
  40. log.Println("Failed to setup cognito:", err)
  41. os.Exit(1)
  42. }
  43. authService := &auth.Service{
  44. Provider: cognitoAuth,
  45. }
  46. scopesService := &scopes.Service{
  47. Auth: authService,
  48. Repository: db.Scopes(),
  49. StatsLister: db.Stats(),
  50. }
  51. statsService := &stats.Service{
  52. Scopes: scopesService,
  53. Repository: db.Stats(),
  54. }
  55. itemsService := &items.Service{
  56. Auth: authService,
  57. Scopes: scopesService,
  58. Stats: statsService,
  59. Repository: db.Items(),
  60. RequirementFetcher: db.Projects(),
  61. ProjectFetcher: db.Projects(),
  62. }
  63. projectsService := &projects.Service{
  64. Auth: authService,
  65. Scopes: scopesService,
  66. Stats: statsService,
  67. Items: itemsService,
  68. Repository: db.Projects(),
  69. }
  70. sprintsService := &sprints.Service{
  71. Scopes: scopesService,
  72. Items: itemsService,
  73. Projects: projectsService,
  74. Repository: db.Sprints(),
  75. }
  76. server := gin.New()
  77. httpapi.Auth(server.Group("/api/v1/auth"), authService)
  78. apiV1 := server.Group("/api/v1")
  79. if os.Getenv("STUFFLOG3_USE_DUMMY_USER") == "true" {
  80. log.Println("Using dummy UUID")
  81. apiV1.Use(httpapi.DummyMiddleware(authService, "c11230be-4912-4313-83b0-410a248b5bd1"))
  82. } else {
  83. apiV1.Use(httpapi.JwtValidatorMiddleware(authService))
  84. }
  85. apiV1Scopes := apiV1.Group("/scopes")
  86. httpapi.Scopes(apiV1Scopes, scopesService)
  87. apiV1ScopesSub := apiV1Scopes.Group("/:scope_id")
  88. apiV1ScopesSub.Use(httpapi.ScopeMiddleware(scopesService, authService))
  89. httpapi.Projects(apiV1ScopesSub.Group("/projects"), projectsService)
  90. httpapi.Items(apiV1ScopesSub.Group("/items"), itemsService)
  91. httpapi.Sprints(apiV1ScopesSub.Group("/sprints"), sprintsService)
  92. httpapi.Stats(apiV1ScopesSub.Group("/stats"), statsService)
  93. apiV1AllScopes := apiV1.Group("/all-scopes")
  94. apiV1AllScopes.Use(httpapi.AllScopeMiddleware(scopesService, authService))
  95. httpapi.ItemsAllScopes(apiV1AllScopes.Group("/items"), itemsService)
  96. httpapi.SprintsAllScopes(apiV1AllScopes.Group("/sprints"), sprintsService)
  97. exitSignal := make(chan os.Signal)
  98. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  99. errCh := make(chan error)
  100. go func() {
  101. err := server.Run(":8239")
  102. if err != nil {
  103. errCh <- err
  104. }
  105. }()
  106. select {
  107. case sig := <-exitSignal:
  108. {
  109. log.Println("Received signal", sig)
  110. os.Exit(0)
  111. }
  112. case err := <-errCh:
  113. {
  114. log.Println("Server run failed:", err)
  115. os.Exit(2)
  116. }
  117. }
  118. }
  119. func envInt(key string) int {
  120. value := os.Getenv(key)
  121. if value == "" {
  122. return 0
  123. }
  124. intValue, err := strconv.Atoi(value)
  125. if err != nil {
  126. return 0
  127. }
  128. return intValue
  129. }