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

305 lines
8.1 KiB
Go
Raw Normal View History

2020-09-15 14:44:11 +08:00
package services
import (
"context"
2020-09-21 19:51:56 +08:00
"encoding/json"
2020-09-15 14:44:11 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type HTTPWebService struct {
2020-11-24 15:02:44 +08:00
BaseService
2020-09-15 14:44:11 +08:00
}
// 创建Web配置
func (this *HTTPWebService) CreateHTTPWeb(ctx context.Context, req *pb.CreateHTTPWebRequest) (*pb.CreateHTTPWebResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
2020-09-26 11:21:43 +08:00
webId, err := models.SharedHTTPWebDAO.CreateWeb(req.RootJSON)
2020-09-15 14:44:11 +08:00
if err != nil {
return nil, err
}
return &pb.CreateHTTPWebResponse{WebId: webId}, nil
}
// 查找Web配置
func (this *HTTPWebService) FindEnabledHTTPWeb(ctx context.Context, req *pb.FindEnabledHTTPWebRequest) (*pb.FindEnabledHTTPWebResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
web, err := models.SharedHTTPWebDAO.FindEnabledHTTPWeb(req.WebId)
if err != nil {
return nil, err
}
if web == nil {
return &pb.FindEnabledHTTPWebResponse{Web: nil}, nil
}
result := &pb.HTTPWeb{}
result.Id = int64(web.Id)
2020-09-16 09:09:21 +08:00
result.IsOn = web.IsOn == 1
2020-09-15 14:44:11 +08:00
return &pb.FindEnabledHTTPWebResponse{Web: result}, nil
}
2020-09-21 19:51:56 +08:00
// 查找Web配置
func (this *HTTPWebService) FindEnabledHTTPWebConfig(ctx context.Context, req *pb.FindEnabledHTTPWebConfigRequest) (*pb.FindEnabledHTTPWebConfigResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
config, err := models.SharedHTTPWebDAO.ComposeWebConfig(req.WebId)
if err != nil {
return nil, err
}
configJSON, err := json.Marshal(config)
if err != nil {
return nil, err
}
return &pb.FindEnabledHTTPWebConfigResponse{WebJSON: configJSON}, nil
}
2020-09-15 14:44:11 +08:00
// 修改Web配置
func (this *HTTPWebService) UpdateHTTPWeb(ctx context.Context, req *pb.UpdateHTTPWebRequest) (*pb.RPCSuccess, error) {
2020-09-15 14:44:11 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
2020-09-26 11:21:43 +08:00
err = models.SharedHTTPWebDAO.UpdateWeb(req.WebId, req.RootJSON)
2020-09-15 14:44:11 +08:00
if err != nil {
return nil, err
}
return &pb.RPCSuccess{}, nil
2020-09-15 14:44:11 +08:00
}
2020-09-16 09:09:21 +08:00
// 修改Gzip配置
func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.UpdateHTTPWebGzipRequest) (*pb.RPCSuccess, error) {
2020-09-16 09:09:21 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
2020-09-20 16:28:07 +08:00
err = models.SharedHTTPWebDAO.UpdateWebGzip(req.WebId, req.GzipJSON)
2020-09-16 09:09:21 +08:00
if err != nil {
return nil, err
}
return &pb.RPCSuccess{}, nil
2020-09-16 09:09:21 +08:00
}
2020-09-16 20:29:18 +08:00
// 修改字符集配置
func (this *HTTPWebService) UpdateHTTPWebCharset(ctx context.Context, req *pb.UpdateHTTPWebCharsetRequest) (*pb.RPCSuccess, error) {
2020-09-16 20:29:18 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
2020-09-23 18:43:42 +08:00
err = models.SharedHTTPWebDAO.UpdateWebCharset(req.WebId, req.CharsetJSON)
2020-09-16 20:29:18 +08:00
if err != nil {
return nil, err
}
return &pb.RPCSuccess{}, nil
2020-09-16 20:29:18 +08:00
}
// 更改请求Header策略
func (this *HTTPWebService) UpdateHTTPWebRequestHeader(ctx context.Context, req *pb.UpdateHTTPWebRequestHeaderRequest) (*pb.RPCSuccess, error) {
2020-09-16 20:29:18 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
2020-09-23 18:43:42 +08:00
err = models.SharedHTTPWebDAO.UpdateWebRequestHeaderPolicy(req.WebId, req.HeaderJSON)
2020-09-16 20:29:18 +08:00
if err != nil {
return nil, err
}
return &pb.RPCSuccess{}, nil
2020-09-16 20:29:18 +08:00
}
// 更改响应Header策略
func (this *HTTPWebService) UpdateHTTPWebResponseHeader(ctx context.Context, req *pb.UpdateHTTPWebResponseHeaderRequest) (*pb.RPCSuccess, error) {
2020-09-16 20:29:18 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
2020-09-23 18:43:42 +08:00
err = models.SharedHTTPWebDAO.UpdateWebResponseHeaderPolicy(req.WebId, req.HeaderJSON)
2020-09-16 20:29:18 +08:00
if err != nil {
return nil, err
}
return &pb.RPCSuccess{}, nil
2020-09-17 10:16:00 +08:00
}
2020-09-16 20:29:18 +08:00
2020-09-17 10:16:00 +08:00
// 更改Shutdown
func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.UpdateHTTPWebShutdownRequest) (*pb.RPCSuccess, error) {
2020-09-17 10:16:00 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebShutdown(req.WebId, req.ShutdownJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-17 10:16:00 +08:00
}
// 更改Pages
func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.UpdateHTTPWebPagesRequest) (*pb.RPCSuccess, error) {
2020-09-17 10:16:00 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebPages(req.WebId, req.PagesJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-16 20:29:18 +08:00
}
2020-09-20 11:56:30 +08:00
// 更改访问日志配置
func (this *HTTPWebService) UpdateHTTPWebAccessLog(ctx context.Context, req *pb.UpdateHTTPWebAccessLogRequest) (*pb.RPCSuccess, error) {
2020-09-20 11:56:30 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebAccessLogConfig(req.WebId, req.AccessLogJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-20 11:56:30 +08:00
}
2020-09-20 14:48:41 +08:00
// 更改统计配置
func (this *HTTPWebService) UpdateHTTPWebStat(ctx context.Context, req *pb.UpdateHTTPWebStatRequest) (*pb.RPCSuccess, error) {
2020-09-20 14:48:41 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebStat(req.WebId, req.StatJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-20 14:48:41 +08:00
}
2020-09-20 16:28:07 +08:00
// 更改缓存配置
func (this *HTTPWebService) UpdateHTTPWebCache(ctx context.Context, req *pb.UpdateHTTPWebCacheRequest) (*pb.RPCSuccess, error) {
2020-09-20 16:28:07 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebCache(req.WebId, req.CacheJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-20 16:28:07 +08:00
}
2020-09-20 20:12:47 +08:00
// 更改防火墙设置
func (this *HTTPWebService) UpdateHTTPWebFirewall(ctx context.Context, req *pb.UpdateHTTPWebFirewallRequest) (*pb.RPCSuccess, error) {
2020-09-20 20:12:47 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebFirewall(req.WebId, req.FirewallJSON)
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 *HTTPWebService) UpdateHTTPWebLocations(ctx context.Context, req *pb.UpdateHTTPWebLocationsRequest) (*pb.RPCSuccess, error) {
2020-09-21 19:51:56 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebLocations(req.WebId, req.LocationsJSON)
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
}
2020-09-23 18:43:42 +08:00
2020-09-26 19:54:15 +08:00
// 更改跳转到HTTPS设置
func (this *HTTPWebService) UpdateHTTPWebRedirectToHTTPS(ctx context.Context, req *pb.UpdateHTTPWebRedirectToHTTPSRequest) (*pb.RPCSuccess, error) {
2020-09-23 18:43:42 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebRedirectToHTTPS(req.WebId, req.RedirectToHTTPSJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-23 18:43:42 +08:00
}
2020-09-26 19:54:15 +08:00
// 更改Websocket设置
func (this *HTTPWebService) UpdateHTTPWebWebsocket(ctx context.Context, req *pb.UpdateHTTPWebWebsocketRequest) (*pb.RPCSuccess, error) {
2020-09-26 19:54:15 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebsocket(req.WebId, req.WebsocketJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-26 19:54:15 +08:00
}
2020-09-28 16:25:39 +08:00
// 更改重写规则设置
func (this *HTTPWebService) UpdateHTTPWebRewriteRules(ctx context.Context, req *pb.UpdateHTTPWebRewriteRulesRequest) (*pb.RPCSuccess, error) {
2020-09-28 16:25:39 +08:00
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebRewriteRules(req.WebId, req.RewriteRulesJSON)
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-09-28 16:25:39 +08:00
}