加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

一个简单GOLANG路由

发布时间:2020-12-16 18:42:58 所属栏目:大数据 来源:网络整理
导读:package controllersimport ( . "logger" "web" )//对象继承直接拥用REST标准接口type login struct { web .ControllerBase }func (this *login) Get() { this .Template [ "key" ] = "value" this .WriteString ( "GET:" ,this .Template )}func (this *log
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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读