File: routing/custom-wrapper/main.go
~~~
package main
import (
"net/http"
"strings"
"github.com/kataras/iris"
)
//在这个例子中,您将只看到.WrapRouter的一个用例.
// 您可以使用.WrapRouter在路由器应该或不应该时添加自定义逻辑
// 执行以执行注册路由的处理程序.
//
// 要了解如何在没有自定义包装器的情况下在 root“/”上提供文件
// 只需导航到“file-server/single-page-application”示例.
//
// 这仅用于概念验证,如果对您来说太过分,可以跳过本教程.
func newApp() *iris.Application {
app := iris.New()
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
ctx.HTML("<b>Resource Not found</b>")
})
app.Get("/", func(ctx iris.Context) {
ctx.ServeFile("./public/index.html", false)
})
app.Get("/profile/{username}", func(ctx iris.Context) {
ctx.Writef("Hello %s", ctx.Params().Get("username"))
})
// 从root目录“/”提供文件,如果我们使用.StaticWeb它可以覆盖
// 因为下划线需要通配符所有路由.
// 在这里,我们将看到如何绕过这种行为
//通过创建新的文件服务器处理程序和
// 为路由器设置包装器(like a "low-level" middleware)
//为了手动检查我们是否想要正常处理路由器
// 或者执行文件服务器处理程序.
// 使用 the .StaticHandler
// 这与StaticWeb相同,但它没有
// 注册路由,它只返回处理程序.
fileServer := app.StaticHandler("./public", false, false)
//用本机net / http处理程序包装路由器.
// i如果url不包含任何内容 "." (i.e: .css, .js...)
// (取决于应用程序,您可能需要添加更多的文件服务器异常),
//然后处理程序将执行负责的路由器
// r注册路线 (look "/" and "/profile/{username}")
// 如果没有,那么它将基于的服务文件 root "/" path.
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
path := r.URL.Path
// 请注意,如果path的后缀为“index.html”,则会将auto-permant重定向到 "/",
// 所以我们的第一个处理程序将被执行.
if !strings.Contains(path, ".") { // 如果它不是资源,那么就像正常情况一样继续使用路由器.
router(w, r)
return
}
//获取并释放上下文以便使用它来执行
// 文件服务器
//记住:我们使用net / http.Handler,因为这里我们处于“低级”,在路由器本身之前.
ctx := app.ContextPool.Acquire(w, r)
fileServer(ctx)
app.ContextPool.Release(ctx)
})
return app
}
func main() {
app := newApp()
// http://localhost:8080
// http://localhost:8080/index.html
// http://localhost:8080/app.js
// http://localhost:8080/css/main.css
// http://localhost:8080/profile/anyusername
app.Run(iris.Addr(":8080"))
//注意:在这个例子中,我们只看到了一个用例,
// 你可能想要 .WrapRouter or .Downgrade 为了绕过 iris' 默认路由器, i.e:
// 您也可以使用该方法来设置自定义代理.
//
// 如果您只想在除root之外的其他路径上提供静态文件
// 你可以使用StaticWeb, i.e:
// .StaticWeb("/static", "./public")
// ________________________________requestPath, systemPath
}
~~~
File: routing/custom-wrapper/main_test.go
~~~
package main
import (
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/kataras/iris/httptest"
)
type resource string
func (r resource) String() string {
return string(r)
}
func (r resource) strip(strip string) string {
s := r.String()
return strings.TrimPrefix(s, strip)
}
func (r resource) loadFromBase(dir string) string {
filename := r.String()
if filename == "/" {
filename = "/index.html"
}
fullpath := filepath.Join(dir, filename)
b, err := ioutil.ReadFile(fullpath)
if err != nil {
panic(fullpath + " failed with error: " + err.Error())
}
return string(b)
}
var urls = []resource{
"/",
"/index.html",
"/app.js",
"/css/main.css",
}
func TestCustomWrapper(t *testing.T) {
app := newApp()
e := httptest.New(t, app)
for _, u := range urls {
url := u.String()
contents := u.loadFromBase("./public")
e.GET(url).Expect().
Status(httptest.StatusOK).
Body().Equal(contents)
}
}
~~~
File: routing/custom-wrapper/public/app.js
~~~
window.alert("app.js loaded from \"/");
~~~
File: routing/custom-wrapper/public/css/main.css
~~~
body {
background-color: black;
}
~~~
File: routing/custom-wrapper/public/index.html
~~~
<html>
<head>
<title>{{ .Page.Title }}</title>
</head>
<body>
<h1> Hello from index.html </h1>
<script src="/app.js"> </script>
</body>
</html>
~~~
- api文档
- yaag
- 认证
- 认证基本
- oauth2
- 缓存
- 客户端
- 简单的
- 配置
- 配置文件
- tml文件
- yaml文件
- 功能
- 处理程序
- Negroni Like
- http
- Real Usecase Raven
- cookies
- Basic
- 安全cookie
- 实验程序
- casbin
- 云监视
- cors
- csrf
- jwt
- 新文学
- Casbin
- 文件服务器
- Hero
- http监听
- http请求
- HTTP Responsewriter
- Miscellaneous
- MVC
- mvc概观
- 中间件
- Hello World
- 登陆
- Session 控制器
- Singleton
- MVC基本
- ORM
- xorm
- 概况
- 路由
- 概观
- 自定义上下文
- 自定义包装
- 动态路径
- 相反
- HTTP错误
- 路由状态
- 路由基本
- Sessions
- 构建
- 子域
- 重定向
- 万维网
- Single
- 通配符
- 多域名
- 测试
- 教程
- 视图
- Template Html 0
- Template Html 1
- Template Html 2
- Template Html 3
- Template Html 4
- Webassembly
- Websocket
