变更版本

This commit is contained in:
GoEdgeLab
2021-01-03 21:37:47 +08:00
parent 065ddbe734
commit b3cfb7803b
5 changed files with 38 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
package teaconst package teaconst
const ( const (
Version = "0.0.6.2" Version = "0.0.7"
ProductName = "Edge API" ProductName = "Edge API"
ProcessName = "edge-api" ProcessName = "edge-api"

View File

@@ -2,4 +2,4 @@ package models
import "github.com/TeaOSLab/EdgeAPI/internal/errors" import "github.com/TeaOSLab/EdgeAPI/internal/errors"
var ErrNotFound = errors.New("not found") var ErrNotFound = errors.New("resource not found")

View File

@@ -20,6 +20,7 @@ var servicePathReg = regexp.MustCompile(`^/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$`)
var servicesMap = map[string]reflect.Value{ var servicesMap = map[string]reflect.Value{
"APIAccessTokenService": reflect.ValueOf(new(services.APIAccessTokenService)), "APIAccessTokenService": reflect.ValueOf(new(services.APIAccessTokenService)),
"HTTPAccessLogService": reflect.ValueOf(new(services.HTTPAccessLogService)), "HTTPAccessLogService": reflect.ValueOf(new(services.HTTPAccessLogService)),
"IPItemService": reflect.ValueOf(new(services.IPItemService)),
} }
type RestServer struct{} 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) { func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) {
path := req.URL.Path 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) matches := servicePathReg.FindStringSubmatch(path)
if len(matches) != 3 { if len(matches) != 3 {
writer.WriteHeader(http.StatusNotFound) writer.WriteHeader(http.StatusNotFound)
@@ -72,9 +87,6 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) {
return return
} }
// 是否显示Pretty后的JSON
shouldPretty := req.Header.Get("Edge-Response-Pretty") == "on"
// 上下文 // 上下文
ctx := context.Background() 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) { func (this *RestServer) writeJSON(writer http.ResponseWriter, v maps.Map, pretty bool) {
writer.Header().Set("Content-Type", "application/json")
if pretty { if pretty {
_, _ = writer.Write(v.AsPrettyJSON()) _, _ = writer.Write(v.AsPrettyJSON())
} else { } else {

View File

@@ -35,7 +35,7 @@ func (this *BaseService) ValidateAdminAndUser(ctx context.Context, requireAdminI
switch reqUserType { switch reqUserType {
case rpcutils.UserTypeAdmin: case rpcutils.UserTypeAdmin:
adminId = reqUserId adminId = reqUserId
if adminId <= 0 { if adminId < 0 { // 允许AdminId = 0
err = errors.New("invalid 'adminId'") err = errors.New("invalid 'adminId'")
return return
} }

View File

@@ -3,8 +3,10 @@ package services
import ( import (
"context" "context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"net"
) )
// IP条目相关服务 // IP条目相关服务
@@ -20,6 +22,22 @@ func (this *IPItemService) CreateIPItem(ctx context.Context, req *pb.CreateIPIte
return nil, err 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() tx := this.NullTx()
if userId > 0 { if userId > 0 {