1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-27 06:47:08 +00:00

Update to version 12.1.6

Former-commit-id: 4d39e8a764a7c0d91b19a3710d8afe6c8c208c62
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-02-05 12:36:21 +02:00
parent d4e38da3ad
commit 9c97a8a668
14 changed files with 145 additions and 120 deletions

View File

@@ -19,9 +19,7 @@ import (
"github.com/kataras/iris/v12"
)
// $ go get github.com/valyala/tcplisten
// $ go run main.go
// You can run the same app as many times as you want.
func main() {
app := iris.New()
@@ -35,7 +33,7 @@ func main() {
FastOpen: true,
}
l, err := listenerCfg.NewListener("tcp", ":8080")
l, err := listenerCfg.NewListener("tcp4", ":8080")
if err != nil {
panic(err)
}

View File

@@ -125,16 +125,13 @@ func main() {
ctx.ViewData("showingAllDone", true)
ctx.ViewData("title", "Todos - All Done")
// Key does not actual matter at all here.
// However, you can enable it for better performance.
// In order to enable key mapping for
// jet specific renderer and ranger types
// you have to initialize the View Engine
// with `tmpl.DisableViewDataTypeCheck("_jet")`.
//
// Defaults to type checks, empty key.
ctx.ViewData("_jet", (&doneTODOs{}).New(todos))
ctx.View("todos/index.jet")
// Use ctx.ViewData("_jet", jetData)
// if using as middleware and you want
// to pre-set the value or even change it later on from another next middleware.
// ctx.ViewData("_jet", (&doneTODOs{}).New(todos))
// and ctx.View("todos/index.jet")
// OR
ctx.View("todos/index.jet", (&doneTODOs{}).New(todos))
})
port := os.Getenv("PORT")

View File

@@ -2,7 +2,7 @@
{{block documentBody()}}
<h1>Show TODO</h1>
<p>This uses a custom renderer by implementing the jet.Renderer interface.
<p>This uses a custom renderer by implementing the jet.Renderer (or view.JetRenderer) interface.
<p>
{{.}}
</p>

View File

@@ -0,0 +1,66 @@
// Package main an example on how to naming your routes & use the custom 'url path' Jet Template Engine.
package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.RegisterView(iris.Jet("./views", ".jet").Reload(true))
mypathRoute := app.Get("/mypath", writePathHandler)
mypathRoute.Name = "my-page1"
mypath2Route := app.Get("/mypath2/{paramfirst}/{paramsecond}", writePathHandler)
mypath2Route.Name = "my-page2"
mypath3Route := app.Get("/mypath3/{paramfirst}/statichere/{paramsecond}", writePathHandler)
mypath3Route.Name = "my-page3"
mypath4Route := app.Get("/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}", writePathHandler)
// same as: app.Get("/mypath4/:paramfirst/statichere/:paramsecond/:otherparam/*something", writePathHandler)
mypath4Route.Name = "my-page4"
// same with Handle/Func
mypath5Route := app.Handle("GET", "/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{something:path}", writePathHandler)
mypath5Route.Name = "my-page5"
mypath6Route := app.Get("/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}", writePathHandler)
mypath6Route.Name = "my-page6"
app.Get("/", func(ctx iris.Context) {
// for /mypath6...
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.jet"); err != nil {
panic(err)
}
})
app.Get("/redirect/{namedRoute}", func(ctx iris.Context) {
routeName := ctx.Params().Get("namedRoute")
r := app.GetRoute(routeName)
if r == nil {
ctx.StatusCode(iris.StatusNotFound)
ctx.Writef("Route with name %s not found", routeName)
return
}
println("The path of " + routeName + "is: " + r.Path)
// if routeName == "my-page1"
// prints: The path of of my-page1 is: /mypath
// if it's a path which takes named parameters
// then use "r.ResolvePath(paramValuesHere)"
ctx.Redirect(r.Path)
// http://localhost:8080/redirect/my-page1 will redirect to -> http://localhost:8080/mypath
})
// http://localhost:8080
// http://localhost:8080/redirect/my-page1
app.Run(iris.Addr(":8080"))
}
func writePathHandler(ctx iris.Context) {
ctx.Writef("Hello from %s.", ctx.Path())
}

View File

@@ -0,0 +1,24 @@
<a href="{{urlpath("my-page1")}}">/mypath</a>
<br />
<br/>
<a href="{{urlpath("my-page2","theParam1","theParam2")}}">/mypath2/{paramfirst}/{paramsecond}</a>
<br />
<br />
<a href="{{urlpath("my-page3", "theParam1", "theParam2AfterStatic")}}">/mypath3/{paramfirst}/statichere/{paramsecond}</a>
<br />
<br />
<a href="{{urlpath("my-page4", "theParam1", "theparam2AfterStatic", "otherParam", "matchAnything")}}">
/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}</a>
<br />
<br />
<a href="{{urlpath("my-page5", "theParam1", "theParam2Afterstatichere", "otherParam", "matchAnythingAfterStatic")}}">
/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{anything:path}</a>
<br />
<br />
<a href={{urlpath("my-page6", .ParamsAsArray)}}>
/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}
</a>