dataEconomy/main.go

168 lines
3.3 KiB
Go
Raw Normal View History

2016-02-29 20:16:20 +01:00
package main
import (
2016-03-04 10:13:48 +01:00
"errors"
2016-02-29 21:23:47 +01:00
"fmt"
2016-03-01 19:40:13 +01:00
"io/ioutil"
"os"
2016-03-02 22:55:10 +01:00
"strings"
2016-03-01 19:40:13 +01:00
2016-03-01 19:14:44 +01:00
log "github.com/Sirupsen/logrus"
2016-03-01 19:40:13 +01:00
haikunator "github.com/atrox/haikunatorgo"
2016-02-29 21:23:47 +01:00
"github.com/gin-gonic/contrib/sessions"
2016-02-29 20:16:20 +01:00
"github.com/gin-gonic/gin"
)
2016-03-01 19:14:44 +01:00
const SessionHeader = "slide-session"
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"))
2016-03-01 19:14:44 +01:00
r.Use(sessions.Sessions(SessionHeader, store))
2016-02-29 21:23:47 +01:00
2016-03-03 10:38:57 +01:00
r.LoadHTMLGlob("templates/*.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-03-02 22:55:10 +01:00
fname := c.Param("name")
log.WithFields(log.Fields{
"name": fname,
}).Info("Restore?")
2016-02-29 21:23:47 +01:00
haikunator := haikunator.NewHaikunator()
haikunator.TokenLength = 0
name := haikunator.Haikunate()
2016-02-29 21:34:47 +01:00
path := fmt.Sprintf("slides/%s.md", name)
2016-03-01 19:14:44 +01:00
log.WithFields(log.Fields{
"path": path,
}).Info("A new session")
2016-02-29 21:23:47 +01:00
session := sessions.Default(c)
session.Set("name", path)
session.Save()
2016-02-29 21:34:47 +01:00
2016-03-03 10:38:57 +01:00
c.HTML(200, "index.tmpl", gin.H{
2016-02-29 21:23:47 +01:00
"pubTo": path,
2016-02-29 20:29:47 +01:00
})
})
2016-03-04 10:13:48 +01:00
mustHaveSession := func(c *gin.Context) (string, error) {
2016-02-29 21:23:47 +01:00
session := sessions.Default(c)
val := session.Get("name")
2016-03-04 10:13:48 +01:00
emptySession := errors.New("Emtpy session")
2016-03-01 19:14:44 +01:00
if val == nil {
c.String(400, "No context")
2016-03-04 10:13:48 +01:00
return "", emptySession
2016-03-01 19:14:44 +01:00
}
log.WithFields(log.Fields{
"path": val,
}).Info("Got session")
2016-02-29 21:23:47 +01:00
path, ok := val.(string)
if !ok {
2016-02-29 21:34:47 +01:00
c.String(400, "No context")
2016-03-04 10:13:48 +01:00
return "", emptySession
}
return path, nil
}
r.GET("/slides.md", func(c *gin.Context) {
path, err := mustHaveSession(c)
if err != nil {
return
2016-02-29 21:23:47 +01:00
}
if _, err := os.Stat(path); err != nil {
2016-03-01 19:14:44 +01:00
// copy sample markdown file to the path
2016-02-29 21:23:47 +01:00
body, err := ioutil.ReadFile("initial-slides.md")
if err != nil {
panic(err)
}
ioutil.WriteFile(path, body, 0644)
2016-02-29 21:34:47 +01:00
c.String(200, string(body))
return
2016-02-29 21:23:47 +01:00
}
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-03-04 10:13:48 +01:00
path, err := mustHaveSession(c)
if err != nil {
2016-03-01 19:14:44 +01:00
return
2016-02-29 21:23:47 +01:00
}
body, _ := ioutil.ReadAll(c.Request.Body)
ioutil.WriteFile(path, body, 0644)
2016-03-01 19:14:44 +01:00
log.WithFields(log.Fields{
"size": len(body),
"file": path,
}).Info("Wrote to file")
2016-02-29 21:01:13 +01:00
c.String(200, "")
2016-02-29 20:16:20 +01:00
})
2016-03-02 22:55:10 +01:00
r.GET("/stash", func(c *gin.Context) {
files, err := ioutil.ReadDir("slides")
if err != nil {
log.Fatal(err)
}
var stash []string
for _, file := range files {
stash = append(stash, file.Name())
}
c.JSON(200, gin.H{
"data": stash,
})
})
r.GET("/stash/edit/:name", func(c *gin.Context) {
name := c.Param("name")
log.WithFields(log.Fields{
"name": name,
}).Info("Restore session?")
if strings.HasSuffix(name, ".md") {
name = name[0 : len(name)-3]
}
path := fmt.Sprintf("slides/%s.md", name)
session := sessions.Default(c)
session.Set("name", path)
session.Save()
2016-03-03 10:38:57 +01:00
c.HTML(200, "index.tmpl", gin.H{
2016-03-02 22:55:10 +01:00
"pubTo": path,
})
})
2016-03-03 10:38:57 +01:00
r.GET("/published/slides/:name", func(c *gin.Context) {
2016-03-02 22:55:10 +01:00
name := c.Param("name")
log.WithFields(log.Fields{
"name": name,
}).Info("Published")
if strings.HasSuffix(name, ".md") {
name = name[0 : len(name)-3]
}
path := fmt.Sprintf("slides/%s.md", name)
session := sessions.Default(c)
session.Set("name", path)
session.Save()
2016-03-03 10:38:57 +01:00
c.HTML(200, "slides.tmpl", gin.H{
"pubTo": path,
})
2016-03-02 22:55:10 +01:00
})
2016-02-29 20:16:20 +01:00
return r
}
func main() {
r := NewApp()
r.Run(":8080")
}