-
Notifications
You must be signed in to change notification settings - Fork 180
/
main.go
182 lines (148 loc) · 4.43 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
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
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/devfeel/dotweb"
)
func main() {
//初始化DotServer
app := dotweb.New()
//设置dotserver日志目录
//如果不设置,默认不启用,且默认为当前目录
app.SetEnabledLog(true)
//开启development模式
app.SetDevelopmentMode()
app.UseTimeoutHook(dotweb.DefaultTimeoutHookHandler, time.Second*10)
exAccessFmtLog := NewAccessFmtLog("appex")
exAccessFmtLog.Exclude("/index")
exAccessFmtLog.Exclude("/v1/machines/queryIP/:IP")
app.Use(exAccessFmtLog)
app.ExcludeUse(NewAccessFmtLog("appex1"), "/")
app.Use(
NewAccessFmtLog("app"),
)
//设置路由
InitRoute(app.HttpServer)
//启动 监控服务
app.SetPProfConfig(true, 8081)
// 开始服务
port := 8080
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
err := app.StartServer(port)
fmt.Println("dotweb.StartServer error => ", err)
}
func Index(ctx dotweb.Context) error {
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
//fmt.Println(time.Now(), "Index Handler")
err := ctx.WriteString("index => ", ctx.Request().Url())
fmt.Println(ctx.RouterNode().GroupMiddlewares())
return err
}
func ShowMiddlewares(ctx dotweb.Context) error {
err := ctx.WriteString("ShowMiddlewares => ", ctx.RouterNode().GroupMiddlewares())
return err
}
func InitRoute(server *dotweb.HttpServer) {
server.Router().GET("/", Index)
server.Router().GET("/index", Index)
server.Router().GET("/v1/machines/queryIP/:IP", Index)
server.Router().GET("/v1/machines/queryIP2", Index)
server.Router().GET("/use", Index).Use(NewAccessFmtLog("Router-use"))
/*g := server.Group("/group").Use(NewAccessFmtLog("group")).Use(NewSimpleAuth("admin"))
g.GET("/", Index)
g.GET("/use", Index).Use(NewAccessFmtLog("group-use"))*/
g := server.Group("/A").Use(NewAGroup())
g.GET("/", ShowMiddlewares)
g1 := g.Group("/B").Use(NewBGroup())
g1.GET("/", ShowMiddlewares)
g2 := g.Group("/C").Use(NewCGroup())
g2.GET("/", ShowMiddlewares)
g = server.Group("/B").Use(NewBGroup())
g.GET("/", ShowMiddlewares)
}
func InitModule(dotserver *dotweb.HttpServer) {
dotserver.RegisterModule(&dotweb.HttpModule{
OnBeginRequest: func(ctx dotweb.Context) {
fmt.Println(time.Now(), "HttpModule BeginRequest1:", ctx.Request().RequestURI)
},
OnEndRequest: func(ctx dotweb.Context) {
fmt.Println(time.Now(), "HttpModule EndRequest1:", ctx.Request().RequestURI)
},
})
dotserver.RegisterModule(&dotweb.HttpModule{
OnBeginRequest: func(ctx dotweb.Context) {
fmt.Println(time.Now(), "HttpModule BeginRequest2:", ctx.Request().RequestURI)
},
})
dotserver.RegisterModule(&dotweb.HttpModule{
OnEndRequest: func(ctx dotweb.Context) {
fmt.Println(time.Now(), "HttpModule EndRequest3:", ctx.Request().RequestURI)
},
})
}
type AccessFmtLog struct {
dotweb.BaseMiddleware
Index string
}
func (m *AccessFmtLog) Handle(ctx dotweb.Context) error {
fmt.Println(time.Now(), "[AccessFmtLog ", m.Index, "] begin request -> ", ctx.Request().RequestURI)
err := m.Next(ctx)
fmt.Println(time.Now(), "[AccessFmtLog ", m.Index, "] finish request ", err, " -> ", ctx.Request().RequestURI)
return err
}
func NewAccessFmtLog(index string) *AccessFmtLog {
return &AccessFmtLog{Index: index}
}
type SimpleAuth struct {
dotweb.BaseMiddleware
exactToken string
}
func (m *SimpleAuth) Handle(ctx dotweb.Context) error {
fmt.Println(time.Now(), "[SimpleAuth] begin request -> ", ctx.Request().RequestURI)
var err error
if ctx.QueryString("token") != m.exactToken {
ctx.Write(http.StatusUnauthorized, []byte("sorry, Unauthorized"))
} else {
err = m.Next(ctx)
}
fmt.Println(time.Now(), "[SimpleAuth] finish request ", err, " -> ", ctx.Request().RequestURI)
return err
}
func NewSimpleAuth(exactToken string) *SimpleAuth {
return &SimpleAuth{exactToken: exactToken}
}
type AGroup struct {
dotweb.BaseMiddleware
}
func (m *AGroup) Handle(ctx dotweb.Context) error {
fmt.Println(time.Now(), "[AGroup] request)")
err := m.Next(ctx)
return err
}
func NewAGroup() *AGroup {
return &AGroup{}
}
type BGroup struct {
dotweb.BaseMiddleware
}
func (m *BGroup) Handle(ctx dotweb.Context) error {
fmt.Println(time.Now(), "[BGroup] request)")
err := m.Next(ctx)
return err
}
func NewBGroup() *BGroup {
return &BGroup{}
}
type CGroup struct {
dotweb.BaseMiddleware
}
func (m *CGroup) Handle(ctx dotweb.Context) error {
fmt.Println(time.Now(), "[CGroup] request)")
err := m.Next(ctx)
return err
}
func NewCGroup() *CGroup {
return &CGroup{}
}