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. 支付宝网站支付接口配置 RSA 公钥 私钥

    个人博客 地址:http://www.wenhaofan.com/article/20190419143333 下载签名工具 访问:https://docs.open.alipay.com/291/1 ...

  2. Object与byte[]互转

    User user=new User(); user.setId("bonnie"); user.setAge("10"); //Object转byte[] B ...

  3. springboot里面的缓存注解

    https://blog.csdn.net/u012240455/article/details/80844361 https://lfvepclr.gitbooks.io/spring-framew ...

  4. Python 高维数组“稀疏矩阵”scipy sparse学习笔记

    scipy 里面的sparse函数进行的矩阵存储 可以节省内存 主要是scipy包里面的 sparse 这里目前只用到两个 稀疏矩阵的读取 sparse.load() 转稀疏矩阵为普通矩阵 spars ...

  5. CSS-定义样式表

    1.HTML标记定义 p{属性:属性值;属性1:属性1} <p>...</p> 注:p可以叫做选择器,定义那个标记中的内容执行其中的样式.一个选择器可以控制若干个样式属性,他们 ...

  6. #助力CSP2019# OI中容易出现的**错误汇总

    多测不清空,爆0两行泪 3年OI一场空,不开long long见祖宗 线段树空间需要开4倍 读入有负数的时候,如果要写快读,要识别负号 持续更新

  7. 项目打jar包和运行

    打包成jar包和部署,运行. 1.在pom.xml中加入  <packaging>jar</packaging> <groupId>com.demo02</g ...

  8. URLSearchParams/FormData

    一.URLSearchParams()(很好用,但有一定兼容问题,未来版本的浏览器中该功能的语法和行为可能随对应的标准文档而改变.) URLSearchParams 接口定义了一些实用的方法来处理 U ...

  9. numpy学习(三)

    练习篇(Part 3) 31. 略 32. Is the following expressions true? (★☆☆) np.sqrt(-1) == np.emath.sqrt(-1) prin ...

  10. 静态区间第k小 - 整体二分

    蒟蒻终于学会整体二分啦! 思路 实现 丑陋无比的代码 #include <bits/stdc++.h> using namespace std; const int N = 200005; ...