Skip to content

Commit 4f02cf8

Browse files
committed
补充API Root 测试用例
1 parent 7cdcd74 commit 4f02cf8

File tree

13 files changed

+140
-72
lines changed

13 files changed

+140
-72
lines changed

exception/base.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type APIException interface {
99
Meta() interface{}
1010
Is(code int) bool
1111
Namespace() string
12+
Reason() string
1213
}
1314

1415
// WithAPIException 携带元信息的异常
@@ -62,13 +63,6 @@ func (e *exception) Namespace() string {
6263
return e.namespace.String()
6364
}
6465

65-
// NewAPIException 创建一个API异常
66-
// 用于其他模块自定义异常
67-
func NewAPIException(code int, reason, format string, a ...interface{}) WithAPIException {
68-
// 0表示正常状态, 但是要排除变量的零值
69-
if code == 0 {
70-
code = -1
71-
}
72-
73-
return newException(usedNamespace, code, reason, format, a...)
66+
func (e *exception) Reason() string {
67+
return e.reason
7468
}

exception/exception.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package exception
22

3+
// NewAPIException 创建一个API异常
4+
// 用于其他模块自定义异常
5+
func NewAPIException(namespace string, code int, reason, format string, a ...interface{}) WithAPIException {
6+
// 0表示正常状态, 但是要排除变量的零值
7+
if code == 0 {
8+
code = -1
9+
}
10+
11+
return newException(Namespace(namespace), code, reason, format, a...)
12+
}
13+
314
// NewUnauthorized 未认证
415
func NewUnauthorized(format string, a ...interface{}) APIException {
516
return newException(usedNamespace, Unauthorized, codeReason(Unauthorized), format, a...)
@@ -37,5 +48,5 @@ func IsNotFoundError(err error) bool {
3748
return false
3849
}
3950

40-
return e.ErrorCode() == NotFound
51+
return e.ErrorCode() == NotFound && e.Namespace() == GlobalNamespace.String()
4152
}

exception/namespace.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,4 @@ func (ns Namespace) String() string {
1414
const (
1515
// GlobalNamespace 所有服务公用的一些异常
1616
GlobalNamespace = Namespace("global")
17-
// Keyauth 权限系统, 每个子系统有自己的异常空间
18-
Keyauth = Namespace("keyauth")
1917
)
20-
21-
// ChangeNameSapce 切换异常空间
22-
func ChangeNameSapce(ns Namespace) {
23-
usedNamespace = ns
24-
}

http/request/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
var (
13-
// BodyMaxContenxLength body 最大大小 默认 64M
13+
// BodyMaxContenxLength body最大大小 默认64M
1414
BodyMaxContenxLength int64 = 1 << 26
1515
)
1616

http/response/data.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package response
2+
3+
import "github.com/infraboard/mcube/http/request"
4+
5+
// Data to be used by controllers.
6+
type Data struct {
7+
Code *int `json:"code"` // 自定义返回码 0:表示正常
8+
Type string `json:"type,omitempty"` // 数据类型, 可以缺省
9+
Namespace string `json:"namespace,omitempty"` // 异常的范围
10+
Reason string `json:"reason,omitempty"` // 异常原因
11+
Message string `json:"message,omitempty"` // 关于这次响应的说明信息
12+
Data interface{} `json:"data,omitempty"` // 返回的具体数据
13+
}
14+
15+
// PageData 数据分页数据
16+
type PageData struct {
17+
*request.PageRequest
18+
TotalCount uint `json:"total"` // 总共多少条
19+
List interface{} `json:"list"` // 页面数据
20+
}
21+
22+
// NewData new实例
23+
func NewData(data interface{}) *Data {
24+
code := -1
25+
return &Data{
26+
Code: &code,
27+
Data: data,
28+
}
29+
}

http/response/receive.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
package response
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
9+
"github.com/infraboard/mcube/exception"
10+
)
11+
12+
// GetDataFromBody 获取body中的数据
13+
func GetDataFromBody(body io.ReadCloser, v interface{}) error {
14+
defer body.Close()
15+
16+
bytesB, err := ioutil.ReadAll(body)
17+
if err != nil {
18+
return err
19+
}
20+
data := NewData(v)
21+
22+
if err := json.Unmarshal(bytesB, data); err != nil {
23+
return err
24+
}
25+
26+
if data.Code == nil {
27+
return errors.New("reponse code is nil")
28+
}
29+
30+
if *data.Code != 0 {
31+
return exception.NewAPIException(data.Namespace, *data.Code, data.Reason, data.Message)
32+
}
33+
34+
return nil
35+
}

http/response/send.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,22 @@ import (
66
"net/http"
77

88
"github.com/infraboard/mcube/exception"
9-
"github.com/infraboard/mcube/http/request"
109
)
1110

12-
// Data to be used by controllers.
13-
type Data struct {
14-
Code *int `json:"code"` // 自定义返回码 0:表示正常
15-
Type string `json:"type,omitempty"` // 数据类型, 可以缺省
16-
Message string `json:"message,omitempty"` // 关于这次响应的说明信息
17-
Data interface{} `json:"data,omitempty"` // 返回的具体数据
18-
}
19-
20-
// PageData 数据分页数据
21-
type PageData struct {
22-
*request.PageRequest
23-
24-
TotalCount uint `json:"total"` // 总共多少条
25-
List interface{} `json:"list"` // 页面数据
26-
}
27-
2811
// Failed use to response error messge
2912
func Failed(w http.ResponseWriter, err error) {
3013
var (
3114
errCode int
3215
httpCode int
16+
ns string
17+
reason string
3318
)
3419

3520
switch t := err.(type) {
3621
case exception.APIException:
3722
errCode = t.ErrorCode()
23+
reason = t.Reason()
24+
ns = t.Namespace()
3825
default:
3926
errCode = exception.UnKnownException
4027
}
@@ -48,8 +35,10 @@ func Failed(w http.ResponseWriter, err error) {
4835
}
4936

5037
resp := Data{
51-
Code: &errCode,
52-
Message: err.Error(),
38+
Code: &errCode,
39+
Namespace: ns,
40+
Reason: reason,
41+
Message: err.Error(),
5342
}
5443

5544
// set response heanders

http/router/entry.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package router
2+
3+
// Entry 路由条目
4+
type Entry struct {
5+
Path string `json:"path,omitempty"`
6+
Method string `json:"method,omitempty"`
7+
FunctionName string `json:"function_name,omitempty"`
8+
Resource string `json:"resource,omitempty"`
9+
Protected bool `json:"protected"`
10+
Labels map[string]string `json:"labels,omitempty"`
11+
}
12+
13+
// NewEntrySet 实例
14+
func NewEntrySet() *EntrySet {
15+
return &EntrySet{}
16+
}
17+
18+
// EntrySet 路由条目集
19+
type EntrySet struct {
20+
Items []*Entry
21+
}
22+
23+
// AddEntry 添加Entry
24+
func (s *EntrySet) AddEntry(e Entry) {
25+
s.Items = append(s.Items, &e)
26+
}
27+
28+
// GetEntry 获取条目
29+
func (s *EntrySet) GetEntry(path, mothod string) *Entry {
30+
for i := range s.Items {
31+
item := s.Items[i]
32+
if item.Path == path && item.Method == mothod {
33+
return item
34+
}
35+
}
36+
37+
return nil
38+
}

http/router/httprouter/entriy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ func (s *entrySet) AddEntry(es ...*entry) error {
4040
}
4141

4242
// ShowEntries 显示理由条目
43-
func (s *entrySet) ShowEntries() []router.Entry {
44-
entries := make([]router.Entry, 0, len(s.items))
43+
func (s *entrySet) EntrieSet() *router.EntrySet {
44+
es := router.NewEntrySet()
4545
for _, v := range s.items {
46-
entries = append(entries, *v.Entry)
46+
es.AddEntry(*v.Entry)
4747
}
4848

49-
return entries
49+
return es
5050
}
5151

5252
// FindEntry 通过函数地址找到对于的路由条目

http/router/httprouter/httprouter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ func (r *httpRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
100100
r.r.ServeHTTP(response.NewResponse(w), req)
101101
}
102102

103-
func (r *httpRouter) GetEndpoints() []router.Entry {
104-
return r.entrySet.ShowEntries()
103+
func (r *httpRouter) GetEndpoints() *router.EntrySet {
104+
return r.entrySet.EntrieSet()
105105
}
106106

107107
func (r *httpRouter) EnableAPIRoot() {
108108
r.AddPublict("GET", "/", r.apiRoot)
109109
}
110110

111111
func (r *httpRouter) apiRoot(w http.ResponseWriter, req *http.Request) {
112-
response.Success(w, r.entrySet.ShowEntries())
112+
response.Success(w, r.entrySet.EntrieSet())
113113
return
114114
}
115115

0 commit comments

Comments
 (0)