Refactor: Implement uber-go/fx dependency injection
- Replace global variable pattern with proper dependency injection - Add uber-go/fx for automatic dependency resolution - Refactor all services and handlers to use constructor injection - Eliminate fragile initialization order dependencies - Improve testability and modularity - Add structured logging with zap Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
+36
-30
@@ -1,20 +1,33 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/auth"
|
||||
"CatsOfMastodonBotGo/internal/web/handlers"
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"CatsOfMastodonBotGo/internal/web/handlers"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
func SetupRouter() *gin.Engine {
|
||||
// fx.In allows fx to inject multiple dependencies
|
||||
type RouterParams struct {
|
||||
fx.In
|
||||
|
||||
Lifecycle fx.Lifecycle
|
||||
AdminDashboard *handlers.AdminDashboardHandler
|
||||
ApiEndpoint *handlers.ApiEndpointHandler
|
||||
EmbedCard *handlers.EmbedCardHandler
|
||||
OauthLogin *handlers.OauthLoginHandler
|
||||
}
|
||||
|
||||
func SetupRouter(params RouterParams) {
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(cors.New(cors.Config{
|
||||
AllowAllOrigins: true,
|
||||
AllowAllOrigins: true,
|
||||
AllowMethods: []string{"POST", "GET", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
|
||||
AllowCredentials: true,
|
||||
@@ -22,44 +35,37 @@ func SetupRouter() *gin.Engine {
|
||||
|
||||
r.LoadHTMLGlob("internal/web/templates/home/*")
|
||||
|
||||
auth.InitJwtTokenGenerator() // Must be befor initializing admin handler, otherwise 'panic: runtime error: invalid memory address or nil pointer dereference'
|
||||
auth.InitGiteaOauth2Token()
|
||||
handlers.InitAdminDashboardHandler()
|
||||
handlers.InitApiEndpointHandler()
|
||||
handlers.InitEmbedCardHandler()
|
||||
handlers.InitOauthLoginHandler()
|
||||
|
||||
// Main page
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "home/index.html", nil)
|
||||
})
|
||||
// Embed card
|
||||
r.GET("/embed", handlers.EmbedCardHandlerInstance.GetEmbedCard)
|
||||
r.GET("/embed", params.EmbedCard.GetEmbedCard)
|
||||
|
||||
admin := r.Group("/admin")
|
||||
|
||||
// My man, this is done way more efficient and fast in .NET, specially the authentication part
|
||||
// admin.GET("/", func(c *gin.Context) {
|
||||
// c.HTML(http.StatusOK, "admin/index.html", nil)
|
||||
// })
|
||||
// admin.GET("/login", func(c *gin.Context) {
|
||||
// c.HTML(http.StatusOK, "admin/index.html", nil)
|
||||
// })
|
||||
r.Use(static.Serve("/admin", static.LocalFile("internal/web/templates/admin", true)))
|
||||
// I dont know a better way for path handling
|
||||
r.Use(static.Serve("/admin/oauth/gitea/callback", static.LocalFile("internal/web/templates/admin", true)))
|
||||
|
||||
adminApi := admin.Group("/api")
|
||||
adminApi.POST("/login", handlers.AdminDashboardHandlerInstance.Login)
|
||||
adminApi.GET("/login/oauth/gitea", handlers.OauthLoginHandlerInstance.GoToGiteaLogin)
|
||||
adminApi.POST("/login/oauth/gitea/final", handlers.OauthLoginHandlerInstance.LoginWithGitea)
|
||||
adminApi.GET("/getmedia", auth.JwtTokenGeneratorInstance.GinMiddleware(), handlers.AdminDashboardHandlerInstance.GetMedia)
|
||||
adminApi.POST("/approve", auth.JwtTokenGeneratorInstance.GinMiddleware(), handlers.AdminDashboardHandlerInstance.ApproveMedia)
|
||||
adminApi.POST("/reject", auth.JwtTokenGeneratorInstance.GinMiddleware(), handlers.AdminDashboardHandlerInstance.RejectMedia)
|
||||
adminApi.POST("/login", params.AdminDashboard.Login)
|
||||
adminApi.GET("/login/oauth/gitea", params.OauthLogin.GoToGiteaLogin)
|
||||
adminApi.POST("/login/oauth/gitea/final", params.OauthLogin.LoginWithGitea)
|
||||
adminApi.GET("/getmedia", params.AdminDashboard.JWTMiddleware(), params.AdminDashboard.GetMedia)
|
||||
adminApi.POST("/approve", params.AdminDashboard.JWTMiddleware(), params.AdminDashboard.ApproveMedia)
|
||||
adminApi.POST("/reject", params.AdminDashboard.JWTMiddleware(), params.AdminDashboard.RejectMedia)
|
||||
|
||||
api := r.Group("/api")
|
||||
api.GET("/post/random", params.ApiEndpoint.GetRandomPost)
|
||||
|
||||
api.GET("/post/random", handlers.ApiEndpointHandlerInstance.GetRandomPost)
|
||||
|
||||
return r
|
||||
}
|
||||
params.Lifecycle.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
go func() {
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
// Handle error appropriately
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user