Go语言Web框架gwk介绍 (三)

 

上一篇忘了ChanResult

ChanResult

可以用来模拟BigPipe,定义如下

  1. type ChanResult struct {
  2. Wait sync.WaitGroup
  3. Chan chan string
  4. ContentType string
  5. Start []byte
  6. End []byte
  7. Timeout time.Duration
  8. }

ChanResult会先输出Start,然后读取Chan中的字符串输出到客户端,最后输出End。

还忘了Controller的事件。

如果Controller的每个Action执行前后需要执行一些相同的代码怎么办?这就需要用到ActionSubscriber接口:

  1. type ActionSubscriber interface {
  2. OnActionExecuting(action *ActionContext)
  3. OnActionExecuted(action *ActionContext)
  4. OnException(action *ActionContext)
  5. }

OnActionExecuting在具体的Action执行之前执行,OnActionExecuted在具体的Action执行之后执行,OnException在具体的Action执行出错后执行。

通过ActionSubscriber可以做权限验证,数据验证,记录日志,同一错误处理等等。

------

GWK内部机制

gwk的内部逻辑十分简单,它的核心对象是HttpServer,HttpContent, HttpProcessor。下面分别介绍。

HttpServer

前面的例子里已经演示了怎么用函数NewDefaultServer创建一个缺省配置的HttpServer实例,NewDefaultServer会调用函数ReadDefaultConfigFile来尝试读取默认的配置文件,如果./conf/web.conf存在,则解析这个配置文件,如果解析出错或者文件不存在则调用函数NewDefaultConfig获得缺省配置。

你也可以用NewHttpServer传入WebConfig参数创建HttpServer实例。

  1. func NewHttpServer(config *WebConfig) (srv *HttpServer, err error)

WebConfig的定义在后面介绍。

创建好HttpServer后,调用它的Start方法来监听http请求,启动Web服务。如果你的代码运行在Google App Engine这样不需要监听端口的平台,可以调用Setup方法来初始化HttpServer。Start方法内部实际上是先调用Setup方法,再调用http.Server的ListenAndServe方法。

HttpServer内部会创建一个http.Server实例,可以通过InnerServer方法来获得这个http.Server。

HttpServer有一个Variables字段,如果你有什么整个HttpServer共享的全局变量,可以放在Variables中。

  1. //server variables
  2. Variables map[string]interface{}

HttpServer实现了http.Handler接口,ServeHTTP函数的内部流程是:

  1. 创建HttpContext
  2. 循环执行每一个HttpProcessor的Execute方法

我们先介绍HttpContext。

HttpContext

HttpContext是对当前http请求的封装,定义如下:

  1. type HttpContext struct {
  2. Server *HttpServer
  3. Request *http.Request
  4. Resonse http.ResponseWriter
  5. Method string
  6. RequestPath string
  7. PhysicalPath string
  8. RouteData RouteData
  9. ViewData map[string]interface{}
  10. Result HttpResult
  11. Error error
  12. Flash map[string]interface{}
  13. Session Session
  14. SessionIsNew bool
  15. }
  • Server: 当前的HttpServer
  • Request: http.Request
  • Resonse: http.ResponseWriter
  • Method: http请求的method,比如GET,PUT,DELETE,POST...
  • RequestPath: http请求url的path部分
  • PhysicalPath: 请求对应的物理文件,只有请求的是静态文件时,该字段才有值
  • RouteData: RequestPath解析后的路由参数
  • ViewData: 存放传递给View模板的数据
  • Result: 此次请求的HttpResult
  • Error: 此次请求中的Error
  • Flash: 可以存放临时变量,生命周期为此次请求
  • Session: Session
  • SessionIsNew: Session是否此次在请求创建

HttpContext还定义了若干方法简化一些常见的操作:

RouteValue,读取RouteData里的数据

  1. func (ctx *HttpContext) RouteValue(name string) (string, bool)

FormValue,调用http.Request的FormValue,FV也是相同的逻辑

  1. func (ctx *HttpContext) FV(name string) string
  2. func (ctx *HttpContext) FormValue(name string) string

另外还有FormInt,FormIntOr,FormBool,FormBoolOr,FormFloat,FormFloatOr,前面已经做过介绍。

ReqHeader,读取Http request header的数据

  1. func (ctx *HttpContext) ReqHeader(name string) string

SetHeader,设置Http resonse header的数据

  1. func (ctx *HttpContext) SetHeader(key string, value string)

AddHeader,向http response header添加数据

  1. func (ctx *HttpContext) AddHeader(key string, value string)

ContentType,设置http response header的"Content-Type"

  1. func (ctx *HttpContext) ContentType(ctype string)

Status,设置返回http status code

  1. func (ctx *HttpContext) Status(code int)

Accept,读取http request header的"Accept"

  1. func (ctx *HttpContext) Accept() string

Write,调用http response的Write方法

  1. func (ctx *HttpContext) Write(b []byte) (int, error)

