Skip to content

Commit 8718777

Browse files
committed
Version 0.3.6.5
* App、Server、Session配置代码梳理优化 * 新增Server接口,新增OfflineServer实现 * 新增Router接口,将原HttpServer路由操作转移至HttpServer.Router() * 2017-03-24 14:00
1 parent cce9149 commit 8718777

20 files changed

Lines changed: 355 additions & 1392 deletions

File tree

config/configs.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ type (
1313
Session SessionConfig `xml:"session"`
1414
Routers []RouterConfig `xml:"routers>router"`
1515
}
16-
1716
ServerConfig struct {
1817
LogPath string `xml:"logpath,attr"` //文件方式日志目录,如果为空,默认当前目录
18+
EnabledDebug bool `xml:"enableddebug,attr"` //启用Debug模式
1919
Port int `xml:"port,attr"` //端口
2020
Offline bool `xml:"offline,attr"` //是否维护,默认false
2121
OfflineText string `xml:"offlinetext,attr"` //当设置为维护,默认显示内容,如果设置url,优先url
2222
OfflineUrl string `xml:"offlineurl,attr"` //当设置为维护,默认维护页地址,如果设置url,优先url
23-
EnabledDebug bool `xml:"enableddebug,attr"` //启用Debug模式
2423
EnabledGzip bool `xml:"enabledgzip,attr"` //启用gzip
2524
}
2625

@@ -41,6 +40,20 @@ type (
4140
}
4241
)
4342

43+
func NewAppConfig() *AppConfig {
44+
config := &AppConfig{}
45+
return config
46+
}
47+
func NewServerConfig() *ServerConfig {
48+
config := &ServerConfig{}
49+
return config
50+
}
51+
52+
func NewSessionConfig() *SessionConfig {
53+
config := &SessionConfig{}
54+
return config
55+
}
56+
4457
//初始化配置文件
4558
func InitConfig(configFile string) *AppConfig {
4659
content, err := ioutil.ReadFile(configFile)

context.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010

1111
"fmt"
12-
"github.com/devfeel/dotweb/router"
12+
"github.com/devfeel/dotweb/routers"
1313
"github.com/devfeel/dotweb/session"
1414
)
1515

