Former-commit-id: 85c8b1e20da6e39485478025ef1b0f80ef953e4a
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-04-21 09:27:28 +03:00
parent 6c6de6b85d
commit 0cf5d5a4a3
4 changed files with 46 additions and 25 deletions

View File

@ -42,9 +42,10 @@ func main() {
defer db.Close() // close the database connection if application errored. defer db.Close() // close the database connection if application errored.
sess := sessions.New(sessions.Config{ sess := sessions.New(sessions.Config{
Cookie: "sessionscookieid", Cookie: "sessionscookieid",
Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute, Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute,
AllowReclaim: true, AllowReclaim: true,
CookieSecureTLS: true,
}) })
// //
@ -54,70 +55,73 @@ func main() {
// the rest of the code stays the same. // the rest of the code stays the same.
app := iris.New() app := iris.New()
// app.Logger().SetLevel("debug")
app.Get("/", func(ctx iris.Context) { app.Get("/", func(ctx iris.Context) {
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead") ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
}) })
app.Get("/set", func(ctx iris.Context) { app.Get("/set", func(ctx iris.Context) {
s := sess.Start(ctx) session := sessions.Get(ctx)
// set session values // set session values
s.Set("name", "iris") session.Set("name", "iris")
// test if set here // 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) { app.Get("/set/{key}/{value}", func(ctx iris.Context) {
key, value := ctx.Params().Get("key"), ctx.Params().Get("value") key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
s := sess.Start(ctx) session := sessions.Get(ctx)
// set session values // set session values
s.Set(key, value) session.Set(key, value)
// test if set here // 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) { app.Get("/set/int/{key}/{value}", func(ctx iris.Context) {
key := ctx.Params().Get("key") key := ctx.Params().Get("key")
value, _ := ctx.Params().GetInt("value") value, _ := ctx.Params().GetInt("value")
s := sess.Start(ctx) session := sessions.Get(ctx)
// set session values // set session values
s.Set(key, value) session.Set(key, value)
valueSet := s.Get(key) valueSet := session.Get(key)
// test if set here // test if set here
ctx.Writef("All ok session value of the '%s' is: %v", key, valueSet) ctx.Writef("All ok session value of the '%s' is: %v", key, valueSet)
}) })
app.Get("/get/{key}", func(ctx iris.Context) { app.Get("/get/{key}", func(ctx iris.Context) {
key := ctx.Params().Get("key") 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) ctx.Writef("The '%s' on the /set was: %v", key, value)
}) })
app.Get("/get", func(ctx iris.Context) { app.Get("/get", func(ctx iris.Context) {
// get a specific key, as string, if no found returns just an empty string // 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) ctx.Writef("The 'name' on the /set was: %s", name)
}) })
app.Get("/get/{key}", func(ctx iris.Context) { app.Get("/get/{key}", func(ctx iris.Context) {
// get a specific key, as string, if no found returns just an empty string // 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) ctx.Writef("The name on the /set was: %s", name)
}) })
app.Get("/delete", func(ctx iris.Context) { app.Get("/delete", func(ctx iris.Context) {
// delete a specific key // delete a specific key
sess.Start(ctx).Delete("name") sessions.Get(ctx).Delete("name")
}) })
app.Get("/clear", func(ctx iris.Context) { app.Get("/clear", func(ctx iris.Context) {
// removes all entries // removes all entries
sess.Start(ctx).Clear() sessions.Get(ctx).Clear()
}) })
app.Get("/destroy", func(ctx iris.Context) { app.Get("/destroy", func(ctx iris.Context) {
@ -141,5 +145,5 @@ func main() {
} }
}) })
app.Listen(":8080", iris.WithoutServerError(iris.ErrServerClosed)) app.Listen(":8080")
} }

View File

@ -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 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 { type IPRange struct {
Start net.IP `json:"start" yaml:"Start" toml"Start"` Start net.IP `json:"start" yaml:"Start" toml"Start"`
End net.IP `json:"end" yaml:"End" toml"End"` End net.IP `json:"end" yaml:"End" toml"End"`

View File

@ -463,7 +463,7 @@ func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handl
subdomain, path := splitSubdomainAndPath(fullpath) subdomain, path := splitSubdomainAndPath(fullpath)
// if allowMethods are empty, then simply register with the passed, main, method. // 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)) routes := make([]*Route, len(methods))
@ -487,6 +487,20 @@ func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handl
return routes 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, // Party groups routes which may have the same prefix and share same handlers,
// returns that new rich subrouter. // returns that new rich subrouter.
// //

View File

@ -185,14 +185,17 @@ func (c *testControllerGetBy) GetBy(age int64) *testCustomStruct {
} }
} }
func TestControllerGetBy(t *testing.T) { func TestControllerGetByWithAllowMethods(t *testing.T) {
// Tests only GetBy.
app := iris.New() app := iris.New()
app.Configure(iris.WithFireMethodNotAllowed) 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 := httptest.New(t, app)
e.GET("/project/42").Expect().Status(httptest.StatusOK). e.GET("/project/42").Expect().Status(httptest.StatusOK).
JSON().Equal(&testCustomStruct{Age: 42, Name: "name"}) 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)
} }