Skip to content

Commit 39a7747

Browse files
committed
Add gin and echo
1 parent 3673f73 commit 39a7747

File tree

6 files changed

+395
-38
lines changed

6 files changed

+395
-38
lines changed

.gitignore

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,2 @@
1-
# Binaries for programs and plugins
2-
*.exe
3-
*.exe~
4-
*.dll
5-
*.so
6-
*.dylib
7-
8-
# Test binary, built with `go test -c`
9-
*.test
10-
11-
# Output of the go coverage tool, specifically when used with LiteIDE
12-
*.out
13-
14-
# Dependency directories (remove the comment below to include it)
15-
vendor/
16-
17-
# Version control
18-
.vscode
19-
.idea
1+
*.idea
2+
*.vscode

code.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
const (
14-
DriverPostgres = "postgres"
15-
DriverMysql = "mysql"
16-
DriverMssql = "mssql"
17-
DriverOracle = "oracle"
18-
DriverSqlite3 = "sqlite3"
19-
DriverNotSupport = "no support"
14+
driverPostgres = "postgres"
15+
driverMysql = "mysql"
16+
driverMssql = "mssql"
17+
driverOracle = "oracle"
18+
driverSqlite3 = "sqlite3"
19+
driverNotSupport = "no support"
2020
)
2121

