File: mvc/hello-world/main.go
~~~
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)
// 这个例子相当于
// https://github.com/kataras/iris/blob/master/_examples/hello-world/main.go
//
// 似乎是你的额外代码
// 必须写不值得
// 但请记住,这个例子
// 没有使用像iris mvc这样的功能
// 模型,持久性或视图引擎都不是Session,
// 这对于学习目的来说非常简单,
// 可能你永远不会使用这样的
// 作为应用程序中任何位置的简单控制.
//
// 我们在此示例中使用MVC的成本
// 在提供JSON的“/ hello”路径上
// is ~2MB per 20MB 个人笔记本电脑的吞吐量,
// 它适用于大多数应用程序
// 但你可以选择
//最适合你的是Iris,低级处理程序:性能
// 或高级控制器:在大型应用程序上更易于维护和更小的代码库。.
// 当然你可以把所有这些都放到main函数中,它只是一个单独的函数
// 为了 main_test.go.
func newApp() *iris.Application {
app := iris.New()
// (可选)添加两个内置处理程序
// 可以从任何http相关的恐慌中恢复
//并将请求记录到终端
app.Use(recover.New())
app.Use(logger.New())
//根据根路由器提供控制器r, "/".
mvc.New(app).Handle(new(ExampleController))
return app
}
func main() {
app := newApp()
// http://localhost:8080
// http://localhost:8080/ping
// http://localhost:8080/hello
// http://localhost:8080/custom_path
app.Run(iris.Addr(":8080"))
}
// ExampleController服务于 "/", "/ping" 和 "/hello".
type ExampleController struct{}
//获得服务
// Method: GET
// Resource: http://localhost:8080
func (c *ExampleController) Get() mvc.Result {
return mvc.Response{
ContentType: "text/html",
Text: "<h1>Welcome</h1>",
}
}
// GetPing serves
// Method: GET
// Resource: http://localhost:8080/ping
func (c *ExampleController) GetPing() string {
return "pong"
}
// GetPing服务
// Method: GET
// Resource: http://localhost:8080/hello
func (c *ExampleController) GetHello() interface{} {
return map[string]string{"message": "Hello Iris!"}
}
//在控制器适应主应用之前,BeforeActivation调用一次
// 当然,在服务器运行之前.
// 在版本9之后,您还可以为特定控制器的方法添加自定义路由
//在这里,您可以注册自定义方法的处理程序
// 使用带有`ca.Router`的标准路由器做一些你可以在没有mvc的情况下做的事情,
// 并添加将绑定到控制器的字段或方法函数的输入参数的依赖项.
func (c *ExampleController) BeforeActivation(b mvc.BeforeActivation) {
anyMiddlewareHere := func(ctx iris.Context) {
ctx.Application().Logger().Warnf("Inside /custom_path")
ctx.Next()
}
b.Handle("GET", "/custom_path", "CustomHandlerWithoutFollowingTheNamingGuide", anyMiddlewareHere)
//甚至可以添加基于该控制器路由器的全局中间件r,
// 在这个例子中是root "/":
// b.Router().Use(myMiddleware)
}
// CustomHandlerWithoutFollowingTheNamingGuide serves
// Method: GET
// Resource: http://localhost:8080/custom_path
func (c *ExampleController) CustomHandlerWithoutFollowingTheNamingGuide() string {
return "hello from the custom handler without following the naming guide"
}
// GetUserBy serves
// Method: GET
// Resource: http://localhost:8080/user/{username:string}
// By是一个保留的“关键字”来告诉框架你要去的
// 在函数的输入参数中绑定路径参数,它也是
// 有助于在同一个控制器中使用“Get”和“GetBy”.
//
// func (c *ExampleController) GetUserBy(username string) mvc.Result {
// return mvc.View{
// Name: "user/username.html",
// Data: username,
// }
// }
/* 可以使用多个,工厂会确定 为每条路线注册了正确的http方法 对于此控制器,
如果需要,请取消注释:
func (c *ExampleController) Post() {}
func (c *ExampleController) Put() {}
func (c *ExampleController) Delete() {}
func (c *ExampleController) Connect() {}
func (c *ExampleController) Head() {}
func (c *ExampleController) Patch() {}
func (c *ExampleController) Options() {}
func (c *ExampleController) Trace() {}
*/
/*
func (c *ExampleController) All() {}
// OR
func (c *ExampleController) Any() {}
func (c *ExampleController) BeforeActivation(b mvc.BeforeActivation) {
// 1 -> HTTP方法
// 2 -> 路线的路径
// 3 ->此控制器的方法名称应该是该路由的处理程序.
b.Handle("GET", "/mypath/{param}", "DoIt", optionalMiddlewareHere...)
}
// 激活后,所有依赖项都被设置 - 因此只读访问它们
// 但仍然可以添加自定义控制器或简单的标准处理程序.
func (c *ExampleController) AfterActivation(a mvc.AfterActivation) {}
*/
~~~
File: mvc/hello-world/main_test.go
~~~
package main
import (
"testing"
"github.com/kataras/iris/httptest"
)
func TestMVCHelloWorld(t *testing.T) {
e := httptest.New(t, newApp())
e.GET("/").Expect().Status(httptest.StatusOK).
ContentType("text/html", "utf-8").Body().Equal("<h1>Welcome</h1>")
e.GET("/ping").Expect().Status(httptest.StatusOK).
Body().Equal("pong")
e.GET("/hello").Expect().Status(httptest.StatusOK).
JSON().Object().Value("message").Equal("Hello Iris!")
e.GET("/custom_path").Expect().Status(httptest.StatusOK).
Body().Equal("hello from the custom handler without following the naming guide")
}
~~~
- 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
