一个简单GOLANG路由

x_focus · · 5549 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

package controllers

import (
    . "logger"
    "web"
)

//对象继承直接拥用REST标准接口
type login struct {
    web.ControllerBase
}

func (this *login) Get() {
    this.Template["key"] = "value"
    this.WriteString("GET:", this.Template)

}
func (this *login) Post() {
    this.WriteString("POST:", this.Template)
}

//对象不继承注册一个方法到路由
type signOut struct {
}

func (this *signOut) signOutGet(ctx *web.HttpContext) {
    ctx.WriteString("signOutGet:", ctx.Template)

}
func (this *signOut) signOutPost(ctx *web.HttpContext) {
    ctx.WriteString("signOutPost:", ctx.Template)

}

//函数直接注册路由
func IndexGet(ctx *web.HttpContext) {
    ctx.WriteString("Index Get :", ctx.Template)
}
func IndexPost(ctx *web.HttpContext) {
    ctx.WriteString("Index Post :", ctx.Template)
}

func PrintGet(ctx *web.HttpContext) {
    ctx.WriteString(web.PrintRoute("%v<br>")) //打印数据路由状态
}

//action级别拦截器 包含GET POST PUT.....
func IndexActionFilter(ctx *web.HttpContext) {
    ctx.WriteString("IndexActionFilter:", ctx.Template)
    ctx.Next() //向下传递注册的路由
}
func GlobalFilter(ctx *web.HttpContext) {
    ctx.WriteString("GlobalFilter:")
    switch ctx.Params["next"] {
    case "yes":
        ctx.Next()
    }

}
func init() {
    web.BringInLog(Logger) //推入一个第三方中间件切换
    //函数直接注册路由 ...
    web.RegisterRoute("/index", IndexGet)
    web.RegisterRoute("/index", IndexPost)
    web.RegisterRoute("/print", PrintGet)

    //对象匿名继承直接拥用REST标准接口
    web.RegisterRoute("/login", &login{})

    //对象不继承注册一个方法到路由
    web.RegisterRoute("/signout", (&signOut{}).signOutGet)

    //注册一个ACTION级别的拦截器
    web.RegisterActionFilter("/index", IndexActionFilter)
    //全局级别的拦截器
    web.RegisterGlobalFilter(GlobalFilter)
}

有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:x_focus

查看原文:一个简单GOLANG路由

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

5549 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传