package main
import (
"github.com/gorilla/sessions"
"net/http"
"html/template"
"github.com/labstack/echo"
"github.com/labstack/echo-contrib/session"
"io"
)
func countUp(c echo.Context) int {
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
}
cnt := 0
if v, ok := sess.Values["cnt"]; ok {
if c, ok := v.(int); ok {
cnt = c + 1
}
}
sess.Values["cnt"] = cnt
sess.Save(c.Request(), c.Response())
return cnt
}
func topPage(c echo.Context) error {
cnt := countUp(c)
return c.Render(http.StatusOK, "index", map[string]interface{}{
"Cnt": cnt,
})
}
type Renderer struct {
templates *template.Template
}
func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return r.templates.ExecuteTemplate(w, name, data)
}
func tAdd(a, b int64) int64 {
return a + b
}
func tRange(a, b int64) []int64 {
r := make([]int64, b-a+1)
for i := int64(0); i <= (b - a); i++ {
r[i] = a + i
}
return r
}
func main() {
e := echo.New()
funcs := template.FuncMap{
"add": tAdd,
"xrange": tRange,
}
e.Renderer = &Renderer{
templates: template.Must(template.New("").Funcs(funcs).ParseGlob("views/*.html")),
}
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
e.GET("/", topPage)
e.Start(":5000")
}
Created
June 29, 2018 02:07
-
-
Save dorako321/1374d7272031abdb5b9f75e976690a2e to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment