mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 19:21:03 +01:00
7c5d7cae05
Part 2. Former-commit-id: 3ccb7c259e86c0b6e5147d372aa9cff10c1b5bb1
42 lines
918 B
Go
42 lines
918 B
Go
package main
|
|
|
|
import (
|
|
"gopkg.in/kataras/iris.v6"
|
|
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
|
"gopkg.in/kataras/iris.v6/adaptors/sessions"
|
|
)
|
|
|
|
func newApp() *iris.Framework {
|
|
app := iris.New()
|
|
app.Adapt(httprouter.New())
|
|
app.Adapt(sessions.New(sessions.Config{Cookie: "mysessionid"}))
|
|
|
|
app.Get("/hello", func(ctx *iris.Context) {
|
|
sess := ctx.Session()
|
|
if !sess.HasFlash() /* or sess.GetFlash("name") == "", same thing here */ {
|
|
ctx.HTML(iris.StatusUnauthorized, "<h1> Unauthorized Page! </h1>")
|
|
return
|
|
}
|
|
|
|
ctx.JSON(iris.StatusOK, iris.Map{
|
|
"Message": "Hello",
|
|
"From": sess.GetFlash("name"),
|
|
})
|
|
})
|
|
|
|
app.Post("/login", func(ctx *iris.Context) {
|
|
sess := ctx.Session()
|
|
if !sess.HasFlash() {
|
|
sess.SetFlash("name", ctx.FormValue("name"))
|
|
}
|
|
// let's no redirect, just set the flash message, nothing more.
|
|
})
|
|
|
|
return app
|
|
}
|
|
|
|
func main() {
|
|
app := newApp()
|
|
app.Listen(":8080")
|
|
}
|