Go Iris学习笔记01

摘要:
当您使用iris的默认语法来解析控制器处理程序时,需要在方法后面添加“.”字符。大写字母是新的子路径。官方网站示例:1mvc。新Handle23funcGet()-GET:/user.4funcPost()-POST:/user.5funcGetLogin()-GET:/user/login6funcGetLogin()-POST:/user/login 7funcGetProfileFollowers()-GIT:/user/profile/flowers8funcPostProfileFollowers()-POST:/user/profile/flowers9funcGetBy GET:/user/{param:long}10funcPostBy POST:/user/{param:long}mvc。新建。HandlefuncGetByGET:/profile/{param:string}Mvc.New.HHandlefuncGetByWildard GET:/assets/{param:path}方法函数接收器支持的类型:int、int64、bool和string。测试demomain:packagemainimportfuncmain(){app:=newApp()//app.RegisterView//加载模板文件app.StaticWeb//设置静态资源。暂时没有app.RegterViewgolog.Info()。//app.Run}funcrouter{//main:=this.Party.AllowMethods//middleware home:=this.Party(“/”)home.Gethome.Gethome.Getmvc.New.Handle}funcnewApp()*iris。应用程序{app:=iris.New()presettingGroundereturnApp}FuncpreSetstring{//定义错误显示级别app.Logger().SetLevelcustomLogger:=Logger.New(Logger.Config{//状态显示状态代码状态:true,//IP显示请求远程地址IP:true,//方法显示http方法方法:true,//Path显示请求路径路径:true,查询将url查询追加到路径。
Iris MVC支持

文档:

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

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

func (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
    b.Handle("GET", "/something/{id:long}", "MyCustomHandler", anyMiddleware...)
}

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

 1  mvc.New(app.Party("/user")).Handle(new(user.Controller))
 2 
 3 func(*Controller) Get() - GET:/user.
 4 func(*Controller) Post() - POST:/user.
 5 func(*Controller) GetLogin() - GET:/user/login
 6 func(*Controller) PostLogin() - POST:/user/login
 7 func(*Controller) GetProfileFollowers() - GET:/user/profile/followers
 8 func(*Controller) PostProfileFollowers() - POST:/user/profile/followers
 9 func(*Controller) GetBy(id int64) - GET:/user/{param:long}
10 func(*Controller) PostBy(id int64) - POST:/user/{param:long}
mvc.New(app.Party("/profile")).Handle(new(profile.Controller))

func(*Controller) GetBy(username string) - GET:/profile/{param:string}

mvc.New(app.Party(
"/assets")).Handle(new(file.Controller)) func(*Controller) GetByWildard(path string) - GET:/assets/{param:path} 方法函数接收器支持的类型: int,int64, boolstring

测试demo

main:

package main

import (
    "admin/web/controllers"
    "github.com/kataras/golog"
    "github.com/kataras/iris"
    "github.com/kataras/iris/middleware/logger"
    "github.com/kataras/iris/mvc"
)

func main()  {
    app := newApp()
    //app.RegisterView(iris.HTML("./web", ".html")) //加载模版文件
    app.StaticWeb("/static", "web/resources/static") // 设置静态资源,暂时没有
    app.RegisterView(iris.HTML("web/views", ".html").Reload(true))
    golog.Info() //暂时不知道干啥的
    app.Run(iris.Addr(":8081"))
}

func router(
this *iris.Application){ //main := this.Party("/", crs).AllowMethods(iris.MethodOptions) //中间件 home:= this.Party("/") home.Get("/", func(ctx iris.Context) { // 首页模块 ctx.View("index/index.html") }) home.Get("/home", func(ctx iris.Context) { ctx.View("login/login.html") }) home.Get("/welcome", func(ctx iris.Context) { ctx.View("welcome/welcome.html") }) home.Get("/user/list/{page:int}",func(ctx iris.Context){ ctx.View("user/list.html") }) mvc.New(this.Party("/user")).Handle(new(controllers.UserController)) } func newApp() *iris.Application{ app := iris.New() preSettring(app) router(app) return app } func preSettring(app *iris.Application){ // 定义错误显示级别 app.Logger().SetLevel("debug") customLogger := logger.New(logger.Config{ //状态显示状态代码 Status: true, // IP显示请求的远程地址 IP: true, //方法显示http方法 Method: true, // Path显示请求路径 Path: true, // Query将url查询附加到Path。 Query: true, //Columns:true, // 如果不为空然后它的内容来自`ctx.Values(),Get("logger_message") //将添加到日志中。 MessageContextKeys: []string{"logger_message"}, //如果不为空然后它的内容来自`ctx.GetHeader(“User-Agent”) MessageHeaderKeys: []string{"User-Agent"}, }) app.Use( customLogger, //recover2.New(), ) }

controller:

package controllers

import (
    "admin/models"
    "admin/services"
    "fmt"
)

type UserController struct {
    Service services.UserService
}


// 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
func (c *UserController) Get() (result []models.User)  {
    fmt.Println("111111")
    //
    //data := c.Service.GetAll()
    //for k,_ := range data {
    //    result = append(result,models.User{1,string(k)})
    //}
    return
}

// 获取用户列表
// curl -i http://localhost:8080/user/list
func (u *UserController) GetList() (res string){
    fmt.Println("GetUserList")
    return "getUserlist"
}

免责声明:文章转载自《Go Iris学习笔记01》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SwiftUI图片处理(缩放、拼图)[UE4]ProgressBar,进度条下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

金山词霸2016企业版破解版下载 v6.7(含安装教程)

金山词霸2016企业版破解版下载 v6.7(含安装教程) 转https://bbs.52iss.com/forum.php?mod=viewthread&tid=4654&extra= 今天为大家带来的是金山词霸2016企业版破解版,采用的内存劫持补丁可以将软件完全破解,让用户无限期的正常的使用,给用户一个高品质的查词翻译功能。金山词霸20...

Appium(四):真实机第一个appium程序、模拟器第一个appium程序、查看元素

1. 真实机第一个appium程序 学完了前面的知识,也将环境搭建好了,接下来我们就正式开始appium的学习了。 在做app自动化的时候,我们肯定是针对某个产品、某个软件进行测试,那么我们一定是先让模拟器或真机帮我们打开这款软件才可以。所以接下来要学的就是如何打开某个应用程序。 启动步骤: USB连接手机,开启调试模式 打开appium工具 创建pyth...

Win10系统的SurfacePro4如何重装系统-1 SurfacePro专用的PE

下载SurfacePro专用的PE(普通的PE可能不支持触摸屏操作,甚至没法启动Surface,所以务必要重新制作PE),下面提供百度云下载地址,下载之后,双击EXE,会进行检测 链接:https://pan.baidu.com/s/1RHK8EUaLzoPnNfRyH9ZrLg 密码:owno   然后进入下面的窗口,选择目标优盘,所有参数都是默...

软件包管理 之 软件在线升级更新yum 图形工具介绍

作者:北南南北来自:LinuxSir.Org提要:yum 是Fedora/Redhat 软件包管理工具,包括文本命令行模式和图形模式;图形模式的yum也是基于文本模式的;目前yum图形前端程序主要有 yumex和kyum ; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++...

小程序app.onLaunch中获取用户信息,index.onLoad初次载入时取不到值的问题

问题描述: //app.js App({ globalData:{ nickname:'' }, onLaunch: function () { let that=this; //假设已经授权成功 wx.getUserInfo({ success(res){ //获取用户信...

华硕路由器Asus RT-ACRH17 OpenWrt,刷openwrt教程

华硕路由器刷OPENWRT教程 注:该教程整理自恩山论坛,因论坛内容较为繁琐,理解不便,特整理优化本文。 所需文件列表 解锁Bootloader的固件 【openwrt-ipq806x-asus_rt-acrh17-squashfs-flash-factory.trx】 opboot固件 【opboot-rt-acrh17-flash-v1.0.6...