From 079f9de92fac2484df5d6b7f630764f21329ceed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 3 Jan 2021 21:37:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/const/const.go | 2 +- internal/db/models/errors.go | 2 +- internal/nodes/rest_server.go | 20 +++++++++++++++++--- internal/rpc/services/service_base.go | 2 +- internal/rpc/services/service_ip_item.go | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/internal/const/const.go b/internal/const/const.go index e66ee7eb..b63e1767 100644 --- a/internal/const/const.go +++ b/internal/const/const.go @@ -1,7 +1,7 @@ package teaconst const ( - Version = "0.0.6.2" + Version = "0.0.7" ProductName = "Edge API" ProcessName = "edge-api" diff --git a/internal/db/models/errors.go b/internal/db/models/errors.go index 1257a2da..551c8efe 100644 --- a/internal/db/models/errors.go +++ b/internal/db/models/errors.go @@ -2,4 +2,4 @@ package models import "github.com/TeaOSLab/EdgeAPI/internal/errors" -var ErrNotFound = errors.New("not found") +var ErrNotFound = errors.New("resource not found") diff --git a/internal/nodes/rest_server.go b/internal/nodes/rest_server.go index 760689d0..e5d1fe5e 100644 --- a/internal/nodes/rest_server.go +++ b/internal/nodes/rest_server.go @@ -20,6 +20,7 @@ var servicePathReg = regexp.MustCompile(`^/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$`) var servicesMap = map[string]reflect.Value{ "APIAccessTokenService": reflect.ValueOf(new(services.APIAccessTokenService)), "HTTPAccessLogService": reflect.ValueOf(new(services.HTTPAccessLogService)), + "IPItemService": reflect.ValueOf(new(services.IPItemService)), } type RestServer struct{} @@ -43,6 +44,20 @@ func (this *RestServer) ListenHTTPS(listener net.Listener, tlsConfig *tls.Config func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) { path := req.URL.Path + + // 是否显示Pretty后的JSON + shouldPretty := req.Header.Get("Edge-Response-Pretty") == "on" + + // 欢迎页 + if path == "/" { + this.writeJSON(writer, maps.Map{ + "code": 200, + "message": "Welcome to API", + "data": maps.Map{}, + }, shouldPretty) + return + } + matches := servicePathReg.FindStringSubmatch(path) if len(matches) != 3 { writer.WriteHeader(http.StatusNotFound) @@ -72,9 +87,6 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) { return } - // 是否显示Pretty后的JSON - shouldPretty := req.Header.Get("Edge-Response-Pretty") == "on" - // 上下文 ctx := context.Background() @@ -181,6 +193,8 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) { } func (this *RestServer) writeJSON(writer http.ResponseWriter, v maps.Map, pretty bool) { + writer.Header().Set("Content-Type", "application/json") + if pretty { _, _ = writer.Write(v.AsPrettyJSON()) } else { diff --git a/internal/rpc/services/service_base.go b/internal/rpc/services/service_base.go index e85bb450..e8e5a74b 100644 --- a/internal/rpc/services/service_base.go +++ b/internal/rpc/services/service_base.go @@ -35,7 +35,7 @@ func (this *BaseService) ValidateAdminAndUser(ctx context.Context, requireAdminI switch reqUserType { case rpcutils.UserTypeAdmin: adminId = reqUserId - if adminId <= 0 { + if adminId < 0 { // 允许AdminId = 0 err = errors.New("invalid 'adminId'") return } diff --git a/internal/rpc/services/service_ip_item.go b/internal/rpc/services/service_ip_item.go index 6d66af6c..34a743df 100644 --- a/internal/rpc/services/service_ip_item.go +++ b/internal/rpc/services/service_ip_item.go @@ -3,8 +3,10 @@ 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" + "net" ) // IP条目相关服务 @@ -20,6 +22,22 @@ func (this *IPItemService) CreateIPItem(ctx context.Context, req *pb.CreateIPIte return nil, err } + if len(req.IpFrom) == 0 { + return nil, errors.New("'ipFrom' should not be empty") + } + + ipFrom := net.ParseIP(req.IpFrom) + if ipFrom == nil { + return nil, errors.New("invalid 'ipFrom'") + } + + if len(req.IpTo) > 0 { + ipTo := net.ParseIP(req.IpTo) + if ipTo == nil { + return nil, errors.New("invalid 'ipTo'") + } + } + tx := this.NullTx() if userId > 0 {