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:
+11
-19
@@ -11,23 +11,15 @@ import (
|
||||
)
|
||||
|
||||
type GiteaOAuth2Handler struct {
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
InstanceUrl string
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
var GiteaOauth2HandlerInstance *GiteaOAuth2Handler
|
||||
|
||||
func InitGiteaOauth2Token() {
|
||||
GiteaOauth2HandlerInstance = &GiteaOAuth2Handler{
|
||||
ClientID: config.Config.GiteaOauthClientID,
|
||||
ClientSecret: config.Config.GiteaOauthClientSecret,
|
||||
InstanceUrl: config.Config.GiteaOauthInstance,
|
||||
}
|
||||
func NewGiteaOauth2Token(cfg *config.Config) *GiteaOAuth2Handler {
|
||||
return &GiteaOAuth2Handler{cfg: cfg}
|
||||
}
|
||||
|
||||
func (g *GiteaOAuth2Handler) GetGiteaLoginURL(redirectHost string) (string, error) {
|
||||
if g.InstanceUrl == "" {
|
||||
if g.cfg.GiteaOauthInstance == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -36,13 +28,13 @@ func (g *GiteaOAuth2Handler) GetGiteaLoginURL(redirectHost string) (string, erro
|
||||
return "", nil
|
||||
}
|
||||
|
||||
authUrl := g.InstanceUrl + "/login/oauth/authorize?client_id=" + g.ClientID + "&redirect_uri=" + "http://" + redirectHost + "/admin/oauth/gitea/callback&scope=openid&response_type=code&response_mode=form_post"
|
||||
authUrl := g.cfg.GiteaOauthInstance + "/login/oauth/authorize?client_id=" + g.cfg.GiteaOauthClientID + "&redirect_uri=" + "http://" + redirectHost + "/admin/oauth/gitea/callback&scope=openid&response_type=code&response_mode=form_post"
|
||||
return authUrl, nil
|
||||
}
|
||||
|
||||
func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error) {
|
||||
|
||||
if g.InstanceUrl == "" {
|
||||
if g.cfg.GiteaOauthInstance == "" {
|
||||
slog.Error("Instance URL not provided")
|
||||
return "", nil
|
||||
}
|
||||
@@ -52,7 +44,7 @@ func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error
|
||||
return "", err
|
||||
}
|
||||
|
||||
userInfoUrl := g.InstanceUrl + "/login/oauth/userinfo"
|
||||
userInfoUrl := g.cfg.GiteaOauthInstance + "/login/oauth/userinfo"
|
||||
slog.Info(userInfoUrl)
|
||||
req, err := http.NewRequest("POST", userInfoUrl, nil)
|
||||
if err != nil {
|
||||
@@ -85,12 +77,12 @@ func (g *GiteaOAuth2Handler) GetGiteaUserEmailByCode(code string) (string, error
|
||||
|
||||
func (g *GiteaOAuth2Handler) getGiteaAccessTokenByCode(code string) (string, error) {
|
||||
form := url.Values{}
|
||||
form.Add("client_id", g.ClientID)
|
||||
form.Add("client_secret", g.ClientSecret)
|
||||
form.Add("client_id", g.cfg.GiteaOauthClientID)
|
||||
form.Add("client_secret", g.cfg.GiteaOauthClientSecret)
|
||||
form.Add("code", code)
|
||||
form.Add("grant_type", "authorization_code")
|
||||
|
||||
req, err := http.NewRequest("POST", g.InstanceUrl+"/login/oauth/access_token", bytes.NewBufferString(form.Encode()))
|
||||
req, err := http.NewRequest("POST", g.cfg.GiteaOauthInstance+"/login/oauth/access_token", bytes.NewBufferString(form.Encode()))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -112,4 +104,4 @@ func (g *GiteaOAuth2Handler) getGiteaAccessTokenByCode(code string) (string, err
|
||||
}
|
||||
|
||||
return tokenResp.AccessToken, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user