Go Iris学习笔记01

Iris MVC支持

文档:

支持所有 HTTP 方法, 例如,如果想要写一个 GET 那么在控制器中也要写一个 Get() 函数,你可以在一个控制器内定义多个函数。

每个控制器通过 BeforeActivation 自定义事件回调,用来自定义控制器的结构的方法与自定义路径处理程序,如下:(还未实验)

1func (m \*MyController) BeforeActivation(b mvc.BeforeActivation) { // b.Dependencies().Add/Remove // b.Router().Use/UseGlobal/Done // and any standard API call you already know // 1-> Method // 2-> Path // 3-> The controller's function name to be parsed as handler // 4-> Any handlers that should run before the MyCustomHandler 2 b.Handle("GET", "/something/{id:long}", "MyCustomHandler", anyMiddleware...) 3}

通过控制器方法的输入参数访问动态路径参数,不需要绑定。当你使用 iris 的默认语法来解析控制器处理程序时,你需要在方法后加上 "." 字符,大写字母是一个新的子路径。 官网例子:

1 1 mvc.New(app.Party("/user")).Handle(new(user.Controller)) 2 2 3 3 func(\*Controller) Get() - GET:/user. 4 4 func(\*Controller) Post() - POST:/user. 5 5 func(\*Controller) GetLogin() - GET:/user/login 6 6 func(\*Controller) PostLogin() - POST:/user/login 7 7 func(\*Controller) GetProfileFollowers() - GET:/user/profile/followers 8 8 func(\*Controller) PostProfileFollowers() - POST:/user/profile/followers 9 9 func(\*Controller) GetBy(id int64) - GET:/user/{param:long} 10 func(\*Controller) PostBy(id int64) - POST:/user/{param:long}
1mvc.New(app.Party("/profile")).Handle(new(profile.Controller)) 2 3func(\*Controller) GetBy(username string) - GET:/profile/{param:string} 4 5mvc.New(app.Party("/assets")).Handle(new(file.Controller)) 6 7func(\*Controller) GetByWildard(path string) - GET:/assets/{param:path} 8 9方法函数接收器支持的类型: int,int64, bool 和 string。

测试demo

main:

1package main 2 3import ( "admin/web/controllers" 4 "github.com/kataras/golog" 5 "github.com/kataras/iris" 6 "github.com/kataras/iris/middleware/logger" 7 "github.com/kataras/iris/mvc" ) 8 9func main() { 10 app :\= newApp() //app.RegisterView(iris.HTML("./web", ".html")) //加载模版文件 11 app.StaticWeb("/static", "web/resources/static") // 设置静态资源,暂时没有 12 app.RegisterView(iris.HTML("web/views", ".html").Reload(true)) 13 golog.Info() //暂时不知道干啥的 14 app.Run(iris.Addr(":8081")) 15} 16 17func router(this \*iris.Application){ //main := this.Party("/", crs).AllowMethods(iris.MethodOptions) //中间件 18 home:= this.Party("/") 19 home.Get("/", func(ctx iris.Context) { // 首页模块 20 ctx.View("index/index.html") 21 }) 22 home.Get("/home", func(ctx iris.Context) { 23 ctx.View("login/login.html") 24 }) 25 home.Get("/welcome", func(ctx iris.Context) { 26 ctx.View("welcome/welcome.html") 27 }) 28 29 home.Get("/user/list/{page:int}",func(ctx iris.Context){ 30 ctx.View("user/list.html") 31 }) 32 33 mvc.New(this.Party("/user")).Handle(new(controllers.UserController)) 34 35} 36func newApp() \*iris.Application{ 37 app :\= iris.New() 38 39 preSettring(app) 40 router(app) return app 41} 42 43func preSettring(app \*iris.Application){ // 定义错误显示级别 44 app.Logger().SetLevel("debug") 45 46 customLogger :\= logger.New(logger.Config{ //状态显示状态代码 47 Status: true, // IP显示请求的远程地址 48 IP: true, //方法显示http方法 49 Method: true, // Path显示请求路径 50 Path: true, // Query将url查询附加到Path。 51 Query: true, //Columns:true, // 如果不为空然后它的内容来自\`ctx.Values(),Get("logger\_message") //将添加到日志中。 52 MessageContextKeys: \[\]string{"logger\_message"}, //如果不为空然后它的内容来自\`ctx.GetHeader(“User-Agent”) 53 MessageHeaderKeys: \[\]string{"User-Agent"}, 54 }) 55 app.Use( 56 customLogger, //recover2.New(), 57 ) 58 59}

controller:

1package controllers 2 3import ( "admin/models" 4 "admin/services" 5 "fmt" ) 6 7type UserController struct { 8 Service services.UserService 9} // curl -i http://localhost:8080/movies // 如果您有敏感数据,这是正确的方法: // func (c \*MovieController) Get() (results \[\]viewmodels.Movie) { // data := c.Service.GetAll() // for \_, movie := range data { // results = append(results, viewmodels.Movie{movie}) // } // return // } // Get方法 // curl -i http://localhost:8080/user/list 10func (c \*UserController) Get() (result \[\]models.User) { 11 fmt.Println("111111") // 12 //data := c.Service.GetAll() //for k,\_ := range data { // result = append(result,models.User{1,string(k)}) //} 13 return } // 获取用户列表 // curl -i http://localhost:8080/user/list 14func (u \*UserController) GetList() (res string){ 15 fmt.Println("GetUserList") return "getUserlist" }

本文转自 https://www.cnblogs.com/lizhipengvvip/p/10797816.html,如有侵权,请联系删除。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )