package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
) func main() { app := iris.New() //1.handle方式处理请求
//同一用来处理请求的方法
//GET
app.Handle("GET", "/userinfo", func(context context.Context) {
path := context.Path()
app.Logger().Info(path)
app.Logger().Error(" request path :", path)
}) //post
app.Handle("POST", "/postcommit", func(context context.Context) {
path := context.Path()
app.Logger().Info(" post reuqest, the url is : ", path)
context.HTML(path)
}) //正则表达式:{name}
app.Get("/weather/{date}/{city}", func(context context.Context) {
path := context.Path()
//使用:context.Params().Get("name") 获取正则表达式变量
date := context.Params().Get("date")
city := context.Params().Get("city")
context.WriteString(path + " , " + date + " , " + city)
}) //1.Get 正则表达式 路由
//使用:context.Params().Get("name") 获取正则表达式变量
//正则表达式:{name}
app.Get("/hello/{name}", func(context context.Context) {
//获取变量
path := context.Path() app.Logger().Info(path)
//获取正则表达式变量内容值
name := context.Params().Get("name")
context.HTML("<h1>" + name + "</h1>")
}) //2.自定义正则表达式变量路由请求 {unit64:uint64}进行变量类型限制
app.Get("/api/users/{userid:uint64}", func(context context.Context) {
userID, err := context.Params().GetUint("userid") if err != nil {
//设置请求状态码,状态码可以自定义
context.JSON(map[string]interface{}{
"requestcode": 201,
"message": "bad request",
})
return
} context.JSON(map[string]interface{}{
"requestcode": 200,
"user_id": userID,
})
}) //自定义正则表达式路由请求 bool
//api/users/true
app.Get("/api/users/{isLogin:bool}", func(context context.Context) {
isLogin, err := context.Params().GetBool("isLogin")
if err != nil {
context.StatusCode(iris.StatusNonAuthoritativeInfo)
return
}
if isLogin {
context.WriteString(" 已登录 ")
} else {
context.WriteString(" 未登录 ")
} //正则表达式所支持的数据类型
//context.Params().GetBool() //Getxxx()
}) app.Run(iris.Addr(":8002"), iris.WithoutServerError(iris.ErrServerClosed)) }
package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
) func main() { app := iris.New() //用户模块users
//xxx/users/register 注册
//xxx/users/login 登录
//xxx/users/info 获取用户信息 //路由组请求
userParty := app.Party("/users", func(context context.Context) {
//处理下一级请求,就是users斜杠后面的
context.Next()
})
//路由组下面的下一级请求
//xxx/users/register
userParty.Get("/register", func(context context.Context) {
app.Logger().Info("用户注册功能")
context.HTML("<h1>用户注册功能</h1>")
}) //路由组下面的下一级请求
//xxx/users/login
userParty.Get("/login", func(context context.Context) {
app.Logger().Info("用户登录功能")
context.HTML("<h1>用户登录功能</h1>")
}) //另一种方式
usersRouter := app.Party("/admin", userMiddleware) //Done方法,表示请求结束
usersRouter.Done(func(context context.Context) {
context.Application().Logger().Infof("response sent to " + context.Path())
}) usersRouter.Get("/info", func(context context.Context) {
context.HTML("<h1> 用户信息 </h1>")
context.Next() //手动显示调用,去调用Done方法
}) usersRouter.Get("/query", func(context context.Context) {
context.HTML("<h1> 查询信息 </h1>")
}) app.Run(iris.Addr(":8003"), iris.WithoutServerError(iris.ErrServerClosed))
} //另一种方式
//用户路由中间件
func userMiddleware(context iris.Context) {
context.Next()
}

  

  

