mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
Finish anthropic
This commit is contained in:
@@ -3,11 +3,11 @@ package apinto
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway/apinto/auth"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway/apinto/entity"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway"
|
||||
admin_client "github.com/eolinker/eosc/process-admin/client"
|
||||
)
|
||||
@@ -28,7 +28,7 @@ func (a *ApplicationClient) Online(ctx context.Context, applications ...*gateway
|
||||
return err
|
||||
}
|
||||
for _, app := range applications {
|
||||
err = a.client.Set(ctx, genWorkerID(app.ID, ProfessionApplication), a.toApinto(app))
|
||||
err = a.client.Set(ctx, genWorkerID(app.ID, gateway.ProfessionApplication), a.toApinto(app))
|
||||
if err != nil {
|
||||
a.client.Rollback(ctx)
|
||||
return err
|
||||
@@ -43,7 +43,7 @@ func (a *ApplicationClient) Offline(ctx context.Context, applications ...*gatewa
|
||||
return err
|
||||
}
|
||||
for _, app := range applications {
|
||||
err = a.client.Del(ctx, genWorkerID(app.ID, ProfessionApplication))
|
||||
err = a.client.Del(ctx, genWorkerID(app.ID, gateway.ProfessionApplication))
|
||||
if err != nil {
|
||||
a.client.Rollback(ctx)
|
||||
return err
|
||||
@@ -79,7 +79,7 @@ func (a *ApplicationClient) toApinto(app *gateway.ApplicationRelease) interface{
|
||||
}
|
||||
return &entity.Application{
|
||||
BasicInfo: &entity.BasicInfo{
|
||||
ID: genWorkerID(app.ID, ProfessionApplication),
|
||||
ID: genWorkerID(app.ID, gateway.ProfessionApplication),
|
||||
Name: app.ID,
|
||||
Description: app.Description,
|
||||
Driver: "app",
|
||||
|
||||
@@ -3,13 +3,13 @@ package apinto
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/encoding"
|
||||
|
||||
|
||||
"github.com/eolinker/eosc/process-admin/cmd/proto"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway/apinto/entity"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway"
|
||||
admin_client "github.com/eolinker/eosc/process-admin/client"
|
||||
)
|
||||
@@ -17,12 +17,12 @@ import (
|
||||
var _ gateway.IDynamicClient = &DynamicClient{}
|
||||
|
||||
func NewDynamicClient(client admin_client.Client, resource string) (*DynamicClient, error) {
|
||||
cfg, has := dynamicResourceMap[resource]
|
||||
cfg, has := gateway.GetDynamicResourceDriver(resource)
|
||||
if !has {
|
||||
return nil, errors.New("resource not found")
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return &DynamicClient{client: client, profession: cfg.Profession, driver: cfg.Driver}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package apinto
|
||||
|
||||
const (
|
||||
ProfessionOutput = "output"
|
||||
ProfessionCertificate = "certificate"
|
||||
ProfessionRouter = "router"
|
||||
ProfessionApplication = "app"
|
||||
ProfessionService = "service"
|
||||
ProfessionAIProvider = "ai-provider"
|
||||
)
|
||||
|
||||
var dynamicResourceMap = map[string]Worker{
|
||||
"file-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "file",
|
||||
},
|
||||
"http-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "http_output",
|
||||
},
|
||||
"nsqd-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "nsqd",
|
||||
},
|
||||
"syslog-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "syslog_output",
|
||||
},
|
||||
"kafka-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "kafka_output",
|
||||
},
|
||||
"influxdbv2": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "influxdbv2",
|
||||
},
|
||||
"redis": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "redis",
|
||||
},
|
||||
// 证书
|
||||
"certificate": {
|
||||
Profession: ProfessionCertificate,
|
||||
Driver: "server",
|
||||
},
|
||||
"openai": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "openai",
|
||||
},
|
||||
"google": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "google",
|
||||
},
|
||||
"moonshot": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "moonshot",
|
||||
},
|
||||
"tongyi": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "tongyi",
|
||||
},
|
||||
"zhipuai": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "zhipuai",
|
||||
},
|
||||
"fireworks": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "fireworks",
|
||||
},
|
||||
"novita": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "novita",
|
||||
},
|
||||
"mistralai": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "mistralai",
|
||||
},
|
||||
"baichuan": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "baichuan",
|
||||
},
|
||||
"stepfun": {
|
||||
Profession: ProfessionAIProvider,
|
||||
Driver: "stepfun",
|
||||
},
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
Profession string
|
||||
Driver string
|
||||
}
|
||||
+20
-19
@@ -4,16 +4,17 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway/apinto/driver"
|
||||
|
||||
|
||||
"github.com/eolinker/eosc/process-admin/cmd/proto"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/encoding"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/utils"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway/apinto/entity"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway"
|
||||
admin_client "github.com/eolinker/eosc/process-admin/client"
|
||||
)
|
||||
@@ -23,7 +24,7 @@ var _ gateway.IProjectClient = &ProjectClient{}
|
||||
func init() {
|
||||
driver.RegisterApiPublishHandler(func(ctx context.Context, client admin_client.Client, api *entity.Router, extends map[string]any) error {
|
||||
return client.Set(ctx, api.ID, api)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@@ -61,8 +62,8 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele
|
||||
matches := map[string]string{
|
||||
"project": project.Id,
|
||||
}
|
||||
|
||||
upstreams, err := matchLabels[entity.Service](ctx, p.client, ProfessionService, matches)
|
||||
|
||||
upstreams, err := matchLabels[entity.Service](ctx, p.client, gateway.ProfessionService, matches)
|
||||
if err != nil {
|
||||
if !errors.Is(err, proto.Nil) {
|
||||
return err
|
||||
@@ -71,14 +72,14 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele
|
||||
upstreamMap := utils.SliceToMap(upstreams, func(t *entity.Service) string {
|
||||
return t.ID
|
||||
})
|
||||
|
||||
upstreamId := genWorkerID(project.Upstream.ID, ProfessionService)
|
||||
|
||||
upstreamId := genWorkerID(project.Upstream.ID, gateway.ProfessionService)
|
||||
err = p.client.Set(ctx, upstreamId, entity.ToService(project.Upstream, project.Version, matches))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delete(upstreamMap, upstreamId)
|
||||
routers, err := matchLabels[entity.Router](ctx, p.client, ProfessionRouter, matches)
|
||||
routers, err := matchLabels[entity.Router](ctx, p.client, gateway.ProfessionRouter, matches)
|
||||
if err != nil {
|
||||
if !errors.Is(err, proto.Nil) {
|
||||
return err
|
||||
@@ -87,16 +88,16 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele
|
||||
routerMap := utils.SliceToMap(routers, func(t *entity.Router) string {
|
||||
return t.ID
|
||||
})
|
||||
|
||||
|
||||
for _, api := range project.Apis {
|
||||
id := genWorkerID(api.ID, ProfessionRouter)
|
||||
id := genWorkerID(api.ID, gateway.ProfessionRouter)
|
||||
if api.Labels == nil {
|
||||
api.Labels = make(map[string]string)
|
||||
}
|
||||
api.Service = upstreamId
|
||||
api.Labels["provider"] = project.Id
|
||||
routerInfo := entity.ToRouter(api, project.Version, matches)
|
||||
|
||||
|
||||
err = driver.ApiPublish(ctx, p.client, routerInfo, api.Extends)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -113,7 +114,7 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
for id := range upstreamMap {
|
||||
err = p.client.Del(ctx, id)
|
||||
@@ -121,7 +122,7 @@ func (p *ProjectClient) online(ctx context.Context, project *gateway.ProjectRele
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -137,16 +138,16 @@ func (p *ProjectClient) Offline(ctx context.Context, projects ...*gateway.Projec
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return p.client.Commit(ctx)
|
||||
}
|
||||
|
||||
func (p *ProjectClient) delete(ctx context.Context, id string) error {
|
||||
err := p.deleteByLabels(ctx, ProfessionRouter, map[string]string{"project": id})
|
||||
err := p.deleteByLabels(ctx, gateway.ProfessionRouter, map[string]string{"project": id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.deleteByLabels(ctx, ProfessionService, map[string]string{"project": id})
|
||||
return p.deleteByLabels(ctx, gateway.ProfessionService, map[string]string{"project": id})
|
||||
}
|
||||
func matchLabels[T any](ctx context.Context, client admin_client.Client, profession string, labels map[string]string, t ...[]*T) ([]*T, error) {
|
||||
list, err := client.MatchLabels(ctx, profession, labels)
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package gateway
|
||||
|
||||
const (
|
||||
ProfessionOutput = "output"
|
||||
ProfessionCertificate = "certificate"
|
||||
ProfessionRouter = "router"
|
||||
ProfessionApplication = "app"
|
||||
ProfessionService = "service"
|
||||
ProfessionAIProvider = "ai-provider"
|
||||
)
|
||||
|
||||
func RegisterDynamicResourceDriver(key string, worker Worker) {
|
||||
dynamicResourceMap[key] = worker
|
||||
}
|
||||
|
||||
func GetDynamicResourceDriver(key string) (Worker, bool) {
|
||||
v, ok := dynamicResourceMap[key]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
var dynamicResourceMap = map[string]Worker{
|
||||
"file-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "file",
|
||||
},
|
||||
"http-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "http_output",
|
||||
},
|
||||
"nsqd-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "nsqd",
|
||||
},
|
||||
"syslog-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "syslog_output",
|
||||
},
|
||||
"kafka-access-log": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "kafka_output",
|
||||
},
|
||||
"influxdbv2": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "influxdbv2",
|
||||
},
|
||||
"redis": {
|
||||
Profession: ProfessionOutput,
|
||||
Driver: "redis",
|
||||
},
|
||||
// 证书
|
||||
"certificate": {
|
||||
Profession: ProfessionCertificate,
|
||||
Driver: "server",
|
||||
},
|
||||
//"openai": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "openai",
|
||||
//},
|
||||
//"google": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "google",
|
||||
//},
|
||||
//"anthropic": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "anthropic",
|
||||
//},
|
||||
//"moonshot": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "moonshot",
|
||||
//},
|
||||
//"tongyi": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "tongyi",
|
||||
//},
|
||||
//"zhipuai": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "zhipuai",
|
||||
//},
|
||||
//"fireworks": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "fireworks",
|
||||
//},
|
||||
//"novita": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "novita",
|
||||
//},
|
||||
//"mistralai": {
|
||||
// Profession: ProfessionAIProvider,
|
||||
// Driver: "mistralai",
|
||||
//},
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
Profession string
|
||||
Driver string
|
||||
}
|
||||
Reference in New Issue
Block a user