mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
ai路由完成
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package ai_api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/APIParkLab/APIPark/service/universally"
|
||||
"github.com/APIParkLab/APIPark/stores/api"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ IAPIService = (*imlAPIService)(nil)
|
||||
|
||||
type imlAPIService struct {
|
||||
store api.IAiAPIInfoStore `autowired:""`
|
||||
|
||||
universally.IServiceGet[API]
|
||||
universally.IServiceCreate[Create]
|
||||
universally.IServiceEdit[Edit]
|
||||
universally.IServiceDelete
|
||||
}
|
||||
|
||||
func (i *imlAPIService) OnComplete() {
|
||||
i.IServiceGet = universally.NewGetSoftDelete[API, api.AiAPIInfo](i.store, FromEntity)
|
||||
i.IServiceCreate = universally.NewCreatorSoftDelete[Create, api.AiAPIInfo](i.store, "ai_api_info", createEntityHandler, uniquestHandler, labelHandler)
|
||||
i.IServiceEdit = universally.NewEdit[Edit, api.AiAPIInfo](i.store, updateHandler)
|
||||
i.IServiceDelete = universally.NewSoftDelete[api.AiAPIInfo](i.store)
|
||||
}
|
||||
|
||||
func labelHandler(e *api.AiAPIInfo) []string {
|
||||
return []string{e.Name, e.Uuid}
|
||||
}
|
||||
func uniquestHandler(i *Create) []map[string]interface{} {
|
||||
return []map[string]interface{}{{"uuid": i.ID}}
|
||||
}
|
||||
func createEntityHandler(i *Create) *api.AiAPIInfo {
|
||||
now := time.Now()
|
||||
cfg, _ := json.Marshal(i.AdditionalConfig)
|
||||
return &api.AiAPIInfo{
|
||||
Uuid: i.ID,
|
||||
Name: i.Name,
|
||||
Service: i.Service,
|
||||
Path: i.Path,
|
||||
Description: i.Description,
|
||||
Timeout: i.Timeout,
|
||||
Retry: i.Retry,
|
||||
Model: i.Model,
|
||||
CreateAt: now,
|
||||
UpdateAt: now,
|
||||
AdditionalConfig: string(cfg),
|
||||
}
|
||||
}
|
||||
func updateHandler(e *api.AiAPIInfo, i *Edit) {
|
||||
if i.Name != nil {
|
||||
e.Name = *i.Name
|
||||
}
|
||||
if i.Path != nil {
|
||||
e.Path = *i.Path
|
||||
}
|
||||
if i.Description != nil {
|
||||
e.Description = *i.Description
|
||||
}
|
||||
if i.Timeout != nil {
|
||||
e.Timeout = *i.Timeout
|
||||
}
|
||||
if i.Retry != nil {
|
||||
e.Retry = *i.Retry
|
||||
}
|
||||
if i.Model != nil {
|
||||
e.Model = *i.Model
|
||||
}
|
||||
if i.AdditionalConfig != nil {
|
||||
cfg, _ := json.Marshal(i.AdditionalConfig)
|
||||
e.AdditionalConfig = string(cfg)
|
||||
}
|
||||
e.UpdateAt = time.Now()
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package ai_api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/APIParkLab/APIPark/stores/api"
|
||||
"time"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
ID string
|
||||
Name string
|
||||
Service string
|
||||
Path string
|
||||
Description string
|
||||
Timeout int
|
||||
Retry int
|
||||
Model string
|
||||
CreateAt time.Time
|
||||
UpdateAt time.Time
|
||||
Creator string
|
||||
Updater string
|
||||
AdditionalConfig map[string]interface{}
|
||||
Disable bool
|
||||
}
|
||||
|
||||
type Create struct {
|
||||
ID string
|
||||
Name string
|
||||
Service string
|
||||
Path string
|
||||
Description string
|
||||
Timeout int
|
||||
Retry int
|
||||
Model string
|
||||
AdditionalConfig map[string]interface{}
|
||||
}
|
||||
|
||||
type Edit struct {
|
||||
Name *string
|
||||
Path *string
|
||||
Description *string
|
||||
Timeout *int
|
||||
Retry *int
|
||||
Model *string
|
||||
AdditionalConfig *map[string]interface{}
|
||||
}
|
||||
|
||||
func FromEntity(e *api.AiAPIInfo) *API {
|
||||
cfg := make(map[string]interface{})
|
||||
if e.AdditionalConfig != "" {
|
||||
_ = json.Unmarshal([]byte(e.AdditionalConfig), &cfg)
|
||||
}
|
||||
return &API{
|
||||
ID: e.Uuid,
|
||||
Name: e.Name,
|
||||
Service: e.Service,
|
||||
Path: e.Path,
|
||||
Description: e.Description,
|
||||
Timeout: e.Timeout,
|
||||
Retry: e.Retry,
|
||||
Model: e.Model,
|
||||
CreateAt: e.CreateAt,
|
||||
UpdateAt: e.UpdateAt,
|
||||
Creator: e.Creator,
|
||||
Updater: e.Updater,
|
||||
AdditionalConfig: cfg,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ai_api
|
||||
|
||||
import (
|
||||
"github.com/APIParkLab/APIPark/service/universally"
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type IAPIService interface {
|
||||
universally.IServiceGet[API]
|
||||
universally.IServiceCreate[Create]
|
||||
universally.IServiceEdit[Edit]
|
||||
universally.IServiceDelete
|
||||
|
||||
//ListByServices(ctx context.Context, serviceIds ...string) ([]*API, error)
|
||||
}
|
||||
|
||||
func init() {
|
||||
autowire.Auto[IAPIService](func() reflect.Value {
|
||||
return reflect.ValueOf(new(imlAPIService))
|
||||
})
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import "time"
|
||||
|
||||
type UpdateDoc struct {
|
||||
ID string
|
||||
Service string
|
||||
Content string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user