mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
api文档完成
This commit is contained in:
@@ -2,6 +2,14 @@ package upstream_dto
|
||||
|
||||
type UpstreamConfig *Upstream
|
||||
|
||||
type ExportUpstream struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Service string `json:"service"`
|
||||
|
||||
*Upstream
|
||||
}
|
||||
|
||||
type Upstream struct {
|
||||
Type string `json:"driver"`
|
||||
Balance string `json:"balance"`
|
||||
|
||||
+47
-15
@@ -4,34 +4,66 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/universally/commit"
|
||||
"github.com/eolinker/go-common/utils"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/cluster"
|
||||
"github.com/APIParkLab/APIPark/service/service"
|
||||
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/upstream"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/store"
|
||||
|
||||
|
||||
upstream_dto "github.com/APIParkLab/APIPark/module/upstream/dto"
|
||||
)
|
||||
|
||||
var (
|
||||
_ IUpstreamModule = (*imlUpstreamModule)(nil)
|
||||
asServer = map[string]bool{
|
||||
_ IUpstreamModule = (*imlUpstreamModule)(nil)
|
||||
_ IExportUpstreamModule = (*imlUpstreamModule)(nil)
|
||||
asServer = map[string]bool{
|
||||
"as_server": true,
|
||||
}
|
||||
)
|
||||
|
||||
type imlUpstreamModule struct {
|
||||
projectService service.IServiceService `autowired:""`
|
||||
serviceService service.IServiceService `autowired:""`
|
||||
upstreamService upstream.IUpstreamService `autowired:""`
|
||||
transaction store.ITransaction `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlUpstreamModule) ExportAll(ctx context.Context) ([]*upstream_dto.ExportUpstream, error) {
|
||||
latestCommits, err := i.upstreamService.ListLatestCommit(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commitMap := utils.SliceToMap(latestCommits, func(c *commit.Commit[upstream.Config]) string {
|
||||
return c.Target
|
||||
})
|
||||
list, err := i.upstreamService.List(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]*upstream_dto.ExportUpstream, 0, len(list))
|
||||
for _, u := range list {
|
||||
c, ok := commitMap[u.UUID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
items = append(items, &upstream_dto.ExportUpstream{
|
||||
ID: u.UUID,
|
||||
Name: u.Name,
|
||||
Service: u.Service,
|
||||
Upstream: upstream_dto.FromClusterConfig(c.Data),
|
||||
})
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (i *imlUpstreamModule) Get(ctx context.Context, pid string) (upstream_dto.UpstreamConfig, error) {
|
||||
_, err := i.projectService.Check(ctx, pid, asServer)
|
||||
_, err := i.serviceService.Check(ctx, pid, asServer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -49,29 +81,29 @@ func (i *imlUpstreamModule) Get(ctx context.Context, pid string) (upstream_dto.U
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
return upstream_dto.FromClusterConfig(commit.Data), nil
|
||||
}
|
||||
|
||||
func (i *imlUpstreamModule) Save(ctx context.Context, pid string, upstreamConfig upstream_dto.UpstreamConfig) (upstream_dto.UpstreamConfig, error) {
|
||||
pInfo, err := i.projectService.Check(ctx, pid, asServer)
|
||||
pInfo, err := i.serviceService.Check(ctx, pid, asServer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
err = i.transaction.Transaction(ctx, func(ctx context.Context) error {
|
||||
err = i.upstreamService.SaveCommit(ctx, pid, cluster.DefaultClusterID, upstream_dto.ConvertUpstream(upstreamConfig))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
return i.upstreamService.Save(ctx, &upstream.SaveUpstream{
|
||||
UUID: pid,
|
||||
Name: fmt.Sprintf("upstream-%s", pid),
|
||||
Project: pid,
|
||||
Service: pid,
|
||||
Team: pInfo.Team,
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -2,20 +2,31 @@ package upstream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/APIParkLab/APIPark/module/system"
|
||||
"reflect"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
|
||||
|
||||
upstream_dto "github.com/APIParkLab/APIPark/module/upstream/dto"
|
||||
)
|
||||
|
||||
type IUpstreamModule interface {
|
||||
Get(ctx context.Context, pid string) (upstream_dto.UpstreamConfig, error)
|
||||
Save(ctx context.Context, pid string, upstream upstream_dto.UpstreamConfig) (upstream_dto.UpstreamConfig, error)
|
||||
//ExportAll(ctx context.Context) ([]*upstream_dto.ExportUpstream, error)
|
||||
}
|
||||
|
||||
type IExportUpstreamModule interface {
|
||||
system.IExportModule[upstream_dto.ExportUpstream]
|
||||
}
|
||||
|
||||
func init() {
|
||||
upstreamModule := new(imlUpstreamModule)
|
||||
autowire.Auto[IUpstreamModule](func() reflect.Value {
|
||||
return reflect.ValueOf(new(imlUpstreamModule))
|
||||
return reflect.ValueOf(upstreamModule)
|
||||
})
|
||||
|
||||
autowire.Auto[IExportUpstreamModule](func() reflect.Value {
|
||||
return reflect.ValueOf(upstreamModule)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user