Skip to content

Commit 129df76

Browse files
authored
Merge pull request devfeel#98 from devfeel/develop
Develop - 完善HttpContext,提升易用性
2 parents 7fb91c6 + 12babb9 commit 129df76

File tree

13 files changed

+120
-92
lines changed

13 files changed

+120
-92
lines changed

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ func StartServer() error {
2525
app.SetLogPath("/home/logs/wwwroot/")
2626
//set route
2727
app.HttpServer.GET("/index", func(ctx dotweb.Context) error{
28-
_, err := ctx.WriteString("welcome to my first web!")
29-
return err
28+
return ctx.WriteString("welcome to my first web!")
3029
})
3130
//begin server
3231
err := app.StartServer(80)
@@ -115,8 +114,7 @@ import (
115114
func main() {
116115
dotapp := dotweb.New()
117116
dotapp.HttpServer.GET("/hello", func(ctx dotweb.Context) error{
118-
ctx.WriteString("hello world!")
119-
return nil
117+
return ctx.WriteString("hello world!")
120118
})
121119
dotapp.StartServer(80)
122120
}
@@ -135,14 +133,12 @@ import (
135133
func main() {
136134
dotapp := dotweb.New()
137135
dotapp.HttpServer.GET("/hello/:name", func(ctx dotweb.Context) error{
138-
_, err := ctx.WriteString("hello " + ctx.GetRouterName("name"))
139-
return err
136+
return ctx.WriteString("hello " + ctx.GetRouterName("name"))
140137
})
141138
   dotapp.HttpServer.GET("/news/:category/:newsid", func(ctx dotweb.Context) error{
142139
category := ctx.GetRouterName("category")
143140
newsid := ctx.GetRouterName("newsid")
144-
       _, err := ctx.WriteString("news info: category=" + category + " newsid=" + newsid)
145-
return err
141+
       return ctx.WriteString("news info: category=" + category + " newsid=" + newsid)
146142
})
147143
dotapp.StartServer(80)
148144
}
@@ -173,11 +169,10 @@ type UserInfo struct {
173169
func TestBind(ctx dotweb.HttpContext) error{
174170
user := new(UserInfo)
175171
if err := ctx.Bind(user); err != nil {
176-
ctx.WriteString("err => " + err.Error())
172+
return ctx.WriteString("err => " + err.Error())
177173
}else{
178-
ctx.WriteString("TestBind " + fmt.Sprint(user))
174+
return ctx.WriteString("TestBind " + fmt.Sprint(user))
179175
}
180-
return nil
181176
}
182177

183178
```

context.go

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"path/filepath"
1616
"time"
17+
"strconv"
1718
)
1819

1920
const (
@@ -52,6 +53,8 @@ type (
5253
IsEnd() bool
5354
Redirect(code int, targetUrl string) error
5455
QueryString(key string) string
56+
QueryInt(key string) int
57+
QueryInt64(key string) int64
5558
FormValue(key string) string
5659
PostFormValue(key string) string
5760
File(file string) (err error)
@@ -70,18 +73,18 @@ type (
7073
View(name string) error
7174
ViewC(code int, name string) error
7275
Write(code int, content []byte) (int, error)
73-
WriteString(contents ...interface{}) (int, error)
74-
WriteStringC(code int, contents ...interface{}) (int, error)
75-
WriteHtml(contents ...interface{}) (int, error)
76-
WriteHtmlC(code int, contents ...interface{}) (int, error)
77-
WriteBlob(contentType string, b []byte) (int, error)
78-
WriteBlobC(code int, contentType string, b []byte) (int, error)
79-
WriteJson(i interface{}) (int, error)
80-
WriteJsonC(code int, i interface{}) (int, error)
81-
WriteJsonBlob(b []byte) (int, error)
82-
WriteJsonBlobC(code int, b []byte) (int, error)
83-
WriteJsonp(callback string, i interface{}) (int, error)
84-
WriteJsonpBlob(callback string, b []byte) (size int, err error)
76+
WriteString(contents ...interface{}) error
77+
WriteStringC(code int, contents ...interface{}) error
78+
WriteHtml(contents ...interface{}) error
79+
WriteHtmlC(code int, contents ...interface{}) error
80+
WriteBlob(contentType string, b []byte) error
81+
WriteBlobC(code int, contentType string, b []byte) error
82+
WriteJson(i interface{}) error
83+
WriteJsonC(code int, i interface{}) error
84+
WriteJsonBlob(b []byte) error
85+
WriteJsonBlobC(code int, b []byte) error
86+
WriteJsonp(callback string, i interface{}) error
87+
WriteJsonpBlob(callback string, b []byte) error
8588
}
8689

8790
HttpContext struct {
@@ -320,6 +323,35 @@ func (ctx *HttpContext) QueryString(key string) string {
320323
return ctx.request.QueryString(key)
321324
}
322325

326+
// QueryInt get query key with int format
327+
// if not exists or not int type, return 0
328+
func (ctx *HttpContext) QueryInt(key string) int {
329+
param := ctx.request.QueryString(key)
330+
if param == "" {
331+
return 0
332+
}
333+
val, err:=strconv.Atoi(param)
334+
if err != nil{
335+
return 0
336+
}
337+
return val
338+
}
339+
340+
// QueryInt64 get query key with int64 format
341+
// if not exists or not int64 type, return 0
342+
func (ctx *HttpContext) QueryInt64(key string) int64 {
343+
param := ctx.request.QueryString(key)
344+
if param == "" {
345+
return 0
346+
}
347+
val, err:=strconv.ParseInt(param, 10, 64)
348+
if err != nil{
349+
return 0
350+
}
351+
return val
352+
}
353+
354+
323355
/*
324356
* 根据指定key获取包括在post、put和get内的值
325357
*/
@@ -484,94 +516,97 @@ func (ctx *HttpContext) Write(code int, content []byte) (int, error) {
484516
}
485517

486518
// WriteString write (200, string, text/plain) to response
487-
func (ctx *HttpContext) WriteString(contents ...interface{}) (int, error) {
519+
func (ctx *HttpContext) WriteString(contents ...interface{}) error {
488520
return ctx.WriteStringC(defaultHttpCode, contents...)
489521
}
490522

491523
// WriteStringC write (httpCode, string, text/plain) to response
492-
func (ctx *HttpContext) WriteStringC(code int, contents ...interface{}) (int, error) {
524+
func (ctx *HttpContext) WriteStringC(code int, contents ...interface{}) error {
493525
content := fmt.Sprint(contents...)
494526
return ctx.WriteBlobC(code, MIMETextPlainCharsetUTF8, []byte(content))
495527
}
496528

497529
// WriteString write (200, string, text/html) to response
498-
func (ctx *HttpContext) WriteHtml(contents ...interface{}) (int, error) {
530+
func (ctx *HttpContext) WriteHtml(contents ...interface{}) error {
499531
return ctx.WriteHtmlC(defaultHttpCode, contents...)
500532
}
501533

502534
// WriteHtmlC write (httpCode, string, text/html) to response
503-
func (ctx *HttpContext) WriteHtmlC(code int, contents ...interface{}) (int, error) {
535+
func (ctx *HttpContext) WriteHtmlC(code int, contents ...interface{}) error {
504536
content := fmt.Sprint(contents...)
505537
return ctx.WriteBlobC(code, MIMETextHTMLCharsetUTF8, []byte(content))
506538
}
507539

508540
// WriteBlob write []byte content to response
509-
func (ctx *HttpContext) WriteBlob(contentType string, b []byte) (int, error) {
541+
func (ctx *HttpContext) WriteBlob(contentType string, b []byte) error {
510542
return ctx.WriteBlobC(defaultHttpCode, contentType, b)
511543
}
512544

513545
// WriteBlobC write (httpCode, []byte) to response
514-
func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) (int, error) {
546+
func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) error {
515547
if contentType != "" {
516548
ctx.response.SetContentType(contentType)
517549
}
518550
if ctx.IsHijack() {
519-
return ctx.hijackConn.WriteBlob(b)
551+
_, err := ctx.hijackConn.WriteBlob(b)
552+
return err
520553
} else {
521-
return ctx.response.Write(code, b)
554+
_, err := ctx.response.Write(code, b)
555+
return err
522556
}
523557
}
524558

525559
// WriteJson write (httpCode, json string) to response
526560
// auto convert interface{} to json string
527-
func (ctx *HttpContext) WriteJson(i interface{}) (int, error) {
561+
func (ctx *HttpContext) WriteJson(i interface{}) error {
528562
return ctx.WriteJsonC(defaultHttpCode, i)
529563
}
530564

531565
// WriteJsonC write (httpCode, json string) to response
532566
// auto convert interface{} to json string
533-
func (ctx *HttpContext) WriteJsonC(code int, i interface{}) (int, error) {
567+
func (ctx *HttpContext) WriteJsonC(code int, i interface{}) error{
534568
b, err := json.Marshal(i)
535569
if err != nil {
536-
return 0, err
570+
return err
537571
}
538572
return ctx.WriteJsonBlobC(code, b)
539573
}
540574

541575
// WriteJsonBlob write json []byte to response
542-
func (ctx *HttpContext) WriteJsonBlob(b []byte) (int, error) {
576+
func (ctx *HttpContext) WriteJsonBlob(b []byte) error {
543577
return ctx.WriteJsonBlobC(defaultHttpCode, b)
544578
}
545579

546580
// WriteJsonBlobC write (httpCode, json []byte) to response
547-
func (ctx *HttpContext) WriteJsonBlobC(code int, b []byte) (int, error) {
581+
func (ctx *HttpContext) WriteJsonBlobC(code int, b []byte) error {
548582
return ctx.WriteBlobC(code, MIMEApplicationJSONCharsetUTF8, b)
549583
}
550584

551585
// WriteJsonp write jsonp string to response
552-
func (ctx *HttpContext) WriteJsonp(callback string, i interface{}) (int, error) {
586+
func (ctx *HttpContext) WriteJsonp(callback string, i interface{}) error {
553587
b, err := json.Marshal(i)
554588
if err != nil {
555-
return 0, err
589+
return err
556590
}
557591
return ctx.WriteJsonpBlob(callback, b)
558592
}
559593

560594
// WriteJsonpBlob write jsonp string as []byte to response
561-
func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) (size int, err error) {
595+
func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) error {
596+
var err error
562597
ctx.response.SetContentType(MIMEApplicationJavaScriptCharsetUTF8)
563598
//特殊处理,如果为hijack,需要先行WriteBlob头部
564599
if ctx.IsHijack() {
565-
if size, err = ctx.hijackConn.WriteBlob([]byte(ctx.hijackConn.header + "\r\n")); err != nil {
566-
return
600+
if _, err = ctx.hijackConn.WriteBlob([]byte(ctx.hijackConn.header + "\r\n")); err != nil {
601+
return err
567602
}
568603
}
569-
if size, err = ctx.WriteBlob("", []byte(callback+"(")); err != nil {
570-
return
604+
if err = ctx.WriteBlob("", []byte(callback+"(")); err != nil {
605+
return err
571606
}
572-
if size, err = ctx.WriteBlob("", b); err != nil {
573-
return
607+
if err = ctx.WriteBlob("", b); err != nil {
608+
return err
574609
}
575-
size, err = ctx.WriteBlob("", []byte(");"))
576-
return
610+
err = ctx.WriteBlob("", []byte(");"))
611+
return err
577612
}

example/appcontext/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ func Index(ctx dotweb.Context) error {
4242
gint := ctx.AppContext().GetInt("gint")
4343
ctx.AppContext().Set("index", "index-v")
4444
ctx.AppContext().Set("user", "user-v")
45-
_, err := ctx.WriteString("index -> " + gstring + ";" + strconv.Itoa(gint))
46-
return err
45+
return ctx.WriteString("index -> " + gstring + ";" + strconv.Itoa(gint))
4746
}
4847

4948
//you can curl http://127.0.0.1:8080/2
5049
func Index2(ctx dotweb.Context) error {
5150
gindex := ctx.AppContext().GetString("index")
5251
ctx.AppContext().Remove("index")
5352
user, _ := ctx.AppContext().Once("user")
54-
_, err := ctx.WriteString("index -> " + gindex + ";" + fmt.Sprint(user))
55-
return err
53+
return ctx.WriteString("index -> " + gindex + ";" + fmt.Sprint(user))
5654
}
5755

5856
func InitRoute(server *dotweb.HttpServer) {

example/bind/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ func TestBind(ctx dotweb.Context) error {
4848

4949
}
5050

51-
_, err := ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user))
52-
return err
51+
return ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user))
5352
}
5453

5554
func GetBind(ctx dotweb.Context) error {
@@ -69,8 +68,7 @@ func GetBind(ctx dotweb.Context) error {
6968

7069
}
7170

72-
_, err := ctx.WriteString("GetBind [" + errstr + "] " + fmt.Sprint(user))
73-
return err
71+
return ctx.WriteString("GetBind [" + errstr + "] " + fmt.Sprint(user))
7472
}
7573

7674
func PostJsonBind(ctx dotweb.Context) error{
@@ -86,8 +84,7 @@ func PostJsonBind(ctx dotweb.Context) error{
8684

8785
}
8886

89-
_, err := ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user))
90-
return err
87+
return ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user))
9188
}
9289

9390
func InitRoute(server *dotweb.HttpServer) {

example/cache/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ func One(ctx dotweb.Context) error {
5050
g = err.Error()
5151
}
5252
_, err = ctx.Cache().Incr("count")
53-
_, err = ctx.WriteString("One [" + g + "] " + fmt.Sprint(err))
54-
return err
53+
return ctx.WriteString("One [" + g + "] " + fmt.Sprint(err))
5554
}
5655

5756
func Two(ctx dotweb.Context) error {
@@ -61,8 +60,7 @@ func Two(ctx dotweb.Context) error {
6160
}
6261
_, err = ctx.Cache().Incr("count")
6362
c, _ := ctx.Cache().GetString("count")
64-
_, err = ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err))
65-
return err
63+
return ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err))
6664
}
6765

6866
func InitRoute(server *dotweb.HttpServer) {

example/config/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,12 @@ func main() {
4040

4141
func Index(ctx dotweb.Context) error {
4242
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
43-
_, err := ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
44-
return err
43+
return ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
4544
}
4645

4746
func GetAppSet(ctx dotweb.Context) error {
4847
key := ctx.QueryString("key")
49-
_, err := ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key))
50-
return err
48+
return ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key))
5149
}
5250

5351
func DefaultPanic(ctx dotweb.Context) error {
@@ -65,13 +63,11 @@ func Redirect(ctx dotweb.Context) error {
6563
}
6664

6765
func Login(ctx dotweb.Context) error {
68-
_, err := ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
69-
return err
66+
return ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
7067
}
7168

7269
func Logout(ctx dotweb.Context) error {
73-
_, err := ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
74-
return err
70+
return ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
7571
}
7672

7773
func RegisterHandler(server *dotweb.HttpServer) {

example/httpmodule/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ func main() {
5454
func Index(ctx dotweb.Context) error {
5555
ctx.Items().Set("count", 2)
5656
ctx.WriteString(ctx.Request().Path() + ":Items.Count=> " + ctx.Items().GetString("count"))
57-
_, err := ctx.WriteString("\r\n")
58-
return err
57+
return ctx.WriteString("\r\n")
5958
}
6059

6160
func WHtml(ctx dotweb.Context) error {

0 commit comments

Comments
 (0)