# Routing
## Handlers
A Handler, as the name implies, handle requests.
```go
// A Handler responds to an HTTP request.
// It writes reply headers and data to the
// Context.ResponseWriter() and then return.
// Returning signals that the request is finished;
// it is not valid to use the Context after or
// concurrently with the completion of the Handler call.
//
// Depending on the HTTP client software, HTTP protocol version,
// and any intermediaries between the client and the iris server,
// it may not be possible to read from the
// Context.Request().Body after writing to the context.ResponseWriter().
// Cautious handlers should read the Context.Request().Body first, and then reply.
//
// Except for reading the body, handlers should not modify the provided Context.
//
// If Handler panics, the server (the caller of Handler) assumes that
// the effect of the panic was isolated to the active request.
// It recovers the panic, logs a stack trace to the server error log
// and hangs up the connection.
type Handler func(Context)
```
Once the handler is registered, we can use the returned [`Route`](https://godoc.org/github.com/kataras/iris/core/router#Route) instance to give a name to the handler registration for easier lookup in code or in templates.
For more information, checkout the [Routing and reverse lookups](routing_reverse.md) section.
## API
All HTTP methods are supported, developers can also register handlers for same paths for different methods.
The first parameter is the HTTP Method,
second parameter is the request path of the route,
third variadic parameter should contains one or more iris.Handler executed
by the registered order when a user requests for that specific resouce path from the server.
Example code:
```go
app := iris.New()
app.Handle("GET", "/contact", func(ctx iris.Context) {
ctx.HTML("<h1> Hello from /contact </h1>")
})
```
In order to make things easier for the end-developer, iris provides functions for all HTTP Methods.
The first parameter is the request path of the route,
second variadic parameter should contains one or more iris.Handler executed
by the registered order when a user requests for that specific resouce path from the server.
Example code:
```go
app := iris.New()
// Method: "GET"
app.Get("/", handler)
// Method: "POST"
app.Post("/", handler)
// Method: "PUT"
app.Put("/", handler)
// Method: "DELETE"
app.Delete("/", handler)
// Method: "OPTIONS"
app.Options("/", handler)
// Method: "TRACE"
app.Trace("/", handler)
// Method: "CONNECT"
app.Connect("/", handler)
// Method: "HEAD"
app.Head("/", handler)
// Method: "PATCH"
app.Patch("/", handler)
// register the route for all HTTP Methods
app.Any("/", handler)
func handler(ctx iris.Context){
ctx.Writef("Hello from method: %s and path: %s", ctx.Method(), ctx.Path())
}
```
## Grouping Routes
A set of routes that are being groupped by path prefix can (optionally) share the same middleware handlers and template layout.
A group can have a nested group too.
`.Party` is being used to group routes, developers can declare an unlimited number of (nested) groups.
Example code:
```go
app := iris.New()
users := app.Party("/users", myAuthMiddlewareHandler)
// http://localhost:8080/users/42/profile
users.Get("/{id:uint64}/profile", userProfileHandler)
// http://localhost:8080/users/messages/1
users.Get("/inbox/{id:uint64}", userMessageHandler)
```
The same could be also written using a function which accepts the child router(the Party).
```go
app := iris.New()
app.PartyFunc("/users", func(users iris.Party) {
users.Use(myAuthMiddlewareHandler)
// http://localhost:8080/users/42/profile
users.Get("/{id:uint64}/profile", userProfileHandler)
// http://localhost:8080/users/messages/1
users.Get("/inbox/{id:uint64}", userMessageHandler)
})
```
> `id:uint` is a (typed) dynamic path parameter, learn more by scrolling down.
# Dynamic Path Parameters
Iris has the easiest and the most powerful routing process you have ever meet.
At the same time,
Iris has its own interpeter(yes like a programming language)
for route's path syntax and their dynamic path parameters parsing and evaluation.
We call them "macros" for shortcut.
How? It calculates its needs and if not any special regexp needed then it just
registers the route with the low-level path syntax,
otherwise it pre-compiles the regexp and adds the necessary middleware(s). That means that you have zero performance cost compared to other routers or web frameworks.
Standard macro types for route path parameters
```sh
+------------------------+
| {param:string} |
+------------------------+
string type
anything (single path segmnent)
+-------------------------------+
| {param:int} |
+-------------------------------+
int type
-9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depends on the host arch
+------------------------+
| {param:int8} |
+------------------------+
int8 type
-128 to 127
+------------------------+
| {param:int16} |
+------------------------+
int16 type
-32768 to 32767
+------------------------+
| {param:int32} |
+------------------------+
int32 type
-2147483648 to 2147483647
+------------------------+
| {param:int64} |
+------------------------+
int64 type
-9223372036854775808 to 9223372036854775807
+------------------------+
| {param:uint} |
+------------------------+
uint type
0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32)
+------------------------+
| {param:uint8} |
+------------------------+
uint8 type
0 to 255
+------------------------+
| {param:uint16} |
+------------------------+
uint16 type
0 to 65535
+------------------------+
| {param:uint32} |
+------------------------+
uint32 type
0 to 4294967295
+------------------------+
| {param:uint64} |
+------------------------+
uint64 type
0 to 18446744073709551615
+---------------------------------+
| {param:bool} or {param:boolean} |
+---------------------------------+
bool type
only "1" or "t" or "T" or "TRUE" or "true" or "True"
or "0" or "f" or "F" or "FALSE" or "false" or "False"
+------------------------+
| {param:alphabetical} |
+------------------------+
alphabetical/letter type
letters only (upper or lowercase)
+------------------------+
| {param:file} |
+------------------------+
file type
letters (upper or lowercase)
numbers (0-9)
underscore (_)
dash (-)
point (.)
no spaces ! or other character
+------------------------+
| {param:path} |
+------------------------+
path type
anything, should be the last part, can be more than one path segment,
i.e: "/test/*param" and request: "/test/path1/path2/path3" , ctx.Params().Get("param") == "path1/path2/path3"
```
If type is missing then parameter's type is defaulted to string, so
`{param} == {param:string}`.
If a function not found on that type then the `string` macro type's functions are being used.
Besides the fact that iris provides the basic types and some default "macro funcs"
you are able to register your own too!.
Register a named path parameter function
```go
app.Macros().Get("int").RegisterFunc("min", func(argument int) func(paramValue int) bool {
// [...]
return func(paramValue int) bool {
// -> true means valid, false means invalid fire 404
// or if "else 500" is appended to the macro syntax then internal server error.
return true
}
})
```
At the `func(argument ...)` you can have any standard type, it will be validated before the server starts so don't care about any performance cost there, the only thing it runs at serve time is the returning `func(paramValue <T>) bool`.
```go
{param:string equal(iris)}
```
The "iris" will be the argument here:
```go
app.Macros().Get("string").RegisterFunc("equal", func(argument string) func(paramValue string) bool {
return func(paramValue string){ return argument == paramVa
没有合适的资源?快使用搜索试试~ 我知道了~
go语言iris框架完整源码包
共2663个文件
go:2046个
html:82个
s:81个
需积分: 50 27 下载量 98 浏览量
2019-06-25
00:24:06
上传
评论
收藏 10.13MB RAR 举报
温馨提示
完整的iris包,本来有开源包,为啥还需要这个呢?那是因为并不是所有的地方都可以连上github下载源码,而且除了用go get命令经常无法下载,所以提供一个完整的包,方便使用。
资源推荐
资源详情
资源评论
收起资源包目录
go语言iris框架完整源码包 (2663个子文件)
APACHE-LICENSE-2.0 10KB
basic.amber 588B
basic.amber 588B
basic.amber 588B
inherit.master.amber 276B
inherit.amber 223B
multilevel.inheritance.c.amber 78B
multilevel.inheritance.b.amber 78B
recursion.top.amber 63B
recursion.extends.amber 63B
input.amber 45B
multilevel.inheritance.a.amber 36B
recursion.parent.amber 12B
AUTHORS 965B
AUTHORS 499B
AUTHORS 253B
AUTHORS 198B
AUTHORS 176B
AUTHORS 176B
AUTHORS 174B
AUTHORS 174B
AUTHORS 173B
AUTHORS 151B
AUTHORS 133B
AUTHORS 132B
AUTHORS 131B
AUTHORS 128B
AUTHORS 122B
AUTHORS 118B
AUTHORS 116B
AUTHORS 47B
AUTHORS 47B
AUTHORS 47B
libotr_test_helper.c 6KB
gccgo_c.c 1KB
go12.c 497B
Caddyfile 182B
mycert.cert 1KB
codereview.cfg 22B
codereview.cfg 22B
CHANGES 736B
runbench.cmd 148B
.codebeatignore 12B
CODEOWNERS 89B
CODEOWNERS 86B
CODEOWNERS 86B
COMPATIBLE 116B
casbinmodel.conf 249B
casbinmodel.conf 249B
CONTRIBUTORS 1KB
CONTRIBUTORS 1KB
CONTRIBUTORS 173B
CONTRIBUTORS 173B
CONTRIBUTORS 170B
CONTRIBUTORS 170B
COPYING 498B
COPYING 498B
COPYING 498B
COPYING 498B
main.cpp 2KB
server.crt 1KB
Startup.cs 3KB
Startup.cs 2KB
Startup.cs 1KB
Startup.cs 1KB
HomeController.cs 953B
ValuesController.cs 878B
Program.cs 638B
Program.cs 633B
Program.cs 628B
Program.cs 624B
ErrorViewModel.cs 190B
Index.cshtml 6KB
_Layout.cshtml 2KB
Contact.cshtml 425B
Error.cshtml 212B
About.cshtml 122B
_ViewImports.cshtml 121B
_ViewStart.cshtml 35B
netcore-mvc.csproj 524B
netcore.csproj 397B
netcore-sessions.csproj 397B
netcore-mvc-templates.csproj 397B
bootstrap.css 149KB
bootstrap.min.css 138KB
bootstrap.min.css 131KB
bootstrap.min.css 118KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
dropzone.css 12KB
site.css 1KB
site.css 1KB
site.min.css 282B
main.css 41B
main.css 41B
main.css 41B
main.css 41B
style.css 38B
main.css 38B
casbinpolicy.csv 221B
共 2663 条
- 1
- 2
- 3
- 4
- 5
- 6
- 27
资源评论
AlexAcce
- 粉丝: 6
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于JSP水产品销售系统源码.zip
- 基于JSP手机商城管理系统源码.zip
- 5.3.1_1二叉树的先中后序遍历.mp420240404-134540.png
- 基于JSP实现一个C语言教学网站平台源码.zip
- 抖音快手-课程网盘链接提取码下载 .txt
- 934742083249391XGOBOTV1.2.4.apk
- 课程设计基于OpenCV的材料缺陷检测程序python源码(含超详细注释).zip
- 基于python和百度EsayDL实现自动驾驶算法+基于ESP32开发板作为智能车主控芯片的自动驾驶智能车项目+源码(高分项目)
- Centos7-离线安装-MySQL5-7-31-tar包安装
- 蓝海平台带货-课程网盘链接提取码下载 .txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功