forked from sunmi-OS/gocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (63 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"os"
"sort"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/sunmi-OS/gocore/api"
coreMiddleware "github.com/sunmi-OS/gocore/api/middleware"
"github.com/urfave/cli"
)
type EchoApi struct {
}
var eApi EchoApi
func (a *EchoApi) echoStart(c *cli.Context) error {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
//在捕捉panic的同时封装自定义返回错误内容
//如果Recover()函数不传参数默认响应客户端{"message":"Internal Server Error"}
e.Use(coreMiddleware.Recover(`{"code":-1,"data":null,"msg":"服务异常,请稍后再试。"}`))
// Route => handler
e.POST("/", func(c echo.Context) error {
request := api.NewRequest(c)
response := api.NewResponse(c)
err := request.InitRawJson()
if err != nil {
return response.RetError(err, 400)
}
msg := request.Param(`test`).GetString()
return response.RetSuccess(msg)
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
return nil
}
func main() {
app := cli.NewApp()
app.Name = "IOT-seanbox"
app.Usage = "IOT-seanbox"
app.Email = "wenzhenxi@sunmi.com"
app.Version = "1.0.0"
app.Usage = "IOT-seanbox"
app.Action = eApi.echoStart
// 指定对于的命令
app.Commands = []cli.Command{
{
Name: "api",
Aliases: []string{"a"},
Usage: "api",
Subcommands: []cli.Command{
{
Name: "start",
Usage: "开启API-DEMO",
Action: eApi.echoStart,
},
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}