feat: add proxy
This commit is contained in:
@@ -9,7 +9,8 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"gitea.com/texpixel/document_ai/internal/model/user"
|
||||
"gitea.com/texpixel/document_ai/config"
|
||||
model "gitea.com/texpixel/document_ai/internal/model/user"
|
||||
"gitea.com/texpixel/document_ai/internal/storage/cache"
|
||||
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
||||
"gitea.com/texpixel/document_ai/pkg/common"
|
||||
@@ -171,6 +172,17 @@ type googleTokenResponse struct {
|
||||
TokenType string `json:"token_type"`
|
||||
}
|
||||
|
||||
func (svc *UserService) googleHTTPClient() *http.Client {
|
||||
if config.GlobalConfig.Google.Proxy == "" {
|
||||
return &http.Client{}
|
||||
}
|
||||
proxyURL, err := url.Parse(config.GlobalConfig.Google.Proxy)
|
||||
if err != nil {
|
||||
return &http.Client{}
|
||||
}
|
||||
return &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
|
||||
}
|
||||
|
||||
func (svc *UserService) ExchangeGoogleCodeAndGetUserInfo(ctx context.Context, clientID, clientSecret, code, redirectURI string) (*model.GoogleUserInfo, error) {
|
||||
tokenURL := "https://oauth2.googleapis.com/token"
|
||||
formData := url.Values{
|
||||
@@ -181,7 +193,8 @@ func (svc *UserService) ExchangeGoogleCodeAndGetUserInfo(ctx context.Context, cl
|
||||
"redirect_uri": {redirectURI},
|
||||
}
|
||||
|
||||
resp, err := http.PostForm(tokenURL, formData)
|
||||
client := svc.googleHTTPClient()
|
||||
resp, err := client.PostForm(tokenURL, formData)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "ExchangeGoogleCodeAndGetUserInfo", "msg", "exchange code failed", "error", err)
|
||||
return nil, err
|
||||
@@ -219,7 +232,7 @@ func (svc *UserService) getGoogleUserInfo(ctx context.Context, accessToken strin
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
client := &http.Client{}
|
||||
client := svc.googleHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user