惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Recent Announcements
Recent Announcements
H
Help Net Security
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
小众软件
小众软件
Last Week in AI
Last Week in AI
U
Unit 42
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
云风的 BLOG
云风的 BLOG
V
V2EX
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿

博客园 - 基点项目师

dotweb now released to Version 1.5 dotweb框架之旅 [三] - 常用对象-HttpServer dotweb框架之旅 [二] - 常用对象-App(dotweb) dotweb框架之旅 [一] - HelloWorld 对 dotweb 框架进行统一的自定义错误处理 go服务端----使用dotweb框架搭建简易服务 对Golang有兴趣的朋友,推荐一款go语言Web框架-dotweb Nginx日志文件切割 linux安装PHP-memcache-redis扩展 微信公众平台开发 - 页面自适应 linux 安装 mongodb linux 安装 node.js nginx支持php配置 Linux 安装 php linux 安装nginx 如何设计自适应网页 ThinkDev.Data更新日志 ThinkDev.Logging-Queue模块介绍 Windows准备Node.js运行与开发环境
dotweb框架之旅 [四] - 常用对象-HttpContext
基点项目师 · 2017-10-22 · via 博客园 - 基点项目师

dotweb属于一个Web框架,希望通过框架行为,帮助开发人员快速构建Web应用,提升开发效率,减少不必要的代码臃肿。

框架地址:https://github.com/devfeel/dotweb

dotweb包含以下几个常用对象:

  • App(dotweb) App容器,为Web请求处理提供必要的容器类功能。
  • HttpServer 用于真正处理Web请求的服务模块。
  • HttpContext 用于提供单次请求处理中请求信息与响应信息的快捷处理与唯一入口。
  • Response 用于从服务器向用户发送输出的结果。
  • Request 用于从用户那里取得信息。
  • Session 用于存储关于某个连接会话的信息,或者修改相关的设置。目前支持存储本机内存与Redis分布式。

本章主要对HttpContext对象展开介绍。

HttpContext实现Context接口,主要承担单次请求处理中请求信息、响应信息、全局对象的快捷功能与唯一入口。

主要方法

方法 描述
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结尾的方法支持设置状态码

常用功能示例:

1、获取Get参数值

func Index(ctx dotweb.Context) error {
    userid := ctx.QueryString("userid")
    ctx.WriteString(userid)
    return nil
}

 2、获取Post参数值

func Index(ctx dotweb.Context) error {
    userid := ctx.PostFormValue("userid")
    ctx.WriteString(userid)
    return nil
}

 3、获取Post Body

func Index(ctx dotweb.Context) error {
    data := ctx.Request().PostBody()
    ctx.Write(200, data)
    return nil
}

 4、获取上传的文件

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
}

 5、读取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
}

 6、写入Session值

func Index(ctx dotweb.Context) error {
    ctx.Session().Set("UserID", 1)
    ctx.WriteString("set session success")
    return nil
}

 7、输出字符串(默认200状态码)

func Index(ctx dotweb.Context) error {
    ctx.WriteString("welcome to dotweb")
    return nil
}

 8、输出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
}

 9、指定模板名称输出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
}

 10、跳转地址

func Redirect(ctx dotweb.Context) error {
    err := ctx.Redirect(http.StatusFound, "http://www.baidu.com")
    if err != nil {
        ctx.WriteString(err)
    }
    return err
}

 11、设置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