2222
type Model struct {
@@ -68,7 +68,7 @@ func NewDefaultDynamicSqlCodeLoader(db *sql.DB, query string, options ...int) *D
6868
func NewDynamicSqlCodeLoader(db *sql.DB, query string, parameterCount int, options ...bool) *DynamicSqlLoader {
6969
driver := getDriver(db)
7070
var mp func(string) string
71-
if driver == DriverOracle {
71+
if driver == driverOracle {
7272
mp = strings.ToUpper
7373
} else {
7474
mp = strings.ToLower
@@ -83,13 +83,13 @@ func NewDynamicSqlCodeLoader(db *sql.DB, query string, parameterCount int, optio
8383
handleDriver = true
8484
}
8585
if handleDriver {
86-
if driver == DriverOracle || driver == DriverPostgres || driver == DriverMssql {
86+
if driver == driverOracle || driver == driverPostgres || driver == driverMssql {
8787
var x string
88-
if driver == DriverOracle {
88+
if driver == driverOracle {
8989
x = ":val"
90-
} else if driver == DriverPostgres {
90+
} else if driver == driverPostgres {
9191
x = "$"
92-
} else if driver == DriverMssql {
92+
} else if driver == driverMssql {
9393
x = "@p"
9494
}
9595
for i := 0; i < parameterCount; i++ {
@@ -156,7 +156,7 @@ func NewSqlCodeLoader(db *sql.DB, table string, config StructureConfig, options
156156
}
157157
driver := getDriver(db)
158158
var mp func(string) string
159-
if driver == DriverOracle {
159+
if driver == driverOracle {
160160
mp = strings.ToUpper
161161
}
162162
return &SqlLoader{DB: db, Table: table, Config: config, Build: build, Map: mp}
@@ -335,21 +335,21 @@ func getBuild(db *sql.DB) func(i int) string {
335335
}
336336
func getDriver(db *sql.DB) string {
337337
if db == nil {
338-
return DriverNotSupport
338+
return driverNotSupport
339339
}
340340
driver := reflect.TypeOf(db.Driver()).String()
341341
switch driver {
342342
case "*pq.Driver":
343-
return DriverPostgres
343+
return driverPostgres
344344
case "*godror.drv":
345-
return DriverOracle
345+
return driverOracle
346346
case "*mysql.MySQLDriver":
347-
return DriverMysql
347+
return driverMysql
348348
case "*mssql.Driver":
349-
return DriverMssql
349+
return driverMssql
350350
case "*sqlite3.SQLiteDriver":
351-
return DriverSqlite3
351+
return driverSqlite3
352352
default:
353-
return DriverNotSupport
353+
return driverNotSupport
354354
}
355355
}

echo/handler.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package echo
2+
3+
import (
4+
"context"
5+
co "github.com/core-go/code"
6+
"github.com/labstack/echo/v4"
7+
"io/ioutil"
8+
"net/http"
9+
"strings"
10+
)
11+
12+
const internalServerError = "Internal Server Error"
13+
14+
type Handler struct {
15+
Codes func(ctx context.Context, master string) ([]co.Model, error)
16+
RequiredMaster bool
17+
Error func(context.Context, string)
18+
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
19+
Resource string
20+
Action string
21+
Id string
22+
Name string
23+
}
24+
25+
func NewDefaultCodeHandler(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler {
26+
var writeLog func(context.Context, string, string, bool, string) error
27+
if len(options) >= 1 {
28+
writeLog = options[0]
29+
}
30+
return NewCodeHandlerWithLog(load, logError, true, writeLog, "", "")
31+
}
32+
func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]co.Model, error), c co.HandlerConfig, logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler {
33+
var requireMaster bool
34+
if c.Master != nil {
35+
requireMaster = *c.Master
36+
} else {
37+
requireMaster = true
38+
}
39+
var writeLog func(context.Context, string, string, bool, string) error
40+
if len(options) >= 1 {
41+
writeLog = options[0]
42+
}
43+
h := NewCodeHandlerWithLog(load, logError, requireMaster, writeLog, c.Resource, c.Action)
44+
h.Id = c.Id
45+
h.Name = c.Name
46+
return h
47+
}
48+
func NewCodeHandler(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), requiredMaster bool, options ...func(context.Context, string, string, bool, string) error) *Handler {
49+
var writeLog func(context.Context, string, string, bool, string) error
50+
if len(options) >= 1 {
51+
writeLog = options[0]
52+
}
53+
return NewCodeHandlerWithLog(load, logError, requiredMaster, writeLog, "", "")
54+
}
55+
func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), requiredMaster bool, writeLog func(context.Context, string, string, bool, string) error, options ...string) *Handler {
56+
var resource, action string
57+
if len(options) >= 1 && len(options[0]) > 0 {
58+
resource = options[0]
59+
} else {
60+
resource = "code"
61+
}
62+
if len(options) >= 2 && len(options[1]) > 0 {
63+
action = options[1]
64+
} else {
65+
action = "load"
66+
}
67+
h := Handler{Codes: load, Resource: resource, Action: action, RequiredMaster: requiredMaster, Log: writeLog, Error: logError}
68+
return &h
69+
}
70+
func (h *Handler) Load() echo.HandlerFunc {
71+
return func(ctx echo.Context) error {
72+
r := ctx.Request()
73+
code := ""
74+
if h.RequiredMaster {
75+
if r.Method == "GET" {
76+
i := strings.LastIndex(r.RequestURI, "/")
77+
if i >= 0 {
78+
code = r.RequestURI[i+1:]
79+
}
80+
} else {
81+
b, er1 := ioutil.ReadAll(r.Body)
82+
if er1 != nil {
83+
ctx.String(http.StatusBadRequest, "Body cannot is empty")
84+
return er1
85+
}
86+
code = strings.Trim(string(b), " ")
87+
}
88+
}
89+
result, er4 := h.Codes(r.Context(), code)
90+
if er4 != nil {
91+
return respondError(ctx, http.StatusInternalServerError, internalServerError, h.Error, h.Resource, h.Action, er4, h.Log)
92+
} else {
93+
if len(h.Id) == 0 && len(h.Name) == 0 {
94+
return succeed(ctx, http.StatusOK, result, h.Log, h.Resource, h.Action)
95+
} else {
96+
rs := make([]map[string]string, 0)
97+
for _, r := range result {
98+
m := make(map[string]string)
99+
m[h.Id] = r.Id
100+
m[h.Name] = r.Name
101+
rs = append(rs, m)
102+
}
103+
return succeed(ctx, http.StatusOK, rs, h.Log, h.Resource, h.Action)
104+
}
105+
}
106+
}
107+
}
108+
109+
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 {
110+
err := ctx.JSON(code, result)
111+
if writeLog != nil {
112+
writeLog(ctx.Request().Context(), resource, action, success, desc)
113+
}
114+
return err
115+
}
116+
func respondError(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 {
117+
if logError != nil {
118+
logError(ctx.Request().Context(), err.Error())
119+
}
120+
respond(ctx, code, result, writeLog, resource, action, false, err.Error())
121+
return err
122+
}
123+
func succeed(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error {
124+
return respond(ctx, code, result, writeLog, resource, action, true, "")
125+
}

echo_v3/handler.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package echo
2+
3+
import (
4+
"context"
5+
co "github.com/core-go/code"
6+
"github.com/labstack/echo"
7+
"io/ioutil"
8+
"net/http"
9+
"strings"
10+
)
11+
12+
const internalServerError = "Internal Server Error"
13+
14+
type Handler struct {
15+
Codes func(ctx context.Context, master string) ([]co.Model, error)
16+
RequiredMaster bool
17+
Error func(context.Context, string)
18+
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
19+
Resource string
20+
Action string
21+
Id string
22+
Name string
23+
}
24+
25+
func NewDefaultCodeHandler(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler {
26+
var writeLog func(context.Context, string, string, bool, string) error
27+
if len(options) >= 1 {
28+
writeLog = options[0]
29+
}
30+
return NewCodeHandlerWithLog(load, logError, true, writeLog, "", "")
31+
}
32+
func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]co.Model, error), c co.HandlerConfig, logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler {
33+
var requireMaster bool
34+
if c.Master != nil {
35+
requireMaster = *c.Master
36+
} else {
37+
requireMaster = true
38+
}
39+
var writeLog func(context.Context, string, string, bool, string) error
40+
if len(options) >= 1 {
41+
writeLog = options[0]
42+
}
43+
h := NewCodeHandlerWithLog(load, logError, requireMaster, writeLog, c.Resource, c.Action)
44+
h.Id = c.Id
45+
h.Name = c.Name
46+
return h
47+
}
48+
func NewCodeHandler(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), requiredMaster bool, options ...func(context.Context, string, string, bool, string) error) *Handler {
49+
var writeLog func(context.Context, string, string, bool, string) error
50+
if len(options) >= 1 {
51+
writeLog = options[0]
52+
}
53+
return NewCodeHandlerWithLog(load, logError, requiredMaster, writeLog, "", "")
54+
}
55+
func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]co.Model, error), logError func(context.Context, string), requiredMaster bool, writeLog func(context.Context, string, string, bool, string) error, options ...string) *Handler {
56+
var resource, action string
57+
if len(options) >= 1 && len(options[0]) > 0 {
58+
resource = options[0]
59+
} else {
60+
resource = "code"
61+
}
62+
if len(options) >= 2 && len(options[1]) > 0 {
63+
action = options[1]
64+
} else {
65+
action = "load"
66+
}
67+
h := Handler{Codes: load, Resource: resource, Action: action, RequiredMaster: requiredMaster, Log: writeLog, Error: logError}
68+
return &h
69+
}
70+
func (h *Handler) Load() echo.HandlerFunc {
71+
return func(ctx echo.Context) error {
72+
r := ctx.Request()
73+
code := ""
74+
if h.RequiredMaster {
75+
if r.Method == "GET" {
76+
i := strings.LastIndex(r.RequestURI, "/")
77+
if i >= 0 {
78+
code = r.RequestURI[i+1:]
79+
}
80+
} else {
81+
b, er1 := ioutil.ReadAll(r.Body)
82+
if er1 != nil {
83+
ctx.String(http.StatusBadRequest, "Body cannot is empty")
84+
return er1
85+
}
86+
code = strings.Trim(string(b), " ")
87+
}
88+
}
89+
result, er4 := h.Codes(r.Context(), code)
90+
if er4 != nil {
91+
return respondError(ctx, http.StatusInternalServerError, internalServerError, h.Error, h.Resource, h.Action, er4, h.Log)
92+
} else {
93+
if len(h.Id) == 0 && len(h.Name) == 0 {
94+
return succeed(ctx, http.StatusOK, result, h.Log, h.Resource, h.Action)
95+
} else {
96+
rs := make([]map[string]string, 0)
97+
for _, r := range result {
98+
m := make(map[string]string)
99+
m[h.Id] = r.Id
100+
m[h.Name] = r.Name
101+
rs = append(rs, m)
102+
}
103+
return succeed(ctx, http.StatusOK, rs, h.Log, h.Resource, h.Action)
104+
}
105+
}
106+
}
107+
}
108+
109+
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 {
110+
err := ctx.JSON(code, result)
111+
if writeLog != nil {
112+
writeLog(ctx.Request().Context(), resource, action, success, desc)
113+
}
114+
return err
115+
}
116+
func respondError(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 {
117+
if logError != nil {
118+
logError(ctx.Request().Context(), err.Error())
119+
}
120+
respond(ctx, code, result, writeLog, resource, action, false, err.Error())
121+
return err
122+
}
123+
func succeed(ctx echo.Context, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string) error {
124+
return respond(ctx, code, result, writeLog, resource, action, true, "")
125+
}

0 commit comments

Comments
 (0)