From 6f21dfe6bd9fb1c5e0f2e11db8d2e73e213a857b Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Tue, 2 Jan 2018 01:01:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?####=20Version=201.4.1=20*=20dotweb?= =?UTF-8?q?=E6=96=B0=E5=A2=9EExcludeUse=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=E6=8C=87=E5=AE=9A=E6=8E=92?= =?UTF-8?q?=E9=99=A4=E8=B7=AF=E7=94=B1=E7=9A=84=E4=B8=AD=E9=97=B4=E4=BB=B6?= =?UTF-8?q?=EF=BC=8Cfixed=20for=20#94=20=E5=BB=BA=E8=AE=AE=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0exuse=E6=96=B9=E6=B3=95=E7=94=A8=E6=9D=A5=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0exclude=20uri=E5=8A=9F=E8=83=BD=20*=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4dotweb=E9=83=A8=E5=88=86=E5=86=85=E9=83=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=90=8D=E7=A7=B0=20*=20=E9=87=8D=E6=9E=84Middleware?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0,=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=EF=BC=8C=E4=BD=BF=E6=B5=81=E7=A8=8B=E6=9B=B4?= =?UTF-8?q?=E6=B8=85=E6=99=B0=20*=20Node=E6=96=B0=E5=A2=9EAppMiddlewares?= =?UTF-8?q?=E4=B8=8EGroupMiddlewares=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=AD=98?= =?UTF-8?q?=E5=82=A8App=E7=BA=A7=E3=80=81Group=E7=BA=A7=E7=9A=84=E4=B8=AD?= =?UTF-8?q?=E9=97=B4=E4=BB=B6=E5=AE=9E=E4=BE=8B=20*=20Middleware=E6=96=B0?= =?UTF-8?q?=E5=A2=9EExclude=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=20*=20Excl?= =?UTF-8?q?ude(routers=20...string)=E5=87=BD=E6=95=B0=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=8C=87=E5=AE=9A=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=20*=20HasExclude()=20bool=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=88=A4=E5=AE=9A=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6=E5=AE=9E=E4=BE=8B=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E6=8E=92=E9=99=A4=E8=B7=AF=E7=94=B1=20*=20Ex?= =?UTF-8?q?istsExcludeRouter(router=20string)=20bool=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=88=A4=E5=AE=9A=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6=E5=AE=9E=E4=BE=8B=E7=9A=84=E6=8E=92?= =?UTF-8?q?=E9=99=A4=E8=B7=AF=E7=94=B1=E4=B8=AD=E6=98=AF=E5=90=A6=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E6=8C=87=E5=AE=9A=E8=B7=AF=E7=94=B1=20*=20middleware?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E4=BC=98=E5=85=88=E7=BA=A7=EF=BC=9A=20-=20?= =?UTF-8?q?=E4=BC=98=E5=85=88=E7=BA=A71=EF=BC=9Aapp=E7=BA=A7=E5=88=ABmiddl?= =?UTF-8?q?eware=20-=20=E4=BC=98=E5=85=88=E7=BA=A72=EF=BC=9Agroup=E7=BA=A7?= =?UTF-8?q?=E5=88=ABmiddleware=20-=20=E4=BC=98=E5=85=88=E7=BA=A7=EF=BC=9Ar?= =?UTF-8?q?outer=E7=BA=A7=E5=88=ABmiddleware=20*=20=E6=9B=B4=E6=96=B0examp?= =?UTF-8?q?le/middleware=20*=202018-01-01=2023:00:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- version.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.MD b/version.MD index aadf3ec..d15f006 100644 --- a/version.MD +++ b/version.MD @@ -1,7 +1,7 @@ ## dotweb版本记录: #### Version 1.4.1 -* dotweb新增ExcludeUse接口,用于设置指定排除路由的中间件 +* dotweb新增ExcludeUse接口,用于设置指定排除路由的中间件,fixed for #94 建议增加exuse方法用来实现exclude uri功能 * 调整dotweb部分内部函数名称 * 重构Middleware实现,优化代码结构,使流程更清晰 * Node新增AppMiddlewares与GroupMiddlewares,用于存储App级、Group级的中间件实例 From 12babb986c9beca2979a853635eaabcc26649585 Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Wed, 3 Jan 2018 21:48:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?####=20Version=201.4.2=20*=20Context?= =?UTF-8?q?=E6=96=B0=E5=A2=9EQueryInt\QueryInt64=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E7=AE=80=E5=8C=96=E8=8E=B7=E5=8F=96?= =?UTF-8?q?Int=E7=B1=BB=E5=9E=8B=E7=9A=84Get=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E5=8F=82=E6=95=B0=E6=9C=AA=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E6=88=96=E4=B8=8D=E6=98=AF=E5=90=88=E6=B3=95=E6=95=B4=E5=BD=A2?= =?UTF-8?q?=EF=BC=8C=E8=BF=94=E5=9B=9E0=20*=20Context=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=9A=E9=99=A4Write=E5=A4=96=EF=BC=8C?= =?UTF-8?q?=E5=85=B6=E4=BB=96WriteXXX=E6=8E=A5=E5=8F=A3=EF=BC=8C=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E4=BB=8E(int,=20error)=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=B8=BAerror=20*=20=E5=B8=A6=E6=9D=A5=E7=9A=84=E5=8F=98?= =?UTF-8?q?=E5=8C=96=EF=BC=9A=20```=20=09app.HttpServer.GET("/index",=20fu?= =?UTF-8?q?nc(ctx=20dotweb.Context)=20error{=20=09=09=5F,=20err=20:=3D=20c?= =?UTF-8?q?tx.WriteString("welcome=20to=20my=20first=20web!")=20=09=09retu?= =?UTF-8?q?rn=20err=20=09})=20```=20=E7=AE=80=E5=8C=96=E4=B8=BA=20```=20?= =?UTF-8?q?=09app.HttpServer.GET("/index",=20func(ctx=20dotweb.Context)=20?= =?UTF-8?q?error{=20=09=09return=20ctx.WriteString("welcome=20to=20my=20fi?= =?UTF-8?q?rst=20web!")=20=09})=20```=20*=20=E5=90=8C=E6=AD=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0example=E4=BB=A3=E7=A0=81=20*=202018-01-03=2022:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++--- context.go | 107 ++++++++++++++++++++++++------------- example/appcontext/main.go | 6 +-- example/bind/main.go | 9 ++-- example/cache/main.go | 6 +-- example/config/main.go | 12 ++--- example/httpmodule/main.go | 3 +- example/main.go | 15 ++---- example/middleware/main.go | 2 +- example/router/main.go | 6 +-- example/session/main.go | 5 +- example/static/main.go | 3 +- version.MD | 19 +++++++ 13 files changed, 119 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index c00dc76..3b71354 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,7 @@ func StartServer() error { app.SetLogPath("/home/logs/wwwroot/") //set route app.HttpServer.GET("/index", func(ctx dotweb.Context) error{ - _, err := ctx.WriteString("welcome to my first web!") - return err + return ctx.WriteString("welcome to my first web!") }) //begin server err := app.StartServer(80) @@ -115,8 +114,7 @@ import ( func main() { dotapp := dotweb.New() dotapp.HttpServer.GET("/hello", func(ctx dotweb.Context) error{ - ctx.WriteString("hello world!") - return nil + return ctx.WriteString("hello world!") }) dotapp.StartServer(80) } @@ -135,14 +133,12 @@ import ( func main() { dotapp := dotweb.New() dotapp.HttpServer.GET("/hello/:name", func(ctx dotweb.Context) error{ - _, err := ctx.WriteString("hello " + ctx.GetRouterName("name")) - return err + return ctx.WriteString("hello " + ctx.GetRouterName("name")) })    dotapp.HttpServer.GET("/news/:category/:newsid", func(ctx dotweb.Context) error{ category := ctx.GetRouterName("category") newsid := ctx.GetRouterName("newsid") -        _, err := ctx.WriteString("news info: category=" + category + " newsid=" + newsid) - return err +        return ctx.WriteString("news info: category=" + category + " newsid=" + newsid) }) dotapp.StartServer(80) } @@ -173,11 +169,10 @@ type UserInfo struct { func TestBind(ctx dotweb.HttpContext) error{ user := new(UserInfo) if err := ctx.Bind(user); err != nil { - ctx.WriteString("err => " + err.Error()) + return ctx.WriteString("err => " + err.Error()) }else{ - ctx.WriteString("TestBind " + fmt.Sprint(user)) + return ctx.WriteString("TestBind " + fmt.Sprint(user)) } - return nil } ``` diff --git a/context.go b/context.go index 0904287..0f5b443 100644 --- a/context.go +++ b/context.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "time" + "strconv" ) const ( @@ -52,6 +53,8 @@ type ( IsEnd() bool Redirect(code int, targetUrl string) error QueryString(key string) string + QueryInt(key string) int + QueryInt64(key string) int64 FormValue(key string) string PostFormValue(key string) string File(file string) (err error) @@ -70,18 +73,18 @@ type ( View(name string) error ViewC(code int, name string) error Write(code int, content []byte) (int, error) - WriteString(contents ...interface{}) (int, error) - WriteStringC(code int, contents ...interface{}) (int, error) - WriteHtml(contents ...interface{}) (int, error) - WriteHtmlC(code int, contents ...interface{}) (int, error) - WriteBlob(contentType string, b []byte) (int, error) - WriteBlobC(code int, contentType string, b []byte) (int, error) - WriteJson(i interface{}) (int, error) - WriteJsonC(code int, i interface{}) (int, error) - WriteJsonBlob(b []byte) (int, error) - WriteJsonBlobC(code int, b []byte) (int, error) - WriteJsonp(callback string, i interface{}) (int, error) - WriteJsonpBlob(callback string, b []byte) (size int, err error) + WriteString(contents ...interface{}) error + WriteStringC(code int, contents ...interface{}) error + WriteHtml(contents ...interface{}) error + WriteHtmlC(code int, contents ...interface{}) error + WriteBlob(contentType string, b []byte) error + WriteBlobC(code int, contentType string, b []byte) error + WriteJson(i interface{}) error + WriteJsonC(code int, i interface{}) error + WriteJsonBlob(b []byte) error + WriteJsonBlobC(code int, b []byte) error + WriteJsonp(callback string, i interface{}) error + WriteJsonpBlob(callback string, b []byte) error } HttpContext struct { @@ -320,6 +323,35 @@ func (ctx *HttpContext) QueryString(key string) string { return ctx.request.QueryString(key) } +// QueryInt get query key with int format +// if not exists or not int type, return 0 +func (ctx *HttpContext) QueryInt(key string) int { + param := ctx.request.QueryString(key) + if param == "" { + return 0 + } + val, err:=strconv.Atoi(param) + if err != nil{ + return 0 + } + return val +} + +// QueryInt64 get query key with int64 format +// if not exists or not int64 type, return 0 +func (ctx *HttpContext) QueryInt64(key string) int64 { + param := ctx.request.QueryString(key) + if param == "" { + return 0 + } + val, err:=strconv.ParseInt(param, 10, 64) + if err != nil{ + return 0 + } + return val +} + + /* * 根据指定key获取包括在post、put和get内的值 */ @@ -484,94 +516,97 @@ func (ctx *HttpContext) Write(code int, content []byte) (int, error) { } // WriteString write (200, string, text/plain) to response -func (ctx *HttpContext) WriteString(contents ...interface{}) (int, error) { +func (ctx *HttpContext) WriteString(contents ...interface{}) error { return ctx.WriteStringC(defaultHttpCode, contents...) } // WriteStringC write (httpCode, string, text/plain) to response -func (ctx *HttpContext) WriteStringC(code int, contents ...interface{}) (int, error) { +func (ctx *HttpContext) WriteStringC(code int, contents ...interface{}) error { content := fmt.Sprint(contents...) return ctx.WriteBlobC(code, MIMETextPlainCharsetUTF8, []byte(content)) } // WriteString write (200, string, text/html) to response -func (ctx *HttpContext) WriteHtml(contents ...interface{}) (int, error) { +func (ctx *HttpContext) WriteHtml(contents ...interface{}) error { return ctx.WriteHtmlC(defaultHttpCode, contents...) } // WriteHtmlC write (httpCode, string, text/html) to response -func (ctx *HttpContext) WriteHtmlC(code int, contents ...interface{}) (int, error) { +func (ctx *HttpContext) WriteHtmlC(code int, contents ...interface{}) error { content := fmt.Sprint(contents...) return ctx.WriteBlobC(code, MIMETextHTMLCharsetUTF8, []byte(content)) } // WriteBlob write []byte content to response -func (ctx *HttpContext) WriteBlob(contentType string, b []byte) (int, error) { +func (ctx *HttpContext) WriteBlob(contentType string, b []byte) error { return ctx.WriteBlobC(defaultHttpCode, contentType, b) } // WriteBlobC write (httpCode, []byte) to response -func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) (int, error) { +func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) error { if contentType != "" { ctx.response.SetContentType(contentType) } if ctx.IsHijack() { - return ctx.hijackConn.WriteBlob(b) + _, err := ctx.hijackConn.WriteBlob(b) + return err } else { - return ctx.response.Write(code, b) + _, err := ctx.response.Write(code, b) + return err } } // WriteJson write (httpCode, json string) to response // auto convert interface{} to json string -func (ctx *HttpContext) WriteJson(i interface{}) (int, error) { +func (ctx *HttpContext) WriteJson(i interface{}) error { return ctx.WriteJsonC(defaultHttpCode, i) } // WriteJsonC write (httpCode, json string) to response // auto convert interface{} to json string -func (ctx *HttpContext) WriteJsonC(code int, i interface{}) (int, error) { +func (ctx *HttpContext) WriteJsonC(code int, i interface{}) error{ b, err := json.Marshal(i) if err != nil { - return 0, err + return err } return ctx.WriteJsonBlobC(code, b) } // WriteJsonBlob write json []byte to response -func (ctx *HttpContext) WriteJsonBlob(b []byte) (int, error) { +func (ctx *HttpContext) WriteJsonBlob(b []byte) error { return ctx.WriteJsonBlobC(defaultHttpCode, b) } // WriteJsonBlobC write (httpCode, json []byte) to response -func (ctx *HttpContext) WriteJsonBlobC(code int, b []byte) (int, error) { +func (ctx *HttpContext) WriteJsonBlobC(code int, b []byte) error { return ctx.WriteBlobC(code, MIMEApplicationJSONCharsetUTF8, b) } // WriteJsonp write jsonp string to response -func (ctx *HttpContext) WriteJsonp(callback string, i interface{}) (int, error) { +func (ctx *HttpContext) WriteJsonp(callback string, i interface{}) error { b, err := json.Marshal(i) if err != nil { - return 0, err + return err } return ctx.WriteJsonpBlob(callback, b) } // WriteJsonpBlob write jsonp string as []byte to response -func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) (size int, err error) { +func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) error { + var err error ctx.response.SetContentType(MIMEApplicationJavaScriptCharsetUTF8) //特殊处理,如果为hijack,需要先行WriteBlob头部 if ctx.IsHijack() { - if size, err = ctx.hijackConn.WriteBlob([]byte(ctx.hijackConn.header + "\r\n")); err != nil { - return + if _, err = ctx.hijackConn.WriteBlob([]byte(ctx.hijackConn.header + "\r\n")); err != nil { + return err } } - if size, err = ctx.WriteBlob("", []byte(callback+"(")); err != nil { - return + if err = ctx.WriteBlob("", []byte(callback+"(")); err != nil { + return err } - if size, err = ctx.WriteBlob("", b); err != nil { - return + if err = ctx.WriteBlob("", b); err != nil { + return err } - size, err = ctx.WriteBlob("", []byte(");")) - return + err = ctx.WriteBlob("", []byte(");")) + return err } diff --git a/example/appcontext/main.go b/example/appcontext/main.go index d924cca..1d063e9 100644 --- a/example/appcontext/main.go +++ b/example/appcontext/main.go @@ -42,8 +42,7 @@ func Index(ctx dotweb.Context) error { gint := ctx.AppContext().GetInt("gint") ctx.AppContext().Set("index", "index-v") ctx.AppContext().Set("user", "user-v") - _, err := ctx.WriteString("index -> " + gstring + ";" + strconv.Itoa(gint)) - return err + return ctx.WriteString("index -> " + gstring + ";" + strconv.Itoa(gint)) } //you can curl http://127.0.0.1:8080/2 @@ -51,8 +50,7 @@ func Index2(ctx dotweb.Context) error { gindex := ctx.AppContext().GetString("index") ctx.AppContext().Remove("index") user, _ := ctx.AppContext().Once("user") - _, err := ctx.WriteString("index -> " + gindex + ";" + fmt.Sprint(user)) - return err + return ctx.WriteString("index -> " + gindex + ";" + fmt.Sprint(user)) } func InitRoute(server *dotweb.HttpServer) { diff --git a/example/bind/main.go b/example/bind/main.go index 69ab2a0..4cd0626 100644 --- a/example/bind/main.go +++ b/example/bind/main.go @@ -48,8 +48,7 @@ func TestBind(ctx dotweb.Context) error { } - _, err := ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user)) - return err + return ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user)) } func GetBind(ctx dotweb.Context) error { @@ -69,8 +68,7 @@ func GetBind(ctx dotweb.Context) error { } - _, err := ctx.WriteString("GetBind [" + errstr + "] " + fmt.Sprint(user)) - return err + return ctx.WriteString("GetBind [" + errstr + "] " + fmt.Sprint(user)) } func PostJsonBind(ctx dotweb.Context) error{ @@ -86,8 +84,7 @@ func PostJsonBind(ctx dotweb.Context) error{ } - _, err := ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user)) - return err + return ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user)) } func InitRoute(server *dotweb.HttpServer) { diff --git a/example/cache/main.go b/example/cache/main.go index 4624d0d..f1b7f51 100644 --- a/example/cache/main.go +++ b/example/cache/main.go @@ -50,8 +50,7 @@ func One(ctx dotweb.Context) error { g = err.Error() } _, err = ctx.Cache().Incr("count") - _, err = ctx.WriteString("One [" + g + "] " + fmt.Sprint(err)) - return err + return ctx.WriteString("One [" + g + "] " + fmt.Sprint(err)) } func Two(ctx dotweb.Context) error { @@ -61,8 +60,7 @@ func Two(ctx dotweb.Context) error { } _, err = ctx.Cache().Incr("count") c, _ := ctx.Cache().GetString("count") - _, err = ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err)) - return err + return ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err)) } func InitRoute(server *dotweb.HttpServer) { diff --git a/example/config/main.go b/example/config/main.go index 40cb5c6..f291177 100644 --- a/example/config/main.go +++ b/example/config/main.go @@ -40,14 +40,12 @@ func main() { 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 + return ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares())) } func GetAppSet(ctx dotweb.Context) error { key := ctx.QueryString("key") - _, err := ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key)) - return err + return ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key)) } func DefaultPanic(ctx dotweb.Context) error { @@ -65,13 +63,11 @@ func Redirect(ctx dotweb.Context) error { } func Login(ctx dotweb.Context) error { - _, err := ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares())) - return err + return ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares())) } func Logout(ctx dotweb.Context) error { - _, err := ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares())) - return err + return ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares())) } func RegisterHandler(server *dotweb.HttpServer) { diff --git a/example/httpmodule/main.go b/example/httpmodule/main.go index e5c92f6..ffc8d8a 100644 --- a/example/httpmodule/main.go +++ b/example/httpmodule/main.go @@ -54,8 +54,7 @@ func main() { func Index(ctx dotweb.Context) error { ctx.Items().Set("count", 2) ctx.WriteString(ctx.Request().Path() + ":Items.Count=> " + ctx.Items().GetString("count")) - _, err := ctx.WriteString("\r\n") - return err + return ctx.WriteString("\r\n") } func WHtml(ctx dotweb.Context) error { diff --git a/example/main.go b/example/main.go index 9ce5c0c..b1f79b6 100644 --- a/example/main.go +++ b/example/main.go @@ -98,35 +98,30 @@ func Time(ctx dotweb.Context) error { func IndexReg(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") - _, err := ctx.WriteString("welcome to dotweb") - return err + return ctx.WriteString("welcome to dotweb") } func IndexParam(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") - _, err := ctx.WriteString("IndexParam", ctx.GetRouterName("id")) - return err + 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") - _, err := ctx.WriteString("username:" + username1 + " - " + username2 + " - " + username3) - return err + return ctx.WriteString("username:" + username1 + " - " + username2 + " - " + username3) } func JsonPost(ctx dotweb.Context) error { - _, err := ctx.WriteString("body:" + string(ctx.Request().PostBody())) - return err + return ctx.WriteString("body:" + string(ctx.Request().PostBody())) } func DefaultError(ctx dotweb.Context) error { //panic("my panic error!") i := 0 b := 2 / i - _, err := ctx.WriteString(b) - return err + return ctx.WriteString(b) } func Redirect(ctx dotweb.Context) error { diff --git a/example/middleware/main.go b/example/middleware/main.go index b37a7a5..72b3d55 100644 --- a/example/middleware/main.go +++ b/example/middleware/main.go @@ -50,7 +50,7 @@ func main() { 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 => ", fmt.Sprint(ctx.RouterNode().Middlewares())) + err := ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares())) fmt.Println(ctx.RouterNode().GroupMiddlewares()) return err } diff --git a/example/router/main.go b/example/router/main.go index c4bd804..eb490e3 100644 --- a/example/router/main.go +++ b/example/router/main.go @@ -32,14 +32,12 @@ func main() { func Index(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") flag := ctx.HttpServer().Router().MatchPath(ctx, "/d/:x/y") - _, err := ctx.WriteString("index - " + ctx.Request().Method + " - " + fmt.Sprint(flag)) - return err + return ctx.WriteString("index - " + ctx.Request().Method + " - " + fmt.Sprint(flag)) } func Any(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") - _, err := ctx.WriteString("any - " + ctx.Request().Method) - return err + return ctx.WriteString("any - " + ctx.Request().Method) } func InitRoute(server *dotweb.HttpServer) { diff --git a/example/session/main.go b/example/session/main.go index 2ba703d..843aeac 100644 --- a/example/session/main.go +++ b/example/session/main.go @@ -56,7 +56,7 @@ func TestSession(ctx dotweb.Context) error { ctx.WriteString("session read failed, get nil", "\r\n") } - _, err = ctx.WriteString("userinfo=>" + fmt.Sprintln(userRead)) + return ctx.WriteString("userinfo=>" + fmt.Sprintln(userRead)) return err } @@ -73,8 +73,7 @@ func TestReadSession(ctx dotweb.Context) error { ctx.WriteString("session read failed, get nil", "\r\n") } - _, err := ctx.WriteString("userinfo=>" + fmt.Sprintln(userRead)) - return err + return ctx.WriteString("userinfo=>" + fmt.Sprintln(userRead)) } func InitRoute(server *dotweb.HttpServer) { diff --git a/example/static/main.go b/example/static/main.go index 7a2af8f..475dc5b 100644 --- a/example/static/main.go +++ b/example/static/main.go @@ -31,8 +31,7 @@ func main() { func Index(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") - _, err := ctx.WriteString("index") - return err + return ctx.WriteString("index") } func InitRoute(server *dotweb.HttpServer) { diff --git a/version.MD b/version.MD index d15f006..06e16b5 100644 --- a/version.MD +++ b/version.MD @@ -1,5 +1,24 @@ ## dotweb版本记录: +#### Version 1.4.2 +* Context新增QueryInt\QueryInt64接口,用于简化获取Int类型的Get参数,如果参数未传入或不是合法整形,返回0 +* Context接口调整:除Write外,其他WriteXXX接口,返回值从(int, error)调整为error +* 带来的变化: +``` + app.HttpServer.GET("/index", func(ctx dotweb.Context) error{ + _, err := ctx.WriteString("welcome to my first web!") + return err + }) +``` +简化为 +``` + app.HttpServer.GET("/index", func(ctx dotweb.Context) error{ + return ctx.WriteString("welcome to my first web!") + }) +``` +* 同步更新example代码 +* 2018-01-03 22:00 + #### Version 1.4.1 * dotweb新增ExcludeUse接口,用于设置指定排除路由的中间件,fixed for #94 建议增加exuse方法用来实现exclude uri功能 * 调整dotweb部分内部函数名称