This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
/
h2.mux.iris.go
251 lines (216 loc) · 7.09 KB
/
h2.mux.iris.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package svr
import (
"context"
"github.com/hedzr/cmdr"
"github.com/hedzr/cmdr/conf"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/core/host"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
"net"
"net/http"
"time"
)
func newIris() *irisImpl {
d := &irisImpl{
BaseIrisImpl: NewRouterBaseIrisImpl(),
}
return d
}
type irisImpl struct {
BaseIrisImpl
}
func NewRouterBaseIrisImpl() BaseIrisImpl {
d := BaseIrisImpl{}
d.init()
return d
}
type BaseIrisImpl struct {
irisApp *iris.Application
ac *accesslog.AccessLog
}
func (d *BaseIrisImpl) init() {
app := iris.New()
d.irisApp = app
app.UseRouter(recover.New())
}
func (d *BaseIrisImpl) Shutdown(ctx context.Context) error {
return d.irisApp.Shutdown(ctx)
}
func (d *BaseIrisImpl) PrePreServe() {
l := cmdr.GetLoggerLevel()
n := "debug"
switch l {
case cmdr.OffLevel:
n = "disable"
case cmdr.FatalLevel, cmdr.PanicLevel:
n = "fatal"
case cmdr.ErrorLevel:
n = "error"
case cmdr.WarnLevel:
n = "warn"
case cmdr.InfoLevel:
n = "info"
default:
}
customLogger := 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: false,
// 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"},
LogFunc: d.print,
})
app := d.irisApp
app.Use(customLogger)
app.Logger().SetLevel(n)
//app.OnAnyErrorCode(customLogger, func(ctx iris.Context) {
// // this should be added to the logs, at the end because of the `logger.Config#MessageContextKey`
// ctx.Values().Set("logger_message",
// "a dynamic message passed to the logs")
// ctx.Writef("My Custom error page")
//})
// Note, it's buffered, so make sure it's closed so it can flush any buffered contents.
d.ac = accesslog.File("./access.log")
// defer ac.Close()
app.UseRouter(d.ac.Handler)
if cmdr.GetBoolRP(conf.AppName, "server.statics.enabled") {
// Serve our front-end and its assets.
app.HandleDir(cmdr.GetStringRP(conf.AppName, "server.statics.url"), iris.Dir(cmdr.GetStringRP(conf.AppName, "server.statics.path")))
}
//loc := cmdr.GetStringRP(conf.AppName, "server.statics.path")
//yaag.Init(&yaag.Config{ // <- IMPORTANT, init the middleware.
// On: true,
// DocTitle: cmdr.AppName + " via Iris",
// DocPath: path.Join(loc, "apidoc.html"),
// BaseUrls: map[string]string{"Production": "", "Staging": ""},
//})
//app.UseRouter(irisyaag.New()) // <- IMPORTANT, register the middleware.
}
func (d *BaseIrisImpl) print(endTime time.Time, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) {
cmdr.Logger.Infof("%v %v %v %v %v %v %v %v", endTime, latency, status, ip, method, path, message, headerMessage)
}
func (d *BaseIrisImpl) PreServe() (err error) {
return
}
func (d *BaseIrisImpl) PostServe() (err error) {
return
}
func (d *BaseIrisImpl) Handler() http.Handler { return d.irisApp }
func (d *BaseIrisImpl) App() http.Handler { return d.irisApp }
func (d *BaseIrisImpl) Serve(srv *http.Server, listener net.Listener, certFile, keyFile string) (err error) {
// return d.irisApp.Run(iris.Raw(func() error {
// su := d.irisApp.NewHost(srv)
// if netutil.IsTLS(su.Server) {
// h2listener = tls.NewListener(listener, su.Server.TLSConfig)
// // cmdr.Logger.Debugf("new h2listener: %v", su.Server.TLSConfig)
// su.Configure(func(su *host.Supervisor) {
// rs := reflect.ValueOf(su).Elem()
// // rf := rs.FieldByName("manuallyTLS")
// rf := rs.Field(2)
// // rf can't be read or set.
// rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
// // Now rf can be read and set.
//
// // su.manuallyTLS = true
// i := true
// ri := reflect.ValueOf(&i).Elem() // i, but writeable
// rf.Set(ri)
// })
// }
// err = su.Serve(listener)
// return err
// }), iris.WithoutServerError(iris.ErrServerClosed))
if listener != nil {
// v1 - http 1.1 with tls:
//h2listener = tls.NewListener(listener, srv.TLSConfig)
//return d.irisApp.Run(iris.Listener(h2listener), iris.WithoutServerError(iris.ErrServerClosed))
// v2 - http 2 with iris tls, but h2listener not ready
_ = listener.Close()
return d.irisApp.Run(d.irisTLSServer(srv),
// iris.WithoutVersionChecker,
//iris.WithOptimizations,
iris.WithoutServerError(iris.ErrServerClosed))
}
return d.irisApp.Run(iris.Server(srv), iris.WithoutServerError(iris.ErrServerClosed))
}
//type super struct {
// *iris.Supervisor
//}
//
//func irisTLSListener(l net.Listener, srv *http.Server, hostConfigs ...host.Configurator) iris.Runner {
// return func(app *iris.Application) error {
// //app.config.vhost = netutil.ResolveVHost(l.Addr().String())
// s:= &super{Supervisor: app.NewHost(srv),}
// return s.Configure(hostConfigs...).
// ServeTLS(l)
// }
//}
func (d *BaseIrisImpl) irisTLSServer(srv *http.Server, hostConfigs ...host.Configurator) iris.Runner {
return func(app *iris.Application) error {
host := app.NewHost(srv)
host.RegisterOnShutdown(func() {
d.ac.Close()
})
return host.Configure(hostConfigs...).
ListenAndServeTLS("", "")
}
}
func (d *BaseIrisImpl) BuildRoutes() {
}
func (d *irisImpl) BuildRoutes() {
// https://iris-go.com/start/
// https://github.com/kataras/iris
//
// https://www.slant.co/topics/1412/~best-web-frameworks-for-go
d.irisApp.Get("/", func(c iris.Context) {
_, _ = c.JSON(iris.Map{"message": "Hello Iris!"})
})
d.irisApp.Get("/ping", func(ctx iris.Context) {
_, _ = ctx.WriteString("pong")
})
// Resource: http://localhost:1380
d.irisApp.Handle("GET", "/welcome", func(ctx iris.Context) {
_, _ = ctx.HTML("<h1>Welcome</h1>")
})
// app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
d.irisApp.Get("/s/:path", d.echoIrisHandler)
//
// d.irisApp.Get("/users/{id:uint64}", func(ctx iris.Context){
// id := ctx.Params().GetUint64Default("id", 0)
// // [...]
// })
// d.irisApp.Get("/profile/{name:alphabetical max(255)}", func(ctx iris.Context){
// name := ctx.Params().Get("name")
// // len(name) <=255 otherwise this route will fire 404 Not Found
// // and this handler will not be executed at all.
// })
//
// d.irisApp.Get("/someGet", getting)
// d.irisApp.Post("/somePost", posting)
// d.irisApp.Put("/somePut", putting)
// d.irisApp.Delete("/someDelete", deleting)
// d.irisApp.Patch("/somePatch", patching)
// d.irisApp.Head("/someHead", head)
// d.irisApp.Options("/someOptions", options)
// user.Register(d.irisApp)
}
func (d *irisImpl) echoIrisHandler(ctx iris.Context) {
p := ctx.Params().GetString("path")
if p == "zero" {
d0(8, 0) // raise a 0-divide panic and it will be recovered by http.Conn.serve(ctx)
}
_, _ = ctx.WriteString(p)
}