-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
147 lines (125 loc) · 3.59 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
stdContext "context"
"encoding/json"
"fmt"
"go_eden/model"
"go_eden/routes"
"time"
"go_eden/web/controllers"
"github.com/kataras/golog"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
"github.com/kataras/iris/v12/mvc"
)
func main() {
fmt.Print("go main...")
app := newApp()
app.Run(
iris.Addr(":8080"),
iris.WithoutInterruptHandler,
iris.WithoutBodyConsumptionOnUnmarshal,
configure)
}
func newApp() *iris.Application {
// e := casbins.GetEnforcer()
// casbinMiddleware := cm.New(e)
app := iris.New()
//(可选)添加两个内置处理程序
//可以从任何与http相关的panics中恢复
//并将请求记录到终端。
app.Logger().SetLevel("debug")
app.Use(recover.New())
app.Use(logger.New())
//session
// sess := sessions.New(sessions.Config{
// // Cookie string, the session's client cookie name, for example: "mysessionid"
// //
// // Defaults to "irissessionid"
// Cookie: "token",
// // it's time.Duration, from the time cookie is created, how long it can be alive?
// // 0 means no expire.
// // -1 means expire when browser closes
// // or set a value, like 2 hours:
// Expires: time.Hour * 2,
// // if you want to invalid cookies on different subdomains
// // of the same host, then enable it.
// // Defaults to false.
// DisableSubdomainPersistence: false,
// })
// app.Use(sess.Handler()) //// session is always non-nil inside handlers now.
// app.Use(casbinMiddleware.ServeHTTP)
//控制器根路由路径"/"
//
//logger
requestLogger := logger.New(logger.Config{
// Status displays status code
Status: true,
// IP displays request's remote address
IP: true,
// Method displays the http method
Method: true,
// Path displays the request path
Path: true,
// Query appends the url query to the Path.
Query: true,
Columns: true,
// if !empty then its contents derives from `ctx.Values().Get("logger_message")
// will be added to the logs.
MessageContextKeys: []string{"logger_message"},
// if !empty then its contents derives from `ctx.GetHeader("User-Agent")
MessageHeaderKeys: []string{"User-Agent"},
})
app.Use(requestLogger)
// app.Run(
// // Start the web server at localhost:8080
// iris.Addr("localhost:8080"),
// // skip err server closed when CTRL/CMD+C pressed:
// iris.WithoutServerError(iris.ErrServerClosed),
// // enables faster json serialization and more:
// iris.WithOptimizations,
// )
iris.RegisterOnInterrupt(func() {
timeout := 5 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
//
app.Shutdown(ctx)
})
//或捕获所有http错误:
app.OnAnyErrorCode(requestLogger, func(ctx iris.Context) {
body, err := ctx.GetBody()
if err != nil {
// logs.Logger().Log.Error(err.Error())
}
golog.Error("OnAnyErrorCode=", string(body))
response := model.Response{ResCode: ctx.GetStatusCode(), Message: ctx.Path()}
ctx.ContentType("application/json;charset=UTF-8")
ctx.Writef(toJSON(response))
})
mvc.Configure(app, configureMvc)
return app
}
///app configure
func configure(app *iris.Application) {
app.Configure(
iris.WithoutServerError(iris.ErrServerClosed),
)
}
///config mvc
func configureMvc(app *mvc.Application) {
//mvc controller
root := app.Party("/")
root.Handle(new(controllers.RootController))
//login
routes.Route(app)
}
func toJSON(obj interface{}) string {
jsonByte, err := json.Marshal(obj)
if err != nil {
// fmt.Println("error:" + err.Error())
return err.Error()
}
return string(jsonByte)
}