diff --git a/adaptors/view/_examples/template_html_3/main.go b/adaptors/view/_examples/template_html_3/main.go
index b498e32c..77f9866a 100644
--- a/adaptors/view/_examples/template_html_3/main.go
+++ b/adaptors/view/_examples/template_html_3/main.go
@@ -4,25 +4,79 @@ package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/gorillamux"
+ "gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/view"
)
func main() {
app := iris.New()
app.Adapt(iris.DevLogger())
- app.Adapt(gorillamux.New())
+ app.Adapt(view.HTML("./templates", ".html").Reload(true))
- app.Adapt(view.HTML("./templates", ".html"))
+ startWithHTTPRouter(app)
+ // or uncomment
+ // startWithGorillamux()
- app.Get("/mypath", emptyHandler).ChangeName("my-page1")
- app.Get("/mypath2/{param1}/{param2}", emptyHandler).ChangeName("my-page2")
- app.Get("/mypath3/{param1}/statichere/{param2}", emptyHandler).ChangeName("my-page3")
- app.Get("/mypath4/{param1}/statichere/{param2}/{otherparam}/{something:.*}", emptyHandler).ChangeName("my-page4")
+ app.Listen("localhost:8080")
+
+}
+
+func writePathHandler(ctx *iris.Context) {
+ ctx.Writef("Hello from %s.", ctx.Path())
+}
+
+func startWithHTTPRouter(app *iris.Framework) {
+
+ app.Adapt(httprouter.New())
+
+ app.Get("/mypath", writePathHandler).ChangeName("my-page1")
+ app.Get("/mypath2/:param1/:param2", writePathHandler).ChangeName("my-page2")
+ app.Get("/mypath3/:param1/statichere/:param2", writePathHandler).ChangeName("my-page3")
+ app.Get("/mypath4/:param1/statichere/:param2/:otherparam/*something", writePathHandler).ChangeName("my-page4")
// same with Handle/Func
- app.HandleFunc("GET", "/mypath5/{param1}/statichere/{param2}/{otherparam}/anything/{something:.*}", emptyHandler).ChangeName("my-page5")
+ app.HandleFunc("GET", "/mypath5/:param1/statichere/:param2/:otherparam/anything/*something", writePathHandler).ChangeName("my-page5")
- app.Get("/mypath6/{param1}/{param2}/staticParam/{param3AfterStatic}", emptyHandler).ChangeName("my-page6")
+ app.Get("/mypath6/:param1/:param2/staticParam/:param3AfterStatic", writePathHandler).ChangeName("my-page6")
+
+ app.Get("/", func(ctx *iris.Context) {
+ // for /mypath6...
+ paramsAsArray := []string{"param1", "theParam1",
+ "param2", "theParam2",
+ "param3AfterStatic", "theParam3"}
+
+ if err := ctx.Render("page.html", iris.Map{"ParamsAsArray": paramsAsArray}); err != nil {
+ panic(err)
+ }
+ })
+
+ app.Get("/redirect/:namedRoute", func(ctx *iris.Context) {
+ routeName := ctx.Param("namedRoute")
+
+ println("The full uri of " + routeName + "is: " + app.URL(routeName))
+ // if routeName == "my-page1"
+ // prints: The full uri of my-page1 is: http://127.0.0.1:8080/mypath
+ ctx.RedirectTo(routeName)
+ // http://127.0.0.1:8080/redirect/my-page1 will redirect to -> http://127.0.0.1:8080/mypath
+ })
+
+}
+
+// for gorillamux adaptor is the same thing, the path syntax is the only thing changed ofc.
+// Note: Here, we could use app.RouteParam("param1") without even care what router is being used,
+// but I have two examples of the same thing in order to be more understable for you.
+func startWithGorillamux(app *iris.Framework) {
+ app.Adapt(gorillamux.New())
+
+ app.Get("/mypath", writePathHandler).ChangeName("my-page1")
+ app.Get("/mypath2/{param1}/{param2}", writePathHandler).ChangeName("my-page2")
+ app.Get("/mypath3/{param1}/statichere/{param2}", writePathHandler).ChangeName("my-page3")
+ app.Get("/mypath4/{param1}/statichere/{param2}/{otherparam}/{something:.*}", writePathHandler).ChangeName("my-page4")
+
+ // same with Handle/Func
+ app.HandleFunc("GET", "/mypath5/{param1}/statichere/{param2}/{otherparam}/anything/{something:.*}", writePathHandler).ChangeName("my-page5")
+
+ app.Get("/mypath6/{param1}/{param2}/staticParam/{param3AfterStatic}", writePathHandler).ChangeName("my-page6")
app.Get("/", func(ctx *iris.Context) {
// for /mypath6...
@@ -47,7 +101,3 @@ func main() {
app.Listen("localhost:8080")
}
-
-func emptyHandler(ctx *iris.Context) {
- ctx.Writef("Hello from %s.", ctx.Path())
-}
diff --git a/adaptors/view/_examples/template_html_3/templates/page.html b/adaptors/view/_examples/template_html_3/templates/page.html
index 798f34c8..b60139d0 100644
--- a/adaptors/view/_examples/template_html_3/templates/page.html
+++ b/adaptors/view/_examples/template_html_3/templates/page.html
@@ -3,6 +3,8 @@
http://localhost:8080/mypath2/:param1/:param2
+or path only:
+/mypath2/:param1/:param2
diff --git a/adaptors/view/_examples/template_html_4/templates/page.html b/adaptors/view/_examples/template_html_4/templates/page.html
index fab4d355..a52e8f11 100644
--- a/adaptors/view/_examples/template_html_4/templates/page.html
+++ b/adaptors/view/_examples/template_html_4/templates/page.html
@@ -4,13 +4,13 @@ is the subdomain part instead of named parameter-->
username1.127.0.0.1:8080/mypath
-username2.127.0.0.1:8080/mypath2/:param1/:param2
+username2.127.0.0.1:8080/mypath2/{param1}/{param2}
-username3.127.0.0.1:8080/mypath3/:param1/statichere/:param2
+username3.127.0.0.1:8080/mypath3/{param1}/statichere/{param2}
-username4.127.0.0.1:8080/mypath4/:param1/statichere/:param2/:otherparam/*something
+username4.127.0.0.1:8080/mypath4/{param1}/statichere/{param2}/{otherParam}/{something:.*}
-username5.127.0.0.1:8080/mypath6/:param1/:param2/staticParam/:param3AfterStatic
+username5.127.0.0.1:8080/mypath6/{param1}/{param2}/staticParam/{param3}AfterStatic
diff --git a/iris.go b/iris.go
index 55ecb923..0833d02d 100644
--- a/iris.go
+++ b/iris.go
@@ -268,7 +268,7 @@ func New(setters ...OptionSetter) *Framework {
// +------------------------------------------------------------+
s.Adapt(TemplateFuncsPolicy{
"url": s.URL,
- "urlpath": s.policies.RouterReversionPolicy.URLPath,
+ "urlpath": s.Path,
}) // the entire template registration logic lives inside the ./adaptors/view now.
}
diff --git a/policy_sessions_test.go b/policy_sessions_test.go
index 31b8d363..6eaa08e2 100644
--- a/policy_sessions_test.go
+++ b/policy_sessions_test.go
@@ -13,14 +13,38 @@ import (
)
func TestSessions(t *testing.T) {
+ app := iris.New()
+ app.Adapt(httprouter.New())
+ app.Adapt(sessions.New(sessions.Config{Cookie: "mycustomsessionid"}))
+ testSessions(app, t)
+}
+
+func TestSessionsEncodeDecode(t *testing.T) {
+ // test the sessions encode decode via gorilla.securecookie
+ app := iris.New()
+ app.Adapt(httprouter.New())
+ // IMPORTANT
+ cookieName := "mycustomsessionid"
+ // AES only supports key sizes of 16, 24 or 32 bytes.
+ // You either need to provide exactly that amount or you derive the key from what you type in.
+ hashKey := []byte("the-big-and-secret-fash-key-here")
+ blockKey := []byte("lot-secret-of-characters-big-too")
+ secureCookie := securecookie.New(hashKey, blockKey)
+
+ app.Adapt(sessions.New(sessions.Config{
+ Cookie: cookieName,
+ Encode: secureCookie.Encode,
+ Decode: secureCookie.Decode,
+ }))
+ testSessions(app, t)
+}
+
+func testSessions(app *iris.Framework, t *testing.T) {
values := map[string]interface{}{
"Name": "iris",
"Months": "4",
"Secret": "dsads£2132215£%%Ssdsa",
}
- app := iris.New()
- app.Adapt(httprouter.New())
- app.Adapt(sessions.New(sessions.Config{Cookie: "mycustomsessionid"}))
writeValues := func(ctx *iris.Context) {
sessValues := ctx.Session().GetAll()
@@ -170,88 +194,3 @@ func TestFlashMessages(t *testing.T) {
// e.GET("/get/").Expect().Status(http.StatusOK).JSON().Object().Equal(values)
e.GET("/get_single").Expect().Status(iris.StatusOK).Body().Equal(valueSingleValue)
}
-
-func TestSessionsEncodeDecode(t *testing.T) {
- // test the sessions encode decode via gorilla.securecookie
- values := map[string]interface{}{
- "Name": "iris",
- "Months": "4",
- "Secret": "dsads£2132215£%%Ssdsa",
- }
- app := iris.New()
- app.Adapt(httprouter.New())
- // IMPORTANT
- cookieName := "mycustomsessionid"
- // AES only supports key sizes of 16, 24 or 32 bytes.
- // You either need to provide exactly that amount or you derive the key from what you type in.
- hashKey := []byte("the-big-and-secret-fash-key-here")
- blockKey := []byte("lot-secret-of-characters-big-too")
- secureCookie := securecookie.New(hashKey, blockKey)
-
- app.Adapt(sessions.New(sessions.Config{
- Cookie: cookieName,
- Encode: secureCookie.Encode,
- Decode: secureCookie.Decode,
- }))
- //
-
- writeValues := func(ctx *iris.Context) {
- sessValues := ctx.Session().GetAll()
- ctx.JSON(iris.StatusOK, sessValues)
- }
-
- if testEnableSubdomain {
- app.Party(testSubdomain+".").Get("/get", func(ctx *iris.Context) {
- writeValues(ctx)
- })
- }
-
- app.Post("set", func(ctx *iris.Context) {
- vals := make(map[string]interface{}, 0)
- if err := ctx.ReadJSON(&vals); err != nil {
- t.Fatalf("Cannot readjson. Trace %s", err.Error())
- }
- for k, v := range vals {
- ctx.Session().Set(k, v)
- }
- })
-
- app.Get("/get", func(ctx *iris.Context) {
- writeValues(ctx)
- })
-
- app.Get("/clear", func(ctx *iris.Context) {
- ctx.Session().Clear()
- writeValues(ctx)
- })
-
- app.Get("/destroy", func(ctx *iris.Context) {
- ctx.SessionDestroy()
- writeValues(ctx)
- // the cookie and all values should be empty
- })
-
- // request cookie should be empty
- app.Get("/after_destroy", func(ctx *iris.Context) {
- })
- app.Config.VHost = "mydomain.com"
- e := httptest.New(app, t)
-
- e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
- e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
- if testEnableSubdomain {
- es := subdomainTester(e, app)
- es.Request("GET", "/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
- }
-
- // test destroy which also clears first
- d := e.GET("/destroy").Expect().Status(iris.StatusOK)
- d.JSON().Object().Empty()
- // This removed: d.Cookies().Empty(). Reason:
- // httpexpect counts the cookies setted or deleted at the response time, but cookie is not removed, to be really removed needs to SetExpire(now-1second) so,
- // test if the cookies removed on the next request, like the browser's behavior.
- e.GET("/after_destroy").Expect().Status(iris.StatusOK).Cookies().Empty()
- // set and clear again
- e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
- e.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
-}