dotweb屬於一個Web框架,但願經過框架行爲,幫助開發人員快速構建Web應用,提高開發效率,減小沒必要要的代碼臃腫。html
框架地址:https://github.com/devfeel/dotwebgit
dotweb包含如下幾個經常使用對象:github
本章主要對HttpContext對象展開介紹。web
HttpContext實現Context接口,主要承擔單次請求處理中請求信息、響應信息、全局對象的快捷功能與惟一入口。json
主要方法:緩存
方法 | 描述 |
HttpServer() |
獲取當前請求所屬HttpServer對象 |
Response() |
獲取當前請求的Response對象 |
Request() |
獲取當前請求的Request對象 |
WebSocket() |
若是是WebSocket鏈接,返回WebSocket對象 |
HijackConn() |
若是是Hijack請求,返回Hijack鏈接對象 |
AppContext() |
返回全局對象容器 |
Cache() |
返回全局緩存對象 |
Items() |
返回當前請求流程內有效的對象容器 |
ViewData() |
返回用於模板數據傳輸的對象容器 |
Session() |
返回當前請求有效的Session對象 |
Redirect() |
提供跳轉支持,默認建議302跳轉 |
QueryString() |
指定Key查詢Get參數的值 |
PostFormValue() |
指定Key查詢Post參數的值 |
GetRouterName() |
指定Key查詢動態路由值 |
ReadCookie() |
指定Key讀取Cookie對象 |
Bind() |
將Json、Xml、Form提交的屬性綁定指定結構體 |
Write() |
指定狀態碼輸出二進制內容 |
WriteString()\WriteStringC() |
輸出字符串,默認text/plain,其中以C結尾的方法支持設置狀態碼 |
WriteHtml()\WriteHtmlC() |
輸出Html字符串,默認text/html,其中以C結尾的方法支持設置狀態碼 |
WriteJson()\WriteJsonC() |
輸出Json字符串,默認application/json,其中以C結尾的方法支持設置狀態碼 |
WriteJsonp() |
輸出適配Jsonp的字符串 |
View()ViewC() |
指定模板名稱輸出Html內容,其中以C結尾的方法支持設置狀態碼 |
經常使用功能示例:服務器
一、獲取Get參數值session
func Index(ctx dotweb.Context) error { userid := ctx.QueryString("userid") ctx.WriteString(userid) return nil }
二、獲取Post參數值app
func Index(ctx dotweb.Context) error { userid := ctx.PostFormValue("userid") ctx.WriteString(userid) return nil }
三、獲取Post Body框架
func Index(ctx dotweb.Context) error { data := ctx.Request().PostBody() ctx.Write(200, data) return nil }
四、獲取上傳的文件
func Index(ctx dotweb.Context) error { file, err := ctx.Request().FormFile("filekey") if err != nil { ctx.WriteString("upload file error:", err.Error()) } else { ctx.WriteString(file.FileName()) } return nil }
五、讀取Cookie
func Index(ctx dotweb.Context) error { c, err := ctx.ReadCookie("UserName") if err!= nil{ ctx.WriteString(err.Error()) }else { ctx.WriteString(c.Value) } return nil }
六、寫入Session值
func Index(ctx dotweb.Context) error { ctx.Session().Set("UserID", 1) ctx.WriteString("set session success") return nil }
七、輸出字符串(默認200狀態碼)
func Index(ctx dotweb.Context) error { ctx.WriteString("welcome to dotweb") return nil }
八、輸出Json字符串(默認200狀態碼)
func Index(ctx dotweb.Context) error { type User struct { UserName string Age int } u:=&User{ UserName:"dotweb", Age:1, } ctx.WriteJson(u) return nil }
九、指定模板名稱輸出Html字符串
type UserInfo struct { UserName string Sex bool } type BookInfo struct { Name string Size int64 } func TestView(ctx dotweb.Context) error { ctx.ViewData().Set("data", "圖書信息") ctx.ViewData().Set("user", &UserInfo{UserName: "user1", Sex: true}) m := make([]*BookInfo, 5) m[0] = &BookInfo{Name: "book0", Size: 1} m[1] = &BookInfo{Name: "book1", Size: 10} m[2] = &BookInfo{Name: "book2", Size: 100} m[3] = &BookInfo{Name: "book3", Size: 1000} m[4] = &BookInfo{Name: "book4", Size: 10000} ctx.ViewData().Set("Books", m) err := ctx.View("testview.html") return err }
十、跳轉地址
func Redirect(ctx dotweb.Context) error { err := ctx.Redirect(http.StatusFound, "http://www.baidu.com") if err != nil { ctx.WriteString(err) } return err }
十一、設置Header
func Index(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") ctx.WriteString("welcome to dotweb") return nil }
以上簡單示例,展現瞭如何經過Context獲取請求信息,設置輸出信息,使用Session等。
更多代碼可參考 https://github.com/devfeel/dotweb-example
歡迎各位加入咱們的go語言QQ羣:193409346