Skip to content

Commit 3bb7357

Browse files
committed
add gin and echo
1 parent 6c28d4f commit 3bb7357

18 files changed

+1182
-112
lines changed

.gitignore

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,2 @@
1-
# Binaries for programs and plugins
2-
*.exe
3-
*.exe~
4-
*.dll
5-
*.so
6-
*.dylib
7-
8-
# Test binary, build with `go test -c`
9-
*.test
10-
11-
# Output of the go coverage tool, specifically when used with LiteIDE
12-
*.out
13-
14-
.vscode
15-
.idea
1+
*.idea
2+
*.vscode

appr_handler.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package diff
22

33
import (
44
"context"
5-
"encoding/json"
6-
"io"
75
"net/http"
86
"reflect"
97
)
@@ -43,9 +41,9 @@ func NewApprHandlerWithKeysAndLog(apprService ApprService, keys []string, modelT
4341
offset = 1
4442
}
4543
if keys == nil || len(keys) == 0 {
46-
keys = getJsonPrimaryKeys(modelType)
44+
keys = GetJsonPrimaryKeys(modelType)
4745
}
48-
indexes := getIndexes(modelType)
46+
indexes := GetIndexes(modelType)
4947
var resource, action1, action2 string
5048
if len(options) > 0 && len(options[0]) > 0 {
5149
action1 = options[0]
@@ -60,48 +58,33 @@ func NewApprHandlerWithKeysAndLog(apprService ApprService, keys []string, modelT
6058
if len(options) > 2 && len(options[2]) > 0 {
6159
resource = options[2]
6260
} else {
63-
resource = buildResourceName(modelType.Name())
61+
resource = BuildResourceName(modelType.Name())
6462
}
6563
return &ApprHandler{Log: writeLog, ApprService: apprService, ModelType: modelType, Keys: keys, Indexes: indexes, Offset: offset, Error: logError, Resource: resource, Action1: action1, Action2: action2}
6664
}
6765

68-
func (c *ApprHandler) newModel(body interface{}) (out interface{}) {
69-
req := reflect.New(c.ModelType).Interface()
70-
if body != nil {
71-
switch s := body.(type) {
72-
case io.Reader:
73-
err := json.NewDecoder(s).Decode(&req)
74-
if err != nil {
75-
return err
76-
}
77-
return req
78-
}
79-
}
80-
return req
81-
}
82-
8366
func (c *ApprHandler) Approve(w http.ResponseWriter, r *http.Request) {
84-
id, err := buildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
85-
if err != nil {
86-
http.Error(w, err.Error(), http.StatusBadRequest)
67+
id, er1 := BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
68+
if er1 != nil {
69+
http.Error(w, er1.Error(), http.StatusBadRequest)
8770
} else {
88-
result, err := c.ApprService.Approve(r.Context(), id)
89-
if err != nil {
90-
handleError(w, r, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action1, err, c.Log)
71+
result, er2 := c.ApprService.Approve(r.Context(), id)
72+
if er2 != nil {
73+
handleError(w, r, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action1, er2, c.Log)
9174
} else {
9275
succeed(w, r, http.StatusOK, result, c.Log, c.Resource, c.Action1)
9376
}
9477
}
9578
}
9679

9780
func (c *ApprHandler) Reject(w http.ResponseWriter, r *http.Request) {
98-
id, err := buildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
99-
if err != nil {
100-
http.Error(w, err.Error(), http.StatusBadRequest)
81+
id, er1 := BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
82+
if er1 != nil {
83+
http.Error(w, er1.Error(), http.StatusBadRequest)
10184
} else {
102-
result, err := c.ApprService.Reject(r.Context(), id)
103-
if err != nil {
104-
handleError(w, r, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action2, err, c.Log)
85+
result, er2 := c.ApprService.Reject(r.Context(), id)
86+
if er2 != nil {
87+
handleError(w, r, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action2, er2, c.Log)
10588
} else {
10689
succeed(w, r, http.StatusOK, result, c.Log, c.Resource, c.Action2)
10790
}

appr_list_handler.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewApprListHandler(apprListService ApprListService, modelType reflect.Type,
2323

2424
func NewApprListHandlerWithKeys(apprListService ApprListService, keys []string, modelType reflect.Type, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprListHandler {
2525
if keys == nil || len(keys) == 0 {
26-
keys = getJsonPrimaryKeys(modelType)
26+
keys = GetJsonPrimaryKeys(modelType)
2727
}
2828
var resource, action1, action2 string
2929
if len(options) > 0 && len(options[0]) > 0 {
@@ -39,33 +39,33 @@ func NewApprListHandlerWithKeys(apprListService ApprListService, keys []string,
3939
if len(options) > 2 && len(options[2]) > 0 {
4040
resource = options[2]
4141
} else {
42-
resource = buildResourceName(modelType.Name())
42+
resource = BuildResourceName(modelType.Name())
4343
}
4444
return &ApprListHandler{ApprListService: apprListService, ModelType: modelType, Keys: keys, Resource: resource, Error: logError, Log: writeLog, Action1: action1, Action2: action2}
4545
}
4646

4747
func (c *ApprListHandler) Approve(w http.ResponseWriter, r *http.Request) {
48-
ids, err := buildIds(r, c.ModelType, c.Keys)
49-
if err != nil {
50-
http.Error(w, err.Error(), http.StatusBadRequest)
48+
ids, er1 := BuildIds(r, c.ModelType, c.Keys)
49+
if er1 != nil {
50+
http.Error(w, er1.Error(), http.StatusBadRequest)
5151
} else {
52-
result, err := c.ApprListService.Approve(r.Context(), ids)
53-
if err != nil {
54-
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action1, err, c.Log)
52+
result, er2 := c.ApprListService.Approve(r.Context(), ids)
53+
if er2 != nil {
54+
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action1, er2, c.Log)
5555
} else {
5656
succeed(w, r, http.StatusOK, result, c.Log, c.Resource, c.Action1)
5757
}
5858
}
5959
}
6060

6161
func (c *ApprListHandler) Reject(w http.ResponseWriter, r *http.Request) {
62-
ids, err := buildIds(r, c.ModelType, c.Keys)
63-
if err != nil {
64-
http.Error(w, err.Error(), http.StatusBadRequest)
62+
ids, er1 := BuildIds(r, c.ModelType, c.Keys)
63+
if er1 != nil {
64+
http.Error(w, er1.Error(), http.StatusBadRequest)
6565
} else {
66-
result, err := c.ApprListService.Reject(r.Context(), ids)
67-
if err != nil {
68-
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action2, err, c.Log)
66+
result, er2 := c.ApprListService.Reject(r.Context(), ids)
67+
if er2 != nil {
68+
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action2, er2, c.Log)
6969
} else {
7070
succeed(w, r, http.StatusOK, result, c.Log, c.Resource, c.Action2)
7171
}

diff_handler.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ func NewDiffHandlerWithKeys(diff func(context.Context, interface{}) (*DiffModel,
3636
offset = options[0]
3737
}
3838
if keys == nil || len(keys) == 0 {
39-
keys = getJsonPrimaryKeys(modelType)
39+
keys = GetJsonPrimaryKeys(modelType)
4040
}
41-
indexes := getIndexes(modelType)
41+
indexes := GetIndexes(modelType)
4242
var resource, action string
4343
if config != nil {
4444
resource = config.Resource
4545
action = config.Action
4646
}
4747
if len(resource) == 0 {
48-
resource = buildResourceName(modelType.Name())
48+
resource = BuildResourceName(modelType.Name())
4949
}
5050
if len(action) == 0 {
5151
action = "diff"
@@ -54,13 +54,13 @@ func NewDiffHandlerWithKeys(diff func(context.Context, interface{}) (*DiffModel,
5454
}
5555

5656
func (c *DiffHandler) Diff(w http.ResponseWriter, r *http.Request) {
57-
id, err := buildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
58-
if err != nil {
59-
http.Error(w, err.Error(), http.StatusBadRequest)
57+
id, er1 := BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
58+
if er1 != nil {
59+
http.Error(w, er1.Error(), http.StatusBadRequest)
6060
} else {
61-
result, err := c.GetDiff(r.Context(), id)
62-
if err != nil {
63-
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, err, c.Log)
61+
result, er2 := c.GetDiff(r.Context(), id)
62+
if er2 != nil {
63+
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, er2, c.Log)
6464
} else {
6565
if c.Config == nil {
6666
succeed(w, r, http.StatusOK, result, c.Log, c.Resource, c.Action)

diff_list_handler.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"net/http"
66
"reflect"
7-
"strings"
87
)
98

109
type DiffListHandler struct {
@@ -24,16 +23,16 @@ func NewDiffListHandler(diff func(context.Context, interface{}) (*[]DiffModel, e
2423
}
2524
func NewDiffListHandlerWithKeys(diff func(context.Context, interface{}) (*[]DiffModel, error), keys []string, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error) *DiffListHandler {
2625
if keys == nil || len(keys) == 0 {
27-
keys = getJsonPrimaryKeys(modelType)
26+
keys = GetJsonPrimaryKeys(modelType)
2827
}
29-
modelTypeId := newModelTypeID(modelType, keys)
28+
modelTypeId := NewModelTypeID(modelType, keys)
3029
var resource, action string
3130
if config != nil {
3231
resource = config.Resource
3332
action = config.Action
3433
}
3534
if len(resource) == 0 {
36-
resource = buildResourceName(modelType.Name())
35+
resource = BuildResourceName(modelType.Name())
3736
}
3837
if len(action) == 0 {
3938
action = "diff"
@@ -42,13 +41,13 @@ func NewDiffListHandlerWithKeys(diff func(context.Context, interface{}) (*[]Diff
4241
}
4342

4443
func (c *DiffListHandler) DiffList(w http.ResponseWriter, r *http.Request) {
45-
ids, err := buildIds(r, c.modelTypeId, c.Keys)
46-
if err != nil {
47-
http.Error(w, err.Error(), http.StatusBadRequest)
44+
ids, er1 := BuildIds(r, c.modelTypeId, c.Keys)
45+
if er1 != nil {
46+
http.Error(w, er1.Error(), http.StatusBadRequest)
4847
} else {
49-
list, err := c.GetDiff(r.Context(), ids)
50-
if err != nil {
51-
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, err, c.Log)
48+
list, er2 := c.GetDiff(r.Context(), ids)
49+
if er2 != nil {
50+
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, er2, c.Log)
5251
} else {
5352
if c.Config == nil || list == nil || len(*list) == 0 {
5453
succeed(w, r, http.StatusOK, list, c.Log, c.Resource, c.Action)
@@ -75,30 +74,3 @@ func (c *DiffListHandler) DiffList(w http.ResponseWriter, r *http.Request) {
7574
}
7675
}
7776
}
78-
79-
func newModelTypeID(modelType reflect.Type, idJsonNames []string) reflect.Type {
80-
model := reflect.New(modelType).Interface()
81-
value := reflect.Indirect(reflect.ValueOf(model))
82-
sf := make([]reflect.StructField, 0)
83-
for i := 0; i < modelType.NumField(); i++ {
84-
sf = append(sf, modelType.Field(i))
85-
field := modelType.Field(i)
86-
json := field.Tag.Get("json")
87-
s := strings.Split(json, ",")[0]
88-
if find(idJsonNames, s) == false {
89-
sf[i].Tag = `json:"-"`
90-
}
91-
}
92-
newType := reflect.StructOf(sf)
93-
newValue := value.Convert(newType)
94-
return reflect.TypeOf(newValue.Interface())
95-
}
96-
97-
func find(slice []string, val string) bool {
98-
for _, item := range slice {
99-
if item == val {
100-
return true
101-
}
102-
}
103-
return false
104-
}

echo/appr_handler.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package echo
2+
3+
import (
4+
"context"
5+
d "github.com/core-go/diff"
6+
"github.com/labstack/echo/v4"
7+
"net/http"
8+
"reflect"
9+
)
10+
11+
const internalServerError = "Internal Server Error"
12+
13+
type ApprHandler struct {
14+
ApprService d.ApprService
15+
Keys []string
16+
ModelType reflect.Type
17+
Error func(context.Context, string)
18+
Indexes map[string]int
19+
Offset int
20+
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
21+
Resource string
22+
Action1 string
23+
Action2 string
24+
}
25+
26+
func NewApprHandler(apprService d.ApprService, modelType reflect.Type, logError func(context.Context, string), option ...int) *ApprHandler {
27+
offset := 1
28+
if len(option) > 0 && option[0] >= 0 {
29+
offset = option[0]
30+
}
31+
return NewApprHandlerWithKeysAndLog(apprService, nil, modelType, offset, logError, nil)
32+
}
33+
func NewApprHandlerWithLogs(apprService d.ApprService, modelType reflect.Type, offset int, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler {
34+
return NewApprHandlerWithKeysAndLog(apprService, nil, modelType, offset, logError, writeLog, options...)
35+
}
36+
func NewApprHandlerWithKeys(apprService d.ApprService, modelType reflect.Type, logError func(context.Context, string), idNames []string, option ...int) *ApprHandler {
37+
offset := 1
38+
if len(option) > 0 && option[0] >= 0 {
39+
offset = option[0]
40+
}
41+
return NewApprHandlerWithKeysAndLog(apprService, idNames, modelType, offset, logError, nil)
42+
}
43+
func NewApprHandlerWithKeysAndLog(apprService d.ApprService, keys []string, modelType reflect.Type, offset int, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler {
44+
if offset < 0 {
45+
offset = 1
46+
}
47+
if keys == nil || len(keys) == 0 {
48+
keys = d.GetJsonPrimaryKeys(modelType)
49+
}
50+
indexes := d.GetIndexes(modelType)
51+
var resource, action1, action2 string
52+
if len(options) > 0 && len(options[0]) > 0 {
53+
action1 = options[0]
54+
} else {
55+
action1 = "approve"
56+
}
57+
if len(options) > 1 && len(options[1]) > 0 {
58+
action2 = options[1]
59+
} else {
60+
action2 = "reject"
61+
}
62+
if len(options) > 2 && len(options[2]) > 0 {
63+
resource = options[2]
64+
} else {
65+
resource = d.BuildResourceName(modelType.Name())
66+
}
67+
return &ApprHandler{Log: writeLog, ApprService: apprService, ModelType: modelType, Keys: keys, Indexes: indexes, Offset: offset, Error: logError, Resource: resource, Action1: action1, Action2: action2}
68+
}
69+
70+
func (c *ApprHandler) Approve() echo.HandlerFunc {
71+
return func(ctx echo.Context) error {
72+
r := ctx.Request()
73+
id, er1 := d.BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
74+
if er1 != nil {
75+
ctx.String(http.StatusBadRequest, er1.Error())
76+
return er1
77+
} else {
78+
result, er2 := c.ApprService.Approve(r.Context(), id)
79+
if er2 != nil {
80+
return handleError(ctx, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action1, er2, c.Log)
81+
} else {
82+
return succeed(ctx, http.StatusOK, result, c.Log, c.Resource, c.Action1)
83+
}
84+
}
85+
}
86+
}
87+
88+
func (c *ApprHandler) Reject() echo.HandlerFunc {
89+
return func(ctx echo.Context) error {
90+
r := ctx.Request()
91+
id, er1 := d.BuildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
92+
if er1 != nil {
93+
ctx.String(http.StatusBadRequest, er1.Error())
94+
return er1
95+
} else {
96+
result, er2 := c.ApprService.Reject(r.Context(), id)
97+
if er2 != nil {
98+
return handleError(ctx, http.StatusOK, internalServerError, c.Error, c.Resource, c.Action2, er2, c.Log)
99+
} else {
100+
return succeed(ctx, http.StatusOK, result, c.Log, c.Resource, c.Action2)
101+
}
102+
}
103+
}
104+
}
105+
106+
func respond(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) error {
107+
err := ctx.JSON(code, result)
108+
if writeLog != nil {
109+
writeLog(ctx.Request().Context(), resource, action, success, desc)
110+
}
111+
return err
112+
}
113+
func handleError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string), resource string, action string, err error, writeLog func(context.Context, string, string, bool, string) error) error {
114+
if logError != nil {
115+
logError(ctx.Request().Context(), err.Error())
116+
}
117+
respond(ctx, code, result, writeLog, resource, action, false, err.Error())
118+
return err
119+
}
120+
func succeed(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error {
121+
return respond(ctx, code, result, writeLog, resource, action, true, "")
122+
}

0 commit comments

Comments
 (0)