Updates for urls

This commit is contained in:
Alex Myasoedov 2016-03-02 23:55:10 +02:00
parent 8ddd1d61bb
commit 5b877cfbdc
2 changed files with 74 additions and 0 deletions

View file

@ -45,6 +45,21 @@ func Test(t *testing.T) {
g.Assert(w.Code).Equal(200)
})
g.It("Should return list of slides ", func() {
w := client("GET", "/stash", cookie)
g.Assert(w.Code).Equal(200)
})
g.It("Should return specific slide in preview", func() {
w := client("GET", "/published/shy-cell.md", cookie)
g.Assert(w.Code).Equal(200)
})
g.It("Should return specific slide in edit mode", func() {
w := client("GET", "/stash/edit/shy-cell.md", cookie)
g.Assert(w.Code).Equal(200)
})
g.It("Should works")
})

59
main.go
View file

@ -3,7 +3,9 @@ package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
log "github.com/Sirupsen/logrus"
haikunator "github.com/atrox/haikunatorgo"
@ -24,6 +26,12 @@ func NewApp() *gin.Engine {
r.Static("/static", "./static")
r.GET("/", func(c *gin.Context) {
fname := c.Param("name")
log.WithFields(log.Fields{
"name": fname,
}).Info("Restore?")
haikunator := haikunator.NewHaikunator()
haikunator.TokenLength = 0
name := haikunator.Haikunate()
@ -96,6 +104,57 @@ func NewApp() *gin.Engine {
c.String(200, "")
})
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()
c.HTML(200, "users/index.tmpl", gin.H{
"pubTo": path,
})
})
r.GET("/published/:name", func(c *gin.Context) {
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()
c.Redirect(http.StatusMovedPermanently, "static/preview")
})
return r
}