Skip to content

Commit

Permalink
fix(platform): fix idl relative bug
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Nov 11, 2023
1 parent 0eb1937 commit f447acd
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
9 changes: 9 additions & 0 deletions platform/server/cmd/agent/internal/biz/service/add_idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
agent "github.com/cloudwego/cwgo/platform/server/shared/kitex_gen/agent"
"github.com/cloudwego/cwgo/platform/server/shared/kitex_gen/model"
"github.com/cloudwego/cwgo/platform/server/shared/parser"
"github.com/cloudwego/cwgo/platform/server/shared/repository"
"github.com/cloudwego/cwgo/platform/server/shared/utils"
"net/http"
"path/filepath"
Expand All @@ -47,6 +48,14 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
// check main idl path
repoClient, err := s.svcCtx.RepoManager.GetClient(req.RepositoryId)
if err != nil {
if err == repository.ErrTokenInvalid {
// repo token is invalid or expired
return &agent.AddIDLRes{
Code: http.StatusBadRequest,
Msg: err.Error(),
}, nil
}

return &agent.AddIDLRes{
Code: http.StatusBadRequest,
Msg: "can not get the client",
Expand Down
7 changes: 4 additions & 3 deletions platform/server/cmd/api/internal/biz/logic/idl/add_idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ func (l *AddIDLLogic) AddIDL(req *idl.AddIDLReq) (res *idl.AddIDLRes) {
}

rpcRes, err := client.AddIDL(l.ctx, &agent.AddIDLReq{
RepositoryId: req.RepositoryID,
MainIdlPath: req.MainIdlPath,
ServiceName: req.ServiceName,
RepositoryId: req.RepositoryID,
MainIdlPath: req.MainIdlPath,
ServiceName: req.ServiceName,
ServiceRepositoryName: req.ServiceRepositoryName,
})
if err != nil {
logger.Logger.Error("connect to rpc client failed", zap.Error(err))
Expand Down
1 change: 1 addition & 0 deletions platform/server/shared/config/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package app
import "time"

type Config struct {
ProxyUrl string `mapstructure:"proxyUrl"`
SyncAgentServiceInterval string `mapstructure:"syncAgentServiceInterval"`
SyncRepositoryInterval string `mapstructure:"syncRepositoryInterval"`
SyncIdlInterval string `mapstructure:"syncIdlInterval"`
Expand Down
3 changes: 3 additions & 0 deletions platform/server/shared/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func InitManager(serverType consts.ServerType, serverMode consts.ServerMode, con

}

// init consts in config
consts.ProxyUrl = config.App.ProxyUrl

// get service id
var serviceId string
_, err := os.Stat(consts.AgentMetadataFile)
Expand Down
23 changes: 23 additions & 0 deletions platform/server/shared/consts/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
*
* * Copyright 2022 CloudWeGo Authors
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package consts

var (
ProxyUrl string
)
33 changes: 27 additions & 6 deletions platform/server/shared/repository/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package repository
import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -48,8 +50,6 @@ func NewRepoManager(daoManager *dao.Manager) (*Manager, error) {
}

func (rm *Manager) AddClient(repository *model.Repository) error {
rm.Lock()
defer rm.Unlock()

switch repository.RepositoryType {
case consts.RepositoryTypeNumGitLab:
Expand All @@ -62,14 +62,18 @@ func (rm *Manager) AddClient(repository *model.Repository) error {
}
}

rm.Lock()
rm.repositoryClientsCache.SetDefault(strconv.FormatInt(repository.Id, 10), NewGitLabApi(gitlabClient))
rm.Unlock()
case consts.RepositoryTypeNumGithub:
githubClient, err := NewGithubClient(repository.Token)
if err != nil {
return err
}

rm.Lock()
rm.repositoryClientsCache.SetDefault(strconv.FormatInt(repository.Id, 10), NewGitHubApi(githubClient))
rm.Unlock()
default:
return errors.New("invalid repository type")
}
Expand All @@ -86,9 +90,8 @@ func (rm *Manager) DelClient(repository *model.Repository) {

func (rm *Manager) GetClient(repoId int64) (IRepository, error) {
rm.RLock()
defer rm.RUnlock()

if clientIface, ok := rm.repositoryClientsCache.Get(strconv.FormatInt(repoId, 10)); !ok {
rm.RUnlock()
repo, err := rm.daoManager.Repository.GetRepository(context.Background(), repoId)
if err != nil {
return nil, err
Expand All @@ -109,12 +112,23 @@ func (rm *Manager) GetClient(repoId int64) (IRepository, error) {

return rm.GetClient(repoId)
} else {
rm.RUnlock()
return clientIface.(IRepository), nil
}
}

func NewGitlabClient(token string) (*gitlab.Client, error) {
client, err := gitlab.NewClient(token)
var client *gitlab.Client
var err error

if consts.ProxyUrl != "" {
proxyUrl, _ := url.Parse(consts.ProxyUrl)
httpClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
client, err = gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient))
} else {
client, err = gitlab.NewClient(token)
}

if err != nil {
if strings.Contains(err.Error(), "401 Unauthorized") {
return nil, ErrTokenInvalid
Expand All @@ -127,7 +141,14 @@ func NewGitlabClient(token string) (*gitlab.Client, error) {
}

func NewGithubClient(token string) (*github.Client, error) {
client := github.NewClient(nil).WithAuthToken(token)
var httpClient *http.Client

if consts.ProxyUrl != "" {
proxyUrl, _ := url.Parse(consts.ProxyUrl)
httpClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
}

client := github.NewClient(httpClient).WithAuthToken(token)

_, _, err := client.Meta.Get(context.Background())
if err != nil {
Expand Down

0 comments on commit f447acd

Please sign in to comment.