forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (115 loc) · 3.66 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
package main
import (
"errors"
"fmt"
"github.com/devfeel/dotweb"
"github.com/devfeel/dotweb/config"
"github.com/devfeel/dotweb/framework/json"
"net/http"
"time"
)
func main() {
//初始化DotServer
app := dotweb.New()
//注册HttpHandler
RegisterHandler(app.HttpServer)
//appConfig, err := config.InitConfig("d:/gotmp/dotweb.conf")
//json config
appConfig, err := config.InitConfig("d:/gotmp/dotweb.json.conf", "json")
if err != nil {
fmt.Println("dotweb.InitConfig error => " + fmt.Sprint(err))
return
}
fmt.Println(jsonutil.GetJsonString(appConfig))
RegisterMiddlewares(app)
err = app.SetConfig(appConfig)
if err != nil {
fmt.Println("dotweb.SetConfig error => " + fmt.Sprint(err))
return
}
fmt.Println("dotweb.StartServer => " + fmt.Sprint(appConfig))
err = app.Start()
fmt.Println("dotweb.StartServer error => ", err)
}
func Index(ctx dotweb.Context) error {
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
_, err := ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
return err
}
func GetAppSet(ctx dotweb.Context) error {
key := ctx.QueryString("key")
_, err := ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key))
return err
}
func DefaultPanic(ctx dotweb.Context) error {
panic("my panic error!")
return nil
}
func DefaultError(ctx dotweb.Context) error {
err := errors.New("my return error")
return err
}
func Redirect(ctx dotweb.Context) error {
return ctx.Redirect(200, "http://www.baidu.com")
}
func Login(ctx dotweb.Context) error {
_, err := ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
return err
}
func Logout(ctx dotweb.Context) error {
_, err := ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
return err
}
func RegisterHandler(server *dotweb.HttpServer) {
server.Router().RegisterHandler("Index", Index)
server.Router().RegisterHandler("Error", DefaultError)
server.Router().RegisterHandler("Panic", DefaultPanic)
server.Router().RegisterHandler("Redirect", Redirect)
server.Router().RegisterHandler("Login", Login)
server.Router().RegisterHandler("Logout", Logout)
server.Router().RegisterHandler("appset", GetAppSet)
}
func RegisterMiddlewares(app *dotweb.DotWeb) {
//集中注册middleware
app.RegisterMiddlewareFunc("applog", NewAppAccessFmtLog)
app.RegisterMiddlewareFunc("grouplog", NewGroupAccessFmtLog)
app.RegisterMiddlewareFunc("urllog", NewUrlAccessFmtLog)
app.RegisterMiddlewareFunc("simpleauth", NewSimpleAuth)
}
type AccessFmtLog struct {
dotweb.BaseMiddlware
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 NewAppAccessFmtLog() dotweb.Middleware {
return &AccessFmtLog{Index: "app"}
}
func NewGroupAccessFmtLog() dotweb.Middleware {
return &AccessFmtLog{Index: "group"}
}
func NewUrlAccessFmtLog() dotweb.Middleware {
return &AccessFmtLog{Index: "url"}
}
type SimpleAuth struct {
dotweb.BaseMiddlware
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() dotweb.Middleware {
return &SimpleAuth{exactToken: "admin"}
}