forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (123 loc) · 3.81 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
package main
import (
"errors"
"fmt"
"github.com/devfeel/dotweb"
"github.com/devfeel/dotweb/framework/exception"
"github.com/devfeel/dotweb/session"
"net/http"
"strconv"
"time"
)
func main() {
defer func() {
var errmsg string
if err := recover(); err != nil {
errmsg = exception.CatchError("main", dotweb.LogTarget_HttpServer, err)
fmt.Println("main error : ", errmsg)
}
}()
//初始化DotServer
app := dotweb.New()
//设置dotserver日志目录
//如果不设置,默认不启用,且默认为当前目录
app.SetEnabledLog(true)
//开启development模式
app.SetDevelopmentMode()
//设置gzip开关
//app.HttpServer.SetEnabledGzip(true)
//设置Session开关
app.HttpServer.SetEnabledSession(true)
//1.use default config
//app.HttpServer.Features.SetEnabledCROS()
//2.use user config
//app.HttpServer.Features.SetEnabledCROS(true).SetOrigin("*").SetMethod("GET")
//设置Session配置
//runtime mode
app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig())
//redis mode
//app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6379", ""))
app.HttpServer.SetEnabledDetailRequestData(true)
//设置路由
InitRoute(app.HttpServer)
//自定义404输出
app.SetNotFoundHandle(func(ctx dotweb.Context) {
ctx.Response().Write(http.StatusNotFound, []byte("is't app's not found!"))
})
//设置HttpModule
//InitModule(app)
//启动 监控服务
app.SetPProfConfig(true, 8081)
//全局容器
app.Items.Set("gstring", "gvalue")
app.Items.Set("gint", 1)
// 开始服务
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")
ctx.WriteString(ctx.Request().URL.Path)
//_, err := ctx.WriteStringC(201, "index => ", ctx.RemoteIP(), "我是首页")
return nil
}
func Time(ctx dotweb.Context) error {
minuteTimeLayout := "200601021504"
if t, err := time.Parse(minuteTimeLayout, "201709251541"); err != nil {
ctx.WriteString(err.Error())
} else {
now, _ := time.Parse(minuteTimeLayout, time.Now().Format(minuteTimeLayout))
ctx.WriteString(t)
ctx.WriteString(now)
ctx.WriteString(t.Sub(now))
//ctx.WriteString(t.Sub(time.Now()) > 5*time.Minute)
}
return nil
}
func IndexReg(ctx dotweb.Context) error {
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
return ctx.WriteString("welcome to dotweb")
}
func IndexParam(ctx dotweb.Context) error {
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
return ctx.WriteString("IndexParam", ctx.GetRouterName("id"))
}
func KeyPost(ctx dotweb.Context) error {
username1 := ctx.PostFormValue("username")
username2 := ctx.FormValue("username")
username3 := ctx.PostFormValue("username")
return ctx.WriteString("username:" + username1 + " - " + username2 + " - " + username3)
}
func JsonPost(ctx dotweb.Context) error {
return ctx.WriteString("body:" + string(ctx.Request().PostBody()))
}
func DefaultError(ctx dotweb.Context) error {
//panic("my panic error!")
i := 0
b := 2 / i
return ctx.WriteString(b)
}
func Redirect(ctx dotweb.Context) error {
err := ctx.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
if err != nil {
ctx.WriteString(err)
}
return err
}
func ReturnError(ctx dotweb.Context) error {
return errors.New("return error")
}
func InitRoute(server *dotweb.HttpServer) {
server.GET("/", Index)
server.GET("/time", Time)
server.GET("/index", Index)
server.GET("/id/:id", IndexParam)
server.POST("/keypost", KeyPost)
server.POST("/jsonpost", JsonPost)
server.GET("/error", DefaultError)
server.GET("/returnerr", ReturnError)
server.GET("/redirect", Redirect)
//server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
}