Created
July 1, 2019 03:13
-
-
Save iammarkps/a5c1ff9503e751bb4ad5f3539c7015af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
_ "github.com/gomodule/redigo/redis" | |
"github.com/gorilla/sessions" | |
"github.com/labstack/echo" | |
"github.com/labstack/echo-contrib/session" | |
"github.com/labstack/echo/middleware" | |
"gopkg.in/boj/redistore.v1" | |
"net/http" | |
) | |
func main() { | |
e := echo.New() | |
e.Use(middleware.Logger()) | |
e.Use(middleware.CORS()) | |
e.Use(middleware.Gzip()) | |
e.Use(middleware.Secure()) | |
e.Use(middleware.Recover()) | |
key := []byte("super-secret-key") | |
store, _ := redistore.NewRediStore(10, "tcp", ":6379", "", key) | |
e.Use(session.Middleware(store)) | |
e.GET("/login", func(c echo.Context) error { | |
sess, _ := session.Get("session", c) | |
sess.Options = &sessions.Options{ | |
Path: "/", | |
MaxAge: 86400 * 7, | |
HttpOnly: true, | |
} | |
sess.Values["authenticated"] = "true" | |
_ = sess.Save(c.Request(), c.Response()) | |
return c.String(http.StatusOK, "Login") | |
}) | |
e.GET("/logout", func(c echo.Context) error { | |
sess, _ := session.Get("session", c) | |
sess.Values["authenticated"] = "false" | |
_ = sess.Save(c.Request(), c.Response()) | |
return c.String(http.StatusOK, "Logout") | |
}) | |
e.GET("/secret", func(c echo.Context) error { | |
sess, _ := session.Get("session", c) | |
if sess.Values["authenticated"] == "false" { | |
return c.String(http.StatusForbidden, "WTF") | |
} | |
return c.String(http.StatusOK, "Secret") | |
}) | |
e.Logger.Fatal(e.Start(":1323")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment