From 0a864aea41fa7c53943d4e2e995a283f93d84e34 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Fri, 17 Jan 2025 16:03:09 +0800 Subject: [PATCH 1/2] add openapi --- init.go | 1 + plugins/openapi/authorization.go | 18 +++++++++++++ plugins/openapi/check.go | 45 ++++++++++++++++++++++++++++++++ plugins/openapi/driver.go | 19 ++++++++++++++ plugins/openapi/plugin.go | 33 +++++++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 plugins/openapi/authorization.go create mode 100644 plugins/openapi/check.go create mode 100644 plugins/openapi/driver.go create mode 100644 plugins/openapi/plugin.go diff --git a/init.go b/init.go index 74592a3a..af7b6ca9 100644 --- a/init.go +++ b/init.go @@ -5,6 +5,7 @@ import ( _ "github.com/APIParkLab/APIPark/frontend" _ "github.com/APIParkLab/APIPark/gateway/apinto" _ "github.com/APIParkLab/APIPark/plugins/core" + _ "github.com/APIParkLab/APIPark/plugins/openapi" _ "github.com/APIParkLab/APIPark/plugins/permit" _ "github.com/APIParkLab/APIPark/plugins/publish_flow" _ "github.com/APIParkLab/APIPark/resources/locale" diff --git a/plugins/openapi/authorization.go b/plugins/openapi/authorization.go new file mode 100644 index 00000000..d6f37e8a --- /dev/null +++ b/plugins/openapi/authorization.go @@ -0,0 +1,18 @@ +package openapi + +import ( + "net/http" + + "github.com/eolinker/go-common/pm3" +) + +func (p *plugin) appAuthorizationApis() []pm3.Api { + return []pm3.Api{ + pm3.CreateApiWidthDoc(http.MethodPost, "/openapi/v1/app/authorization", []string{"context", "query:app", "body"}, []string{"authorization"}, p.authorizationController.AddAuthorization), + pm3.CreateApiWidthDoc(http.MethodPut, "/openapi/v1/app/authorization", []string{"context", "query:app", "query:authorization", "body"}, []string{"authorization"}, p.authorizationController.EditAuthorization), + pm3.CreateApiWidthDoc(http.MethodDelete, "/openapi/v1/app/authorization", []string{"context", "query:app", "query:authorization"}, nil, p.authorizationController.DeleteAuthorization), + pm3.CreateApiWidthDoc(http.MethodGet, "/openapi/v1/app/authorization", []string{"context", "query:app", "query:authorization"}, []string{"authorization"}, p.authorizationController.Info), + pm3.CreateApiWidthDoc(http.MethodGet, "/openapi/v1/app/authorizations", []string{"context", "query:app"}, []string{"authorizations"}, p.authorizationController.Authorizations), + pm3.CreateApiWidthDoc(http.MethodGet, "/openapi/v1/app/authorization/details", []string{"context", "query:app", "query:authorization"}, []string{"details"}, p.authorizationController.Detail), + } +} diff --git a/plugins/openapi/check.go b/plugins/openapi/check.go new file mode 100644 index 00000000..4f5f50f7 --- /dev/null +++ b/plugins/openapi/check.go @@ -0,0 +1,45 @@ +package openapi + +import ( + "strings" + + "github.com/eolinker/eosc/env" + + "github.com/gin-gonic/gin" +) + +var ( + defaultAPIKey = "37eb0ebf" + openCheck = newOpenapiCheck() +) + +type openapiCheck struct { + apikey string +} + +func newOpenapiCheck() *openapiCheck { + apikey, has := env.GetEnv("API_KEY") + if !has { + apikey = defaultAPIKey + } + return &openapiCheck{apikey: apikey} +} + +func (o *openapiCheck) Check(method string, path string) (bool, []gin.HandlerFunc) { + if strings.HasPrefix(path, "/openapi/") { + return true, []gin.HandlerFunc{o.Handler} + } + return false, nil +} + +func (o *openapiCheck) Sort() int { + return -1 +} + +func (o *openapiCheck) Handler(ginCtx *gin.Context) { + authorization := ginCtx.GetHeader("Authorization") + if authorization == "" { + ginCtx.AbortWithStatusJSON(403, gin.H{"code": -8, "msg": "invalid token", "success": "fail"}) + return + } +} diff --git a/plugins/openapi/driver.go b/plugins/openapi/driver.go new file mode 100644 index 00000000..9e9997a6 --- /dev/null +++ b/plugins/openapi/driver.go @@ -0,0 +1,19 @@ +package openapi + +import ( + "github.com/eolinker/go-common/autowire" + "github.com/eolinker/go-common/pm3" +) + +func init() { + pm3.Register("openapi", new(Driver)) +} + +type Driver struct { +} + +func (d *Driver) Create() (pm3.IPlugin, error) { + p := new(plugin) + autowire.Autowired(p) + return p, nil +} diff --git a/plugins/openapi/plugin.go b/plugins/openapi/plugin.go new file mode 100644 index 00000000..3082fb84 --- /dev/null +++ b/plugins/openapi/plugin.go @@ -0,0 +1,33 @@ +package openapi + +import ( + application_authorization "github.com/APIParkLab/APIPark/controller/application-authorization" + "github.com/eolinker/go-common/pm3" +) + +var ( + _ pm3.IPlugin = (*plugin)(nil) + _ pm3.IPluginMiddleware = (*plugin)(nil) +) + +type plugin struct { + apis []pm3.Api + authorizationController application_authorization.IAuthorizationController `autowired:""` +} + +func (p *plugin) Middlewares() []pm3.IMiddleware { + return []pm3.IMiddleware{ + openCheck, + } +} + +func (p *plugin) APis() []pm3.Api { + return p.apis +} + +func (p *plugin) Name() string { + return "openapi" +} +func (p *plugin) OnComplete() { + p.apis = p.appAuthorizationApis() +} From 070882655db3623152b5766ba853c3532f503fce Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 20 Jan 2025 13:54:58 +0800 Subject: [PATCH 2/2] update ai bug --- module/ai/iml.go | 67 ++++++++++++++--------------- stores/api/model.go | 2 +- stores/universally/commit/commit.go | 2 +- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/module/ai/iml.go b/module/ai/iml.go index 0e4b9a50..427155b1 100644 --- a/module/ai/iml.go +++ b/module/ai/iml.go @@ -7,17 +7,17 @@ import ( "net/http" "sort" "time" - + "github.com/APIParkLab/APIPark/service/service" - + ai_key_dto "github.com/APIParkLab/APIPark/module/ai-key/dto" - + ai_key "github.com/APIParkLab/APIPark/service/ai-key" - + "github.com/eolinker/go-common/auto" - + ai_api "github.com/APIParkLab/APIPark/service/ai-api" - + model_runtime "github.com/APIParkLab/APIPark/ai-provider/model-runtime" "github.com/APIParkLab/APIPark/gateway" ai_dto "github.com/APIParkLab/APIPark/module/ai/dto" @@ -30,7 +30,7 @@ import ( ) func newKey(key *ai_key.Key) *gateway.DynamicRelease { - + return &gateway.DynamicRelease{ BasicItem: &gateway.BasicItem{ ID: fmt.Sprintf("%s-%s", key.Provider, key.ID), @@ -91,7 +91,7 @@ func (i *imlProviderModule) Sort(ctx context.Context, input *ai_dto.Sort) error if !has { continue } - + l, has := providerMap[id] if !has { continue @@ -139,7 +139,7 @@ func (i *imlProviderModule) Sort(ctx context.Context, input *ai_dto.Sort) error return err } return i.syncGateway(ctx, cluster.DefaultClusterID, offlineReleases, false) - + }) } @@ -176,7 +176,7 @@ func (i *imlProviderModule) ConfiguredProviders(ctx context.Context) ([]*ai_dto. return nil, nil, fmt.Errorf("create default key error:%v", err) } } - + p, has := model_runtime.GetProvider(l.Id) if !has { continue @@ -185,7 +185,7 @@ func (i *imlProviderModule) ConfiguredProviders(ctx context.Context) ([]*ai_dto. if err != nil { return nil, nil, fmt.Errorf("get provider keys error:%v", err) } - + keysStatus := make([]*ai_dto.KeyStatus, 0, len(keys)) for _, k := range keys { status := ai_key_dto.ToKeyStatus(k.Status) @@ -204,7 +204,7 @@ func (i *imlProviderModule) ConfiguredProviders(ctx context.Context) ([]*ai_dto. sort.Slice(keysStatus, func(i, j int) bool { return keysStatus[i].Priority < keysStatus[j].Priority }) - + providers = append(providers, &ai_dto.ConfiguredProviderItem{ Id: l.Id, Name: l.Name, @@ -248,7 +248,7 @@ func (i *imlProviderModule) SimpleProviders(ctx context.Context) ([]*ai_dto.Simp return nil, err } providers := model_runtime.Providers() - + providerMap := utils.SliceToMap(list, func(e *ai.Provider) string { return e.Id }) @@ -315,7 +315,7 @@ func (i *imlProviderModule) SimpleConfiguredProviders(ctx context.Context) ([]*a Name: model.ID(), }, } - + items = append(items, item) } sort.Slice(items, func(i, j int) bool { @@ -426,7 +426,7 @@ func (i *imlProviderModule) Provider(ctx context.Context, id string) (*ai_dto.Pr if info.Priority == 0 { info.Priority = maxPriority } - + return &ai_dto.Provider{ Id: info.Id, Name: info.Name, @@ -445,12 +445,12 @@ func (i *imlProviderModule) LLMs(ctx context.Context, driver string) ([]*ai_dto. if !has { return nil, nil, fmt.Errorf("ai provider not found") } - + llms, has := p.ModelsByType(model_runtime.ModelTypeLLM) if !has { return nil, nil, fmt.Errorf("ai provider not found") } - + items := make([]*ai_dto.LLMItem, 0, len(llms)) for _, v := range llms { items = append(items, &ai_dto.LLMItem{ @@ -478,7 +478,7 @@ func (i *imlProviderModule) LLMs(ctx context.Context, driver string) ([]*ai_dto. Logo: p.Logo(), }, nil } - + return items, &ai_dto.ProviderItem{ Id: info.Id, Name: info.Name, @@ -533,6 +533,7 @@ func (i *imlProviderModule) UpdateProviderConfig(ctx context.Context, id string, DefaultLLM: &input.DefaultLLM, Config: &input.Config, Priority: input.Priority, + Status: &status, } _, err = i.aiKeyService.DefaultKey(ctx, id) if err != nil { @@ -557,7 +558,7 @@ func (i *imlProviderModule) UpdateProviderConfig(ctx context.Context, id string, if err != nil { return err } - + if input.Enable != nil { status = 0 if *input.Enable { @@ -569,6 +570,7 @@ func (i *imlProviderModule) UpdateProviderConfig(ctx context.Context, id string, if err != nil { return err } + if *pInfo.Status == 0 { return i.syncGateway(ctx, cluster.DefaultClusterID, []*gateway.DynamicRelease{ { @@ -612,11 +614,11 @@ func (i *imlProviderModule) getAiProviders(ctx context.Context) ([]*gateway.Dyna if err != nil { return nil, err } - + providers := make([]*gateway.DynamicRelease, 0, len(list)) for _, l := range list { // 获取当前供应商所有Key信息 - + driver, has := model_runtime.GetProvider(l.Id) if !has { return nil, fmt.Errorf("provider not found: %s", l.Id) @@ -651,7 +653,7 @@ func (i *imlProviderModule) initGateway(ctx context.Context, clusterId string, c if err != nil { return err } - + for _, p := range providers { client, err := clientDriver.Dynamic(p.Resource) if err != nil { @@ -662,7 +664,7 @@ func (i *imlProviderModule) initGateway(ctx context.Context, clusterId string, c return err } } - + return nil } @@ -692,7 +694,7 @@ func (i *imlProviderModule) syncGateway(ctx context.Context, clusterId string, r return err } } - + return nil } @@ -725,9 +727,9 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st Name: s.Name, }) serviceTeamMap[s.Id] = s.Team - + } - + modelItems := utils.SliceToSlice(p.Models(), func(e model_runtime.IModel) *ai_dto.BasicInfo { return &ai_dto.BasicInfo{ Id: e.ID(), @@ -750,7 +752,7 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st if err != nil { return nil, nil, 0, err } - + if len(apis) <= 0 { return nil, condition, 0, nil } @@ -765,10 +767,10 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st if err != nil { return nil, nil, 0, err } - + apiItems := utils.SliceToSlice(results, func(e *ai_api.APIUse) *ai_dto.APIItem { info := apiMap[e.API] - + delete(apiMap, e.API) return &ai_dto.APIItem{ Id: e.API, @@ -812,8 +814,5 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st for i := offset; i < offset+size && i < len(sortApis); i++ { apiItems = append(apiItems, sortApis[i]) } - - total := int64(len(apis)) - return apiItems, condition, total, nil - } -} + + total := int64(len(apis)) \ No newline at end of file diff --git a/stores/api/model.go b/stores/api/model.go index 1cfb6106..d530cf3f 100644 --- a/stores/api/model.go +++ b/stores/api/model.go @@ -55,7 +55,7 @@ type Doc struct { Id int64 `gorm:"column:id;type:BIGINT(20);AUTO_INCREMENT;NOT NULL;comment:id;primary_key;comment:主键ID;"` UUID string `gorm:"type:varchar(36);not null;column:uuid;uniqueIndex:uuid;comment:UUID;"` Service string `gorm:"size:36;not null;column:service;comment:服务;index:service"` - Content string `gorm:"type:text;null;column:content;comment:文档内容"` + Content string `gorm:"type:longtext;null;column:content;comment:文档内容"` Updater string `gorm:"size:36;not null;column:updater;comment:更新人;index:updater" aovalue:"updater"` UpdateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:update_at;comment:更新时间"` APICount int64 `gorm:"type:int(11);not null;column:api_count;comment:接口数量"` diff --git a/stores/universally/commit/commit.go b/stores/universally/commit/commit.go index d6850960..700fb722 100644 --- a/stores/universally/commit/commit.go +++ b/stores/universally/commit/commit.go @@ -7,7 +7,7 @@ type Commit[H any] struct { UUID string `gorm:"size:36;not null;column:uuid;comment:uuid;uniqueIndex:uuid;"` Target string `gorm:"column:target;type:varchar(36);NOT NULL;comment:目标id;index:target;"` Key string `gorm:"size:50;not null;column:key;comment:类型;index:key;"` - Data *H `gorm:"type:text;not null;column:data;comment:数据;charset=utf8mb4;serializer:json"` + Data *H `gorm:"type:longtext;not null;column:data;comment:数据;charset=utf8mb4;serializer:json"` CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:创建时间"` Operator string `gorm:"size:36;not null;column:operator;comment:操作人;index:operator;"` }