mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-30 09:56:35 +08:00
[API节点]支持HTTP API
This commit is contained in:
40
internal/rpc/services/service_api_access_token.go
Normal file
40
internal/rpc/services/service_api_access_token.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// AccessToken相关服务
|
||||
type APIAccessTokenService struct {
|
||||
}
|
||||
|
||||
// 获取AccessToken
|
||||
func (this *APIAccessTokenService) GetAPIAccessToken(ctx context.Context, req *pb.GetAPIAccessTokenRequest) (*pb.GetAPIAccessTokenResponse, error) {
|
||||
if req.Type == "user" { // 用户
|
||||
accessKey, err := models.SharedUserAccessKeyDAO.FindAccessKeyWithUniqueId(req.AccessKeyId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if accessKey == nil {
|
||||
return nil, errors.New("access key not found")
|
||||
}
|
||||
if accessKey.Secret != req.AccessKey {
|
||||
return nil, errors.New("access key not found")
|
||||
}
|
||||
|
||||
// 创建AccessToken
|
||||
token, expiresAt, err := models.SharedAPIAccessTokenDAO.GenerateAccessToken(int64(accessKey.UserId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.GetAPIAccessTokenResponse{
|
||||
Token: token,
|
||||
ExpiresAt: expiresAt,
|
||||
}, nil
|
||||
} else {
|
||||
return nil, errors.New("unsupported type '" + req.Type + "'")
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ func (this *APINodeService) CreateAPINode(ctx context.Context, req *pb.CreateAPI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nodeId, err := models.SharedAPINodeDAO.CreateAPINode(req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.AccessAddrsJSON, req.IsOn)
|
||||
nodeId, err := models.SharedAPINodeDAO.CreateAPINode(req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.RestIsOn, req.RestHTTPJSON, req.RestHTTPSJSON, req.AccessAddrsJSON, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func (this *APINodeService) UpdateAPINode(ctx context.Context, req *pb.UpdateAPI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedAPINodeDAO.UpdateAPINode(req.NodeId, req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.AccessAddrsJSON, req.IsOn)
|
||||
err = models.SharedAPINodeDAO.UpdateAPINode(req.NodeId, req.Name, req.Description, req.HttpJSON, req.HttpsJSON, req.RestIsOn, req.RestHTTPJSON, req.RestHTTPSJSON, req.AccessAddrsJSON, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -138,6 +138,9 @@ func (this *APINodeService) ListEnabledAPINodes(ctx context.Context, req *pb.Lis
|
||||
Description: node.Description,
|
||||
HttpJSON: []byte(node.Http),
|
||||
HttpsJSON: []byte(node.Https),
|
||||
RestIsOn: node.RestIsOn == 1,
|
||||
RestHTTPJSON: []byte(node.RestHTTP),
|
||||
RestHTTPSJSON: []byte(node.RestHTTPS),
|
||||
AccessAddrsJSON: []byte(node.AccessAddrs),
|
||||
AccessAddrs: accessAddrs,
|
||||
StatusJSON: []byte(node.Status),
|
||||
@@ -178,6 +181,9 @@ func (this *APINodeService) FindEnabledAPINode(ctx context.Context, req *pb.Find
|
||||
Description: node.Description,
|
||||
HttpJSON: []byte(node.Http),
|
||||
HttpsJSON: []byte(node.Https),
|
||||
RestIsOn: node.RestIsOn == 1,
|
||||
RestHTTPJSON: []byte(node.RestHTTP),
|
||||
RestHTTPSJSON: []byte(node.RestHTTPS),
|
||||
AccessAddrsJSON: []byte(node.AccessAddrs),
|
||||
AccessAddrs: accessAddrs,
|
||||
}
|
||||
|
||||
@@ -3,12 +3,14 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// 访问日志相关服务
|
||||
type HTTPAccessLogService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建访问日志
|
||||
@@ -34,11 +36,23 @@ func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req
|
||||
// 列出单页访问日志
|
||||
func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *pb.ListHTTPAccessLogsRequest) (*pb.ListHTTPAccessLogsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查服务ID
|
||||
if userId > 0 {
|
||||
if req.ServerId <= 0 {
|
||||
return nil, errors.New("invalid serverId")
|
||||
}
|
||||
|
||||
err = models.SharedServerDAO.CheckUserServer(req.ServerId, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
accessLogs, requestId, hasMore, err := models.SharedHTTPAccessLogDAO.ListAccessLogs(req.RequestId, req.Size, req.Day, req.ServerId, req.Reverse, req.HasError, req.FirewallPolicyId, req.FirewallRuleGroupId, req.FirewallRuleSetId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
37
internal/rpc/utils/plain_context.go
Normal file
37
internal/rpc/utils/plain_context.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package rpcutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PlainContext struct {
|
||||
UserType string
|
||||
UserId int64
|
||||
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewPlainContext(userType string, userId int64) *PlainContext {
|
||||
return &PlainContext{
|
||||
UserType: userType,
|
||||
UserId: userId,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *PlainContext) Deadline() (deadline time.Time, ok bool) {
|
||||
return this.ctx.Deadline()
|
||||
}
|
||||
|
||||
func (this *PlainContext) Done() <-chan struct{} {
|
||||
return this.ctx.Done()
|
||||
}
|
||||
|
||||
func (this *PlainContext) Err() error {
|
||||
return this.ctx.Err()
|
||||
}
|
||||
|
||||
func (this *PlainContext) Value(key interface{}) interface{} {
|
||||
return this.ctx.Value(key)
|
||||
}
|
||||
@@ -33,6 +33,29 @@ const (
|
||||
|
||||
// 校验请求
|
||||
func ValidateRequest(ctx context.Context, userTypes ...UserType) (userType UserType, userId int64, err error) {
|
||||
if ctx == nil {
|
||||
err = errors.New("context should not be nil")
|
||||
return
|
||||
}
|
||||
|
||||
// 支持直接认证
|
||||
plainCtx, ok := ctx.(*PlainContext)
|
||||
if ok {
|
||||
userType = plainCtx.UserType
|
||||
userId = plainCtx.UserId
|
||||
|
||||
if len(userTypes) > 0 && !lists.ContainsString(userTypes, userType) {
|
||||
userType = UserTypeNone
|
||||
userId = 0
|
||||
}
|
||||
|
||||
if userId <= 0 {
|
||||
err = errors.New("context: can not find user or permission denied")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return UserTypeNone, 0, errors.New("context: need 'nodeId'")
|
||||
|
||||
Reference in New Issue
Block a user