72 lines
1.7 KiB
Go
Raw Permalink Normal View History

2025-03-16 15:26:12 +08:00
package permission
import (
"context"
2025-03-23 10:39:44 +08:00
"github.com/infraboard/mcube/v2/exception"
2025-03-16 15:26:12 +08:00
"github.com/infraboard/mcube/v2/ioc"
2025-03-23 10:39:44 +08:00
"github.com/infraboard/mcube/v2/ioc/config/application"
2025-03-16 15:26:12 +08:00
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
2025-03-23 10:39:44 +08:00
"github.com/infraboard/mcube/v2/types"
2025-03-16 15:26:12 +08:00
"github.com/infraboard/modules/iam/apps/endpoint"
2025-03-23 10:39:44 +08:00
"resty.dev/v3"
2025-03-16 15:26:12 +08:00
)
func init() {
ioc.Api().Registry(&ApiRegister{})
}
2025-03-23 10:39:44 +08:00
func GetApiRegister() *ApiRegister {
return ioc.Api().Get("api_register").(*ApiRegister)
}
2025-03-16 15:26:12 +08:00
type ApiRegister struct {
ioc.ObjectImpl
2025-03-23 10:39:44 +08:00
}
func (c *ApiRegister) Name() string {
return "api_register"
}
func (i *ApiRegister) Priority() int {
return -100
2025-03-16 15:26:12 +08:00
}
func (a *ApiRegister) Init() error {
// 注册认证中间件
entries := endpoint.NewEntryFromRestfulContainer(gorestful.RootRouter())
req := endpoint.NewRegistryEndpointRequest()
req.AddItem(entries...)
2025-03-23 10:39:44 +08:00
_, err := a.RegistryEndpoint(context.Background(), req)
2025-03-16 15:26:12 +08:00
if err != nil {
return err
}
return nil
}
2025-03-23 10:39:44 +08:00
// 注册API接口(RPC --> REST SDK)
// 自己的 注册API接口
// restful client: github.com/go-resty/resty/v2
// http://127.0.0.1:8020/api/mcenter/v1/endpoint
func (a *ApiRegister) RegistryEndpoint(ctx context.Context, in *endpoint.RegistryEndpointRequest) (*types.Set[*endpoint.Endpoint], error) {
set := types.New[*endpoint.Endpoint]()
resp, err := resty.New().
SetDebug(true).
SetBaseURL(application.Get().InternalAddress).
SetAuthToken(application.Get().InternalToken).
R().
WithContext(ctx).
SetContentType("application/json").
SetBody(in.Items).
SetResult(set).
Post("/api/mcenter/v1/endpoint")
if err != nil {
return nil, err
}
if resp.StatusCode()/100 != 2 {
return nil, exception.NewPermissionDeny("[%d] API注册异常: %s", resp.StatusCode(), resp.String())
}
return set, nil
2025-03-16 15:26:12 +08:00
}