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