@@ -19,7 +19,7 @@ const (
1919

2020
type HttpContext struct {
2121
Request *http.Request
22-
RouterParams router.Params
22+
RouterParams routers.Params
2323
Response *Response
2424
WebSocket *WebSocket
2525
HijackConn *HijackConn
@@ -33,7 +33,7 @@ type HttpContext struct {
3333
}
3434

3535
//reset response attr
36-
func (ctx *HttpContext) Reset(res *Response, r *http.Request, server *HttpServer, params router.Params) {
36+
func (ctx *HttpContext) Reset(res *Response, r *http.Request, server *HttpServer, params routers.Params) {
3737
ctx.Request = r
3838
ctx.Response = res
3939
ctx.RouterParams = params
@@ -77,7 +77,7 @@ func (ctx *HttpContext) Session() (session *session.SessionState) {
7777
//return nil, errors.New("no effective http-server")
7878
panic("no effective http-server")
7979
}
80-
if !ctx.HttpServer.ServerConfig.EnabledSession {
80+
if !ctx.HttpServer.SessionConfig.EnabledSession {
8181
//return nil, errors.New("http-server not enabled session")
8282
panic("http-server not enabled session")
8383
}
@@ -306,13 +306,16 @@ func (ctx *HttpContext) SetStatusCode(code int) error {
306306
return ctx.Response.WriteHeader(code)
307307
}
308308

309-
// write cookie for domain&name&liveseconds
309+
// write cookie for domain & name & maxAge
310310
//
311311
// default path = "/"
312312
// default domain = current domain
313-
// default seconds = 0
314-
func (ctx *HttpContext) WriteCookie(name, value string, seconds int) {
315-
cookie := http.Cookie{Name: name, Value: value, MaxAge: seconds}
313+
// default maxAge = 0 //seconds
314+
// seconds=0 means no 'Max-Age' attribute specified.
315+
// seconds<0 means delete cookie now, equivalently 'Max-Age: 0'
316+
// seconds>0 means Max-Age attribute present and given in seconds
317+
func (ctx *HttpContext) WriteCookie(name, value string, maxAge int) {
318+
cookie := http.Cookie{Name: name, Value: value, MaxAge: maxAge}
316319
http.SetCookie(ctx.Response.Writer(), &cookie)
317320
}
318321

dotweb.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/devfeel/dotweb/config"
66
"github.com/devfeel/dotweb/framework/json"
77
"github.com/devfeel/dotweb/framework/log"
8+
"github.com/devfeel/dotweb/servers"
89
"github.com/devfeel/dotweb/session"
910
"net/http"
1011
_ "net/http/pprof"
@@ -19,8 +20,8 @@ import (
1920
type (
2021
DotWeb struct {
2122
HttpServer *HttpServer
23+
OfflineServer servers.Server
2224
AppConfig *config.AppConfig
23-
SessionConfig *session.StoreConfig
2425
Modules []*HttpModule
2526
logpath string
2627
ExceptionHandler ExceptionHandle
@@ -44,9 +45,10 @@ const (
4445
*/
4546
func New() *DotWeb {
4647
app := &DotWeb{
47-
HttpServer: NewHttpServer(),
48-
Modules: make([]*HttpModule, 0, 10),
49-
AppContext: NewItemContext(),
48+
HttpServer: NewHttpServer(),
49+
OfflineServer: servers.NewOfflineServer(),
50+
Modules: make([]*HttpModule, 0, 10),
51+
AppContext: NewItemContext(),
5052
}
5153
app.HttpServer.setDotApp(app)
5254
return app
@@ -130,7 +132,7 @@ func (ds *DotWeb) SetEnabledDebug(isEnabled bool) {
130132
设置是否启用Session,默认为false
131133
*/
132134
func (ds *DotWeb) SetEnabledSession(isEnabled bool) {
133-
ds.HttpServer.ServerConfig.EnabledSession = isEnabled
135+
ds.HttpServer.SessionConfig.EnabledSession = isEnabled
134136
}
135137

136138
/*
@@ -141,8 +143,12 @@ func (ds *DotWeb) SetEnabledGzip(isEnabled bool) {
141143
}
142144

143145
//set session store config
144-
func (ds *DotWeb) SetSessionConfig(config *session.StoreConfig) {
145-
ds.SessionConfig = config
146+
func (ds *DotWeb) SetSessionConfig(storeConfig *session.StoreConfig) {
147+
ds.HttpServer.SessionConfig.Timeout = storeConfig.Maxlifetime
148+
ds.HttpServer.SessionConfig.SessionMode = storeConfig.StoreName
149+
ds.HttpServer.SessionConfig.ServerIP = storeConfig.ServerIP
150+
ds.HttpServer.SessionConfig.UserName = storeConfig.UserName
151+
ds.HttpServer.SessionConfig.Password = storeConfig.Password
146152
}
147153

148154
/*
@@ -178,21 +184,22 @@ func (ds *DotWeb) StartServer(httpport int) error {
178184

179185
//添加框架默认路由规则
180186
//默认支持pprof信息查看
181-
ds.HttpServer.GET("/dotweb/debug/pprof/:key", initPProf)
182-
ds.HttpServer.GET("/dotweb/debug/freemem", freeMemory)
183-
ds.HttpServer.GET("/dotweb/state", showServerState)
184-
ds.HttpServer.GET("/dotweb/query/:key", showQuery)
187+
ds.HttpServer.Router().GET("/dotweb/debug/pprof/:key", initPProf)
188+
ds.HttpServer.Router().GET("/dotweb/debug/freemem", freeMemory)
189+
ds.HttpServer.Router().GET("/dotweb/state", showServerState)
190+
ds.HttpServer.Router().GET("/dotweb/query/:key", showQuery)
185191

186192
if ds.ExceptionHandler == nil {
187193
ds.SetExceptionHandle(ds.DefaultHTTPErrorHandler)
188194
}
189195

190196
//init session manager
191-
if ds.HttpServer.ServerConfig.EnabledSession {
192-
if ds.SessionConfig == nil {
193-
panic("no set SessionConfig, but set enabledsession true")
197+
if ds.HttpServer.SessionConfig.EnabledSession {
198+
if ds.HttpServer.sessionManager == nil {
199+
//panic("no set SessionConfig, but set enabledsession true")
200+
logger.Warn("no set SessionConfig, but set enabledsession true, now will use default runtime session", LogTarget_HttpServer)
194201
}
195-
ds.HttpServer.InitSessionManager(ds.SessionConfig)
202+
ds.HttpServer.InitSessionManager(session.NewDefaultRuntimeConfig())
196203
}
197204

198205
port := ":" + strconv.Itoa(httpport)
@@ -211,17 +218,21 @@ func (ds *DotWeb) StartServerWithConfig(config *config.AppConfig) error {
211218
ds.SetEnabledGzip(config.Server.EnabledGzip)
212219

213220
//设置维护
214-
ds.HttpServer.setOffline(config.Server.Offline, config.Server.OfflineText, config.Server.OfflineUrl)
221+
if config.Server.Offline {
222+
ds.HttpServer.SetOffline(config.Server.Offline, config.Server.OfflineText, config.Server.OfflineUrl)
223+
ds.OfflineServer.SetOffline(config.Server.Offline, config.Server.OfflineText, config.Server.OfflineUrl)
224+
}
215225

226+
//设置session
216227
if config.Session.EnabledSession {
217228
ds.SetEnabledSession(config.Session.EnabledSession)
218229
ds.SetSessionConfig(session.NewStoreConfig(config.Session.SessionMode, config.Session.Timeout, config.Session.ServerIP, config.Session.UserName, config.Session.Password))
219230
}
220231

221232
//load router and register
222233
for _, v := range config.Routers {
223-
if h, isok := ds.HttpServer.GetHandler(v.HandlerName); isok && v.IsUse {
224-
ds.HttpServer.RegisterRoute(strings.ToUpper(v.Method), v.Path, h)
234+
if h, isok := ds.HttpServer.Router().GetHandler(v.HandlerName); isok && v.IsUse {
235+
ds.HttpServer.Router().RegisterRoute(strings.ToUpper(v.Method), v.Path, h)
225236
}
226237
}
227238

example/appcontext/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ func Index(ctx *dotweb.HttpContext) {
4343
}
4444

4545
func InitRoute(server *dotweb.HttpServer) {
46-
server.GET("/", Index)
46+
server.Router().GET("/", Index)
4747
}

example/bind/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ func TestBind(ctx *dotweb.HttpContext) {
5151
}
5252

5353
func InitRoute(server *dotweb.HttpServer) {
54-
server.POST("/", TestBind)
54+
server.Router().POST("/", TestBind)
5555
}

example/config/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Redirect(ctx *dotweb.HttpContext) {
3434
}
3535

3636
func RegisterHandler(server *dotweb.HttpServer) {
37-
server.RegisterHandler("Index", Index)
38-
server.RegisterHandler("DefaultError", DefaultError)
39-
server.RegisterHandler("Redirect", Redirect)
37+
server.Router().RegisterHandler("Index", Index)
38+
server.Router().RegisterHandler("DefaultError", DefaultError)
39+
server.Router().RegisterHandler("Redirect", Redirect)
4040
}

example/httpmodule/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func Index(ctx *dotweb.HttpContext) {
5858
}
5959

6060
func InitRoute(server *dotweb.HttpServer) {
61-
server.GET("/", Index)
61+
server.Router().GET("/", Index)
6262
}
6363

6464
func InitModule(dotserver *dotweb.DotWeb) {

example/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ func Redirect(ctx *dotweb.HttpContext) {
8181
}
8282

8383
func InitRoute(server *dotweb.HttpServer) {
84-
server.GET("/", Index)
85-
server.POST("/keypost", KeyPost)
86-
server.POST("/jsonpost", JsonPost)
87-
server.GET("/error", DefaultError)
88-
server.GET("/redirect", Redirect)
89-
server.RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
84+
server.Router().GET("/", Index)
85+
server.Router().POST("/keypost", KeyPost)
86+
server.Router().POST("/jsonpost", JsonPost)
87+
server.Router().GET("/error", DefaultError)
88+
server.Router().GET("/redirect", Redirect)
89+
server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
9090
}
9191

9292
func InitModule(dotserver *dotweb.DotWeb) {

example/session/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ func TestSession(ctx *dotweb.HttpContext) {
6666
}
6767

6868
func InitRoute(server *dotweb.HttpServer) {
69-
server.GET("/", TestSession)
69+
server.Router().GET("/", TestSession)
7070
}

example/uploadfile/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
}
3636

3737
func InitRoute(server *dotweb.HttpServer) {
38-
server.POST("/file", FileUpload)
38+
server.Router().POST("/file", FileUpload)
3939
}
4040

4141
func FileUpload(ctx *dotweb.HttpContext) {

0 commit comments

Comments
 (0)