dataEconomy/main.go

77 lines
1.5 KiB
Go
Raw Normal View History

2016-02-29 20:16:20 +01:00
package main
import (
2016-02-29 21:23:47 +01:00
"fmt"
"github.com/atrox/haikunatorgo"
"github.com/gin-gonic/contrib/sessions"
2016-02-29 20:16:20 +01:00
"github.com/gin-gonic/gin"
"io/ioutil"
2016-02-29 21:23:47 +01:00
"os"
2016-02-29 20:16:20 +01:00
)
func NewApp() *gin.Engine {
r := gin.Default()
2016-02-29 21:23:47 +01:00
store := sessions.NewCookieStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
2016-02-29 20:42:00 +01:00
r.LoadHTMLGlob("templates/index.tmpl")
2016-02-29 21:23:47 +01:00
r.Static("/static", "./static")
2016-02-29 20:29:47 +01:00
r.GET("/", func(c *gin.Context) {
2016-02-29 21:23:47 +01:00
haikunator := haikunator.NewHaikunator()
haikunator.TokenLength = 0
name := haikunator.Haikunate()
path := fmt.Sprintf("%s.md", name)
session := sessions.Default(c)
session.Set("name", path)
session.Save()
2016-02-29 20:42:00 +01:00
c.HTML(200, "users/index.tmpl", gin.H{
2016-02-29 21:23:47 +01:00
"pubTo": path,
2016-02-29 20:29:47 +01:00
})
})
2016-02-29 20:16:20 +01:00
r.GET("/slides.md", func(c *gin.Context) {
2016-02-29 21:23:47 +01:00
session := sessions.Default(c)
val := session.Get("name")
path, ok := val.(string)
if !ok {
panic("unlucky")
}
if _, err := os.Stat(path); err != nil {
body, err := ioutil.ReadFile("initial-slides.md")
if err != nil {
panic(err)
}
ioutil.WriteFile(path, body, 0644)
}
body, err := ioutil.ReadFile(path)
2016-02-29 20:16:20 +01:00
if err != nil {
panic(err)
}
c.String(200, string(body))
})
2016-02-29 20:29:47 +01:00
r.PUT("/slides.md", func(c *gin.Context) {
2016-02-29 21:23:47 +01:00
session := sessions.Default(c)
val := session.Get("name")
path, ok := val.(string)
if !ok {
panic("unlucky")
}
body, _ := ioutil.ReadAll(c.Request.Body)
ioutil.WriteFile(path, body, 0644)
2016-02-29 21:01:13 +01:00
c.String(200, "")
2016-02-29 20:16:20 +01:00
})
return r
}
func main() {
r := NewApp()
r.Run(":8080")
}