Expires,设置http response header的"Expires"

  1. func (ctx *HttpContext) Expires(t string)

SetCookie,设置cookie

  1. func (ctx *HttpContext) SetCookie(cookie *http.Cookie)

Cookie,读取Cookie

  1. func (ctx *HttpContext) Cookie(name string) (*http.Cookie, error)

SessionId,返回SessionId,只有启用了Session才有效

  1. func (ctx *HttpContext) SessionId() string

GetFlash,读取Flash中的变量

  1. func (ctx *HttpContext) GetFlash(key string) (v interface{}, ok bool)

SetFlash,设置Flash中的变量

  1. func (ctx *HttpContext) SetFlash(key string, v interface{})

ReadBody,读取整个http request的内容

  1. func (ctx *HttpContext) ReadBody() ([]byte, error)

Flush,Flush当前Response中的数据到客户端

  1. func (ctx *HttpContext) Flush() {

前面介绍的ChanResult就是调用Flush把内容输出到客户端,代码基本逻辑如下:

  1. ctx.Write(c.Start)
  2. ctx.Flush()
  3. if c.Timeout < time.Millisecond {
  4. c.Timeout = defaultChanResultTimeout
  5. }
  6. waitchan := make(chan bool)
  7. donechan := make(chan bool)
  8. go func() {
  9. for s := range c.Chan {
  10. ctx.Write([]byte(s))
  11. ctx.Flush()
  12. }
  13. donechan <- true
  14. }()
  15. go func() {
  16. c.Wait.Wait()
  17. close(c.Chan)
  18. waitchan <- true
  19. }()
  20. select {
  21. case <-waitchan:
  22. case <-time.After(c.Timeout):
  23. }
  24. <-donechan
  25. ctx.Write(c.End)

HttpProcessor

HttpProcessor的定义如下

  1. type HttpProcessor interface {
  2. Execute(ctx *HttpContext)
  3. Register(server *HttpServer)
  4. }

Execute负责处理http请求,Register会在HttpServer初始化时调用一次,如果你的HttpProcessor需要执行一些初始化代码,可以放在Register方法中。

调用RegisterProcessor可以注册一个HttpProcessor

  1. func RegisterProcessor(name string, p HttpProcessor)

注册的HttpProcessor存在ProcessTable类型的全局变量中

  1. type ProcessTable []*Process
  2. type Process struct {
  3. Name string
  4. Path string
  5. Method string
  6. Handler HttpProcessor
  7. }

如果一个Processor需要特定的条件才执行,可以设置它的Path和Method字段,Method是要匹配的http method,既GET、PUT、POST、DELETE...,"*"或者""匹配所有的http method,Path是要匹配的Request Path,目前版本是前缀匹配,以后可能改成支持通配符。

HttpServer启动时,默认注册三个HttpProcessor:StaticProcessor、RouteProcessor、RenderProcessor。

StaticProcessor

StaticProcessor负责处理静态文件,如果请求的路径能匹配到物理文件,则将HttpContext的的Result设置为FileResult。

StaticProcessor支持缓存静态文件以及自定义http response header。缓存静态文件在缓存一节详细介绍,自定义输出的http header是指为每个静态文件的Response设置你定义的http header,比如统一为静态文件设置Cache-Control。下面是配置的例子:

  1. #static processor config
  2. static_processor: {
  3. cache_enable: true
  4. cache_expire: 3600
  5. header: {
  6. Cache-Control: max-age=43200
  7. X-Title: gwk-demo
  8. }
  9. }
  10. # -->end static processor

RouteProcessor

RouteProcessor负责按照你定义的路由规则调用具体的处理代码,逻辑很简单,只有几十行代码。

RenderProcessor

RenderProcessor负责执行HttpResult的Execute,也只有几十行代码。HttpResult没有赋值的话则返回404错误。

自定义HttpProcessor

你可以增删HttpProcessor或者调整顺序来改变默认的处理逻辑,比如你的程序是一个web api服务,不需要处理静态文件,则可以去掉RouteProcessor。ProcessTable定义了Append、InsertBefore、InsertAfter、Remove方法来简化对HttpProcessor的调整。

http gzip压缩

CompressProcessor可以对http输出做gzip压缩,需要注册到RenderProcessor之前才有效,其本质是用compressResponseWriter来代替默认的ResponseWriter。

  1. type CompressProcessor struct {
  2. Enable bool
  3. Level int
  4. MimeType string
  5. }
  6. type compressResponseWriter struct {
  7. rw http.ResponseWriter
  8. writer compresser
  9. contentType string
  10. format string
  11. headerWritten bool
  12. }

CompressProcessor设计时考虑能够按照MimeType或者RequestPath来过滤需要压缩的内容,但一直没实现,因为访问量小流量小的网站开不开启gzip压缩意义不大,访问量大的网站一般会用一些http反向代理或者http缓存的服务,自己没必要处理gzip压缩。

通过自定义HttpProcessor,你可以为全网站做统一的权限验证,访问限制,日志处理,错误处理等等。

除了HttpProcessor,你还可以通过gwk的事件机制来实现这些逻辑。

 
 
分类: Go
标签: golangWeb FrameworkGWKMVC

Go语言Web框架gwk介绍 3的更多相关文章

  1. Go语言Web框架gwk介绍4

    Go语言Web框架gwk介绍 (四)   事件 gwk支持事件系统,但并没有硬编码有哪些事件,而是采用了比较松散的定义方式. 订阅事件有两种方式: 调用On函数或者OnFunc函数 func On(m ...

  2. Go语言Web框架gwk介绍2

    Go语言Web框架gwk介绍 (二) HttpResult 凡是实现了HttpResult接口的对象,都可以作为gwk返回Web客户端的内容.HttpResult接口定义非常简单,只有一个方法: ty ...

  3. Go语言Web框架gwk介绍 1

    Go语言Web框架gwk介绍 (一)   今天看到Golang排名到前30名了,看来关注的人越来越多了,接下来几天详细介绍Golang一个web开发框架GWK. 现在博客园支持markdown格式发布 ...

  4. Go语言Web框架gwk介绍 (一)

    今天看到Golang排名到前30名了,看来关注的人越来越多了,接下来几天详细介绍Golang一个web开发框架GWK. 现在博客园支持markdown格式发布文章么?后台的编辑器不太好用嘛. GWK ...

  5. Go语言Web框架gwk介绍 (五)

    Session Go的net/http本身不带session的机制,需要开发人员自行实现,gwk实现了内存中的session存储机制,如果需要将session存在其他地方比如redis或者memcac ...

  6. Go语言Web框架gwk介绍 (四)

    事件 gwk支持事件系统,但并没有硬编码有哪些事件,而是采用了比较松散的定义方式. 订阅事件有两种方式: 调用On函数或者OnFunc函数 func On(moudle, name string, h ...

  7. Go语言Web框架gwk介绍 (二)

    HttpResult 凡是实现了HttpResult接口的对象,都可以作为gwk返回Web客户端的内容.HttpResult接口定义非常简单,只有一个方法: type HttpResult inter ...

  8. Go语言Web框架gwk介绍 (三)

    上一篇忘了ChanResult ChanResult 可以用来模拟BigPipe,定义如下 type ChanResult struct { Wait sync.WaitGroup Chan chan ...

  9. 最好的6个Go语言Web框架

    原文:Top 6 web frameworks for Go as of 2017 作者:Edward Marinescu 译者:roy 译者注:本文介绍截至目前(2017年)最好的6个Go语言Web ...

随机推荐

  1. PHP进口Excel至MySQL方法

    PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader 注意点: reader.php 中的以下这行要改动  1.将 ...

  2. CSS3+HTML5特效5 - 震动的文字

    先看效果(把鼠标移上去看看) abcd 这个效果很简单,就是移动文字的位置模拟出震动的效果. Css <style> @-webkit-keyframes shake { 0%{ -web ...

  3. Write the code.Change the world.---WWDC2014

  4. GhostDoc的使用

    原文:GhostDoc的使用 一.简介 GhostDoc是Visual Studio的一个免费插件,可以为开发人员自动生成XML格式的注释文档. 二.下载 需要的朋友可以去这里下载,填个Email地址 ...

  5. MVC验证03-自定义验证规则、禁止输入某些值

    原文:MVC验证03-自定义验证规则.禁止输入某些值 本文继续体验自定义验证规则,需求是禁止输入某些值.本文与前2篇相关,请参考:MVC验证01-基础.远程验证   MVC验证02-自定义验证规则.邮 ...

  6. jQuery.extend()源码解读

    // extend方法为jQuery对象和init对象的prototype扩展方法// 同时具有独立的扩展普通对象的功能jQuery.extend = jQuery.fn.extend = funct ...

  7. sqlserver检测数据库是否能连接的小技巧

    有时候可能需要检测下某台机器的服务是不是起来了,或者某台机器的某个库是不是能被连接又不能打开ssms也不想登陆服务器的话就可以用这个方法. 1.在桌面上右键创建个文本,然后改后缀名为udl以后保存(1 ...

  8. POJ 1201 &amp;&amp; HDU 1384 Intervals(差动制动系统)

    职务地址:POJ 1201   HDU 1384 依据题目意思.能够列出不等式例如以下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路s ...

  9. IE6常见bug

    1.IE6怪异解析之padding与border算入宽高 原因:未加文档声明造成非盒模型解析 解决方法:加入文档声明<!doctype html> 2.IE6在块元素.左右浮动.设定mar ...

  10. 使用oracle的exp命令时,提示出--hash: exp: command not found

    使用oracle的exp命令时,提示出--hash: exp: command not found 原因:当你在终端使用exp的命名时,当前的账户,并不是oracle认可的账户. 在安装oracle时 ...