Files
EdgeAPI/internal/rpc/services/service_http_location.go

198 lines
5.6 KiB
Go
Raw Normal View History

2020-09-21 19:51:56 +08:00
package services
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
type HTTPLocationService struct {
2020-11-24 15:02:44 +08:00
BaseService
2020-09-21 19:51:56 +08:00
}
// 创建路径规则
func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb.CreateHTTPLocationRequest) (*pb.CreateHTTPLocationResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
tx := this.NullTx()
locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
return &pb.CreateHTTPLocationResponse{LocationId: locationId}, nil
}
// 修改路径规则
func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb.UpdateHTTPLocationRequest) (*pb.RPCSuccess, error) {
2020-09-21 19:51:56 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
tx := this.NullTx()
err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-21 19:51:56 +08:00
}
// 查找路径规则配置
func (this *HTTPLocationService) FindEnabledHTTPLocationConfig(ctx context.Context, req *pb.FindEnabledHTTPLocationConfigRequest) (*pb.FindEnabledHTTPLocationConfigResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
tx := this.NullTx()
config, err := models.SharedHTTPLocationDAO.ComposeLocationConfig(tx, req.LocationId)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
configJSON, err := json.Marshal(config)
if err != nil {
return nil, err
}
return &pb.FindEnabledHTTPLocationConfigResponse{LocationJSON: configJSON}, nil
}
// 删除路径规则
func (this *HTTPLocationService) DeleteHTTPLocation(ctx context.Context, req *pb.DeleteHTTPLocationRequest) (*pb.RPCSuccess, error) {
2020-09-21 19:51:56 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
tx := this.NullTx()
err = models.SharedHTTPLocationDAO.DisableHTTPLocation(tx, req.LocationId)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-21 19:51:56 +08:00
}
// 查找反向代理设置
func (this *HTTPLocationService) FindAndInitHTTPLocationReverseProxyConfig(ctx context.Context, req *pb.FindAndInitHTTPLocationReverseProxyConfigRequest) (*pb.FindAndInitHTTPLocationReverseProxyConfigResponse, error) {
// 校验请求
2020-12-18 21:18:53 +08:00
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
reverseProxyRef, err := models.SharedHTTPLocationDAO.FindLocationReverseProxy(tx, req.LocationId)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
if reverseProxyRef == nil || reverseProxyRef.ReverseProxyId <= 0 {
reverseProxyId, err := models.SharedReverseProxyDAO.CreateReverseProxy(tx, adminId, userId, nil, nil, nil)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
reverseProxyRef = &serverconfigs.ReverseProxyRef{
IsOn: false,
ReverseProxyId: reverseProxyId,
}
2020-09-22 11:36:45 +08:00
reverseProxyJSON, err := json.Marshal(reverseProxyRef)
if err != nil {
return nil, err
}
err = models.SharedHTTPLocationDAO.UpdateLocationReverseProxy(tx, req.LocationId, reverseProxyJSON)
2020-09-22 11:36:45 +08:00
if err != nil {
return nil, err
}
2020-09-21 19:51:56 +08:00
}
reverseProxyConfig, err := models.SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId)
2020-09-21 19:51:56 +08:00
if err != nil {
return nil, err
}
refJSON, err := json.Marshal(reverseProxyRef)
if err != nil {
return nil, err
}
configJSON, err := json.Marshal(reverseProxyConfig)
if err != nil {
return nil, err
}
return &pb.FindAndInitHTTPLocationReverseProxyConfigResponse{
ReverseProxyJSON: configJSON,
ReverseProxyRefJSON: refJSON,
}, nil
}
// 初始化Web设置
func (this *HTTPLocationService) FindAndInitHTTPLocationWebConfig(ctx context.Context, req *pb.FindAndInitHTTPLocationWebConfigRequest) (*pb.FindAndInitHTTPLocationWebConfigResponse, error) {
// 校验请求
2020-12-18 21:18:53 +08:00
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
2020-09-21 19:51:56 +08:00
if err != nil {
2020-09-26 19:54:15 +08:00
return nil, rpcutils.Wrap("ValidateRequest()", err)
2020-09-21 19:51:56 +08:00
}
tx := this.NullTx()
webId, err := models.SharedHTTPLocationDAO.FindLocationWebId(tx, req.LocationId)
2020-09-21 19:51:56 +08:00
if err != nil {
2020-09-26 19:54:15 +08:00
return nil, rpcutils.Wrap("FindLocationWebId()", err)
2020-09-21 19:51:56 +08:00
}
if webId <= 0 {
webId, err = models.SharedHTTPWebDAO.CreateWeb(tx, adminId, userId, nil)
2020-09-21 19:51:56 +08:00
if err != nil {
2020-09-26 19:54:15 +08:00
return nil, rpcutils.Wrap("CreateWeb()", err)
2020-09-21 19:51:56 +08:00
}
err = models.SharedHTTPLocationDAO.UpdateLocationWeb(tx, req.LocationId, webId)
2020-09-22 11:36:45 +08:00
if err != nil {
2020-09-26 19:54:15 +08:00
return nil, rpcutils.Wrap("UpdateLocationWeb()", err)
2020-09-22 11:36:45 +08:00
}
2020-09-21 19:51:56 +08:00
}
config, err := models.SharedHTTPWebDAO.ComposeWebConfig(tx, webId)
2020-09-21 19:51:56 +08:00
if err != nil {
2020-09-26 19:54:15 +08:00
return nil, rpcutils.Wrap("ComposeWebConfig()", err)
2020-09-21 19:51:56 +08:00
}
configJSON, err := json.Marshal(config)
if err != nil {
2020-09-26 19:54:15 +08:00
return nil, rpcutils.Wrap("json.Marshal()", err)
2020-09-21 19:51:56 +08:00
}
return &pb.FindAndInitHTTPLocationWebConfigResponse{
WebJSON: configJSON,
}, nil
}
2020-09-22 11:36:45 +08:00
// 修改反向代理设置
func (this *HTTPLocationService) UpdateHTTPLocationReverseProxy(ctx context.Context, req *pb.UpdateHTTPLocationReverseProxyRequest) (*pb.RPCSuccess, error) {
2020-09-22 11:36:45 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
tx := this.NullTx()
err = models.SharedHTTPLocationDAO.UpdateLocationReverseProxy(tx, req.LocationId, req.ReverseProxyJSON)
2020-09-22 11:36:45 +08:00
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-22 11:36:45 +08:00
}