Iris 学习笔记 1

Iris 学习简单记录,因为某些原因未在项目中使用,所以仅是记录而已,多数内容来源于官方 Readme。

Iris 学习笔记

入门示例

package main

import "github.com/kataras/iris"

func main() {
    app := iris.Default()
    app.Get("/ping", func(ctx iris.Context) {
        ctx.JSON(iris.Map{
            "message": "pong",
        })
    })
    // listen and serve on http://0.0.0.0:8080.
    app.Run(iris.Addr(":8080"))
}

使用记录

路径中的参数类型

Param Type Go Type Validation Retrieve Helper
:string string anything (single path segment) Params().Get
:int int -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depends on the host arch Params().GetInt
:int8 int8 -128 to 127 Params().GetInt8
:int16 int16 -32768 to 32767 Params().GetInt16
:int32 int32 -2147483648 to 2147483647 Params().GetInt32
:int64 int64 -9223372036854775808 to 9223372036854775807 Params().GetInt64
:uint uint 0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32), depends on the host arch Params().GetUint
:uint8 uint8 0 to 255 Params().GetUint8
:uint16 uint16 0 to 65535 Params().GetUint16
:uint32 uint32 0 to 4294967295 Params().GetUint32
:uint64 uint64 0 to 18446744073709551615 Params().GetUint64
:bool bool “1” or “t” or “T” or “TRUE” or “true” or “True” or “0” or “f” or “F” or “FALSE” or “false” or “False” Params().GetBool
:alphabetical string lowercase or uppercase letters Params().Get
:file string lowercase or uppercase letters, numbers, underscore (_), dash (-), point (.) and no spaces or other special characters that are not valid for filenames Params().Get
:path string anything, can be separated by slashes (path segments) but should be the last part of the route path Params().Get

内建辅助函数:

  • regexp(expr string)
  • prefix(prefix string)
  • suffix(suffix string)
  • contains(s string)
  • min()
  • max()
  • range(minValue, maxValue)

用法:

app.Get("/user/{id:uint64}", func(ctx iris.Context) {
  id := ctx.Params().GetUint64Default("id", 0)
})

app.Get("/profile/{name:alphabetical max(255)}", func(ctx iris.Context) {
  name := ctx.Params().Get("name")
})

自定义辅助函数

通过 RegisterFunc 注册自定义的匹配函数, 必须返回 func (paramValue string) boolfunc(string) bool

// 注册宏 range,接受两个参数限定
app.Macros().Get("string").RegisterFunc("range", func(minLength, maxLength int) func(string) bool {
    // 返回符合要求的函数类型
    return func(paramValue string) bool {
        return len(paramValue) >= minLength && len(paramValue) <= maxLength
    }
})

// 匹配失败则返回 400 状态码
app.Get("/limitchar/{name:string range(1,200) else 400}", func(ctx iris.Context) {
    name := ctx.Params().Get("name")
    ctx.Writef(`Hello %s | name 应是 1 到 200 个字符,否则handler不会处理`, name)
})

提取请求信息

  • 获取 URL 查询参数,使用 ctx.URLParam("key")

  • 获取 Form 传递的参数,使用 ctx.FormValue("key")

  • 提取 Referer 头信息,使用 ctx.GetReferer()

设置响应信息

  • 设置 HTTP 状态码,使用 ctx.StatusCode(500)

  • 设置 Header,使用 ctx.Header("c", "my custom header")

热重载

安装rizla包

$ go get -u github.com/kataras/rizla

热重启方式启动iris项目

$ rizla main.go

本文链接:参与评论 »

--EOF--

提醒:本文最后更新于 1998 天前,文中所描述的信息可能已发生改变,请谨慎使用。

Comments