forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
41 lines (34 loc) · 918 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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")
}