diff --git a/_examples/sessions/database/redis/main.go b/_examples/sessions/database/redis/main.go index 8cc67fca..1c5518f5 100644 --- a/_examples/sessions/database/redis/main.go +++ b/_examples/sessions/database/redis/main.go @@ -42,9 +42,10 @@ func main() { defer db.Close() // close the database connection if application errored. sess := sessions.New(sessions.Config{ - Cookie: "sessionscookieid", - Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute, - AllowReclaim: true, + Cookie: "sessionscookieid", + Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute, + AllowReclaim: true, + CookieSecureTLS: true, }) // @@ -54,70 +55,73 @@ func main() { // the rest of the code stays the same. app := iris.New() - // app.Logger().SetLevel("debug") app.Get("/", func(ctx iris.Context) { ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead") }) + app.Get("/set", func(ctx iris.Context) { - s := sess.Start(ctx) + session := sessions.Get(ctx) // set session values - s.Set("name", "iris") + session.Set("name", "iris") // test if set here - ctx.Writef("All ok session value of the 'name' is: %s", s.GetString("name")) + ctx.Writef("All ok session value of the 'name' is: %s", session.GetString("name")) }) app.Get("/set/{key}/{value}", func(ctx iris.Context) { key, value := ctx.Params().Get("key"), ctx.Params().Get("value") - s := sess.Start(ctx) + session := sessions.Get(ctx) // set session values - s.Set(key, value) + session.Set(key, value) // test if set here - ctx.Writef("All ok session value of the '%s' is: %s", key, s.GetString(key)) + ctx.Writef("All ok session value of the '%s' is: %s", key, session.GetString(key)) }) app.Get("/set/int/{key}/{value}", func(ctx iris.Context) { key := ctx.Params().Get("key") value, _ := ctx.Params().GetInt("value") - s := sess.Start(ctx) + session := sessions.Get(ctx) // set session values - s.Set(key, value) - valueSet := s.Get(key) + session.Set(key, value) + valueSet := session.Get(key) // test if set here ctx.Writef("All ok session value of the '%s' is: %v", key, valueSet) }) app.Get("/get/{key}", func(ctx iris.Context) { key := ctx.Params().Get("key") - value := sess.Start(ctx).Get(key) + session := sessions.Get(ctx) + value := session.Get(key) ctx.Writef("The '%s' on the /set was: %v", key, value) }) app.Get("/get", func(ctx iris.Context) { // get a specific key, as string, if no found returns just an empty string - name := sess.Start(ctx).GetString("name") + session := sessions.Get(ctx) + name := session.GetString("name") ctx.Writef("The 'name' on the /set was: %s", name) }) app.Get("/get/{key}", func(ctx iris.Context) { // get a specific key, as string, if no found returns just an empty string - name := sess.Start(ctx).GetString(ctx.Params().Get("key")) + session := sessions.Get(ctx) + name := session.GetString(ctx.Params().Get("key")) ctx.Writef("The name on the /set was: %s", name) }) app.Get("/delete", func(ctx iris.Context) { // delete a specific key - sess.Start(ctx).Delete("name") + sessions.Get(ctx).Delete("name") }) app.Get("/clear", func(ctx iris.Context) { // removes all entries - sess.Start(ctx).Clear() + sessions.Get(ctx).Clear() }) app.Get("/destroy", func(ctx iris.Context) { @@ -141,5 +145,5 @@ func main() { } }) - app.Listen(":8080", iris.WithoutServerError(iris.ErrServerClosed)) + app.Listen(":8080") } diff --git a/core/netutil/ip.go b/core/netutil/ip.go index 7ae395f7..04ff01fd 100644 --- a/core/netutil/ip.go +++ b/core/netutil/ip.go @@ -11,7 +11,7 @@ https://husobee.github.io/golang/ip-address/2015/12/17/remote-ip-go.html request https://github.com/kataras/iris/issues/1453 */ -//IPRange is a structure that holds the start and end of a range of IP Addresses. +// IPRange is a structure that holds the start and end of a range of IP Addresses. type IPRange struct { Start net.IP `json:"start" yaml:"Start" toml"Start"` End net.IP `json:"end" yaml:"End" toml"End"` diff --git a/core/router/api_builder.go b/core/router/api_builder.go index 0118cd94..33b7a24f 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -463,7 +463,7 @@ func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handl subdomain, path := splitSubdomainAndPath(fullpath) // if allowMethods are empty, then simply register with the passed, main, method. - methods = append(api.allowMethods, methods...) + methods = removeDuplString(append(api.allowMethods, methods...)) routes := make([]*Route, len(methods)) @@ -487,6 +487,20 @@ func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handl return routes } +func removeDuplString(elements []string) (result []string) { + seen := make(map[string]struct{}) + + for v := range elements { + val := elements[v] + if _, ok := seen[val]; !ok { + seen[val] = struct{}{} + result = append(result, val) + } + } + + return result +} + // Party groups routes which may have the same prefix and share same handlers, // returns that new rich subrouter. // diff --git a/mvc/controller_handle_test.go b/mvc/controller_handle_test.go index eadc0945..7ad653d2 100644 --- a/mvc/controller_handle_test.go +++ b/mvc/controller_handle_test.go @@ -185,14 +185,17 @@ func (c *testControllerGetBy) GetBy(age int64) *testCustomStruct { } } -func TestControllerGetBy(t *testing.T) { - // Tests only GetBy. +func TestControllerGetByWithAllowMethods(t *testing.T) { app := iris.New() app.Configure(iris.WithFireMethodNotAllowed) + // ^ this 405 status will not be fired on POST: project/... because of + // .AllowMethods, but it will on PUT. + + New(app.Party("/project").AllowMethods(iris.MethodGet, iris.MethodPost)).Handle(new(testControllerGetBy)) - New(app.Party("/project")).Handle(new(testControllerGetBy)) e := httptest.New(t, app) e.GET("/project/42").Expect().Status(httptest.StatusOK). JSON().Equal(&testCustomStruct{Age: 42, Name: "name"}) - e.POST("/project/42").Expect().Status(httptest.StatusMethodNotAllowed) + e.POST("/project/42").Expect().Status(httptest.StatusOK) + e.PUT("/project/42").Expect().Status(httptest.StatusMethodNotAllowed) }