Iris路由和路由组的更多相关文章

  1. CCIE路由实验(6) -- 组播Multicasting

    1.组播IGMP的各种情况2.PIM Dense-Mode3.PIM Sparse-Mode4.PIM双向树和SSM5.动态RP之auto-rp6.动态RP之BSR7.Anycast RP8.域间组播 ...

  2. [水煮 ASP.NET Web API2 方法论](3-2)直接式路由/属性路由

    问题 怎么样可以使用更贴近资源(Controller,Action)的方式定义路由. 解决方案 可以使用属性路由直接在资源级别声明路由.只要简单的在 Action 上使用属性路由 RouteAttri ...

  3. 海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs

    海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs 企业简介 武汉海蜘蛛网络科技有限公司成立于2005年,是一家专注于网络新技术研 ...

  4. ASP.NET没有魔法——ASP.NET MVC 直连路由(特性路由)

    之前对Controller创建的分析中,知道了Controller的创建是有两个步骤组成,分别是Controller的类型查找以及根据类型创建Controller实例. 在查询Controller的类 ...

  5. Angular routing生成路由和路由的跳转

    Angular routing生成路由和路由的跳转 什么是路由 路由的目的是可以让根组件按照不同的需求动态加载不同的组件. 根据不同地址,加载不同组件,实现单页面应用. Angular 命令创建一个配 ...

  6. vue教程3-06 vue路由嵌套(多层路由),路由其他信息

    多层嵌套: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. Vue路由获取路由参数

    vue路由设置路由参数有2种方式: 1.通过query配置: <router-link :to="{ name:'login',query:{id:1} }">登录&l ...

  8. System.ArgumentException:路由集合中已存在名为“XXX”的路由。路由名称必须唯一。

    软件环境:Visual Studio 2017 + MVC4 + EF6 问题描述:System.ArgumentException:路由集合中已存在名为“XXX”的路由.路由名称必须唯一. 解决办法 ...

  9. Vue_(Router路由)-vue-router路由的基本用法

    vue-router官网:传送门 vue-router起步:传送门 vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用 单页应用:Single Page Applicati ...

  10. vue路由--命名路由

    有时我们通过一个名称来标识一个路由显得更方便一些,特别是在链接一个路由,或者是执行一些跳转的时候.你可以在创建 Router 实例的时候,在 routes 配置中给某个路由设置名称. 我们直接在路由下 ...

随机推荐

  1. 安全 - 内容安全策略(CSP)(未完)

    威胁 跨站脚本攻击(Cross-site scripting) 跨站脚本攻击Cross-site scripting (XSS)是一种安全漏洞,攻击者可以利用这种漏洞在网站上注入恶意的客户端代码. 攻 ...

  2. UCB博士资格考试试题

    https://math.berkeley.edu/~myzhang/qual.html?tdsourcetag=s_pcqq_aiomsg <!-- Page Content --> & ...

  3. Python GUI编程(TKinter)(简易计算器)

    搞课设搞得心累,现在看到人脸这两个字就烦躁,无聊搞搞tkinter,实现一个计算器的功能,能够简单的加减乘除. 简单的页面如下: 简单的代码如下: # encoding:utf-8 import tk ...

  4. GYCTF Node game

    考点: NodeJS 代码审计 SSRF 请求夹带 复现: 不太懂js,先留着吧,学懂了再记录

  5. 通过ssh-copy-id免密码连接Linux主机

    Login Raspberry Pi without passcode via ssh-copy-id Generate public key $ ssh-keygen -t rsa Upload p ...

  6. jQuery---固定导航栏案例

    固定导航栏案例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  7. HTTP 协议的 8 种请求类型介绍

    HTTP 协议的 8 种请求类型介绍 HTTP 协议中共定义了八种方法或者叫“动作”来表明对 Request-URI 指定的资源的不同操作方式,具体介绍如下: OPTIONS:返回服务器针对特定资源所 ...

  8. Leetcode Week3 Merge Two(k) Sorted Lists

    Question Q1.Merge two sorted linked lists and return it as a new list. The new list should be made b ...

  9. 请求 - axios

    实际应用示例 前端不需要做统一的接口防重 前端无法通过判断接口是否返回来释放按钮(因为可以手动刷新页面,将导致刷新前请求丢失) 后端对接口做了防重 通过增加时间戳避免IE9的get请求缓存问题 axi ...

  10. Hibernate 和Mybatis的区别

    Hibernate 和Mybatis的区别   1.hibernate 入门门槛高,是一个标准的ORM框架(对象关系映射),不需要程序写sql,sql语句自动生成,对sql语句进行优化.修改比较困难. ...