From 5b877cfbdcdcbc677152673d09704943ee0bcdd1 Mon Sep 17 00:00:00 2001 From: Alex Myasoedov Date: Wed, 2 Mar 2016 23:55:10 +0200 Subject: [PATCH] Updates for urls --- app_test.go | 15 ++++++++++++++ main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/app_test.go b/app_test.go index 39f8ce5..b2f4766 100644 --- a/app_test.go +++ b/app_test.go @@ -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") }) diff --git a/main.go b/main.go index b4fbc14..4c37334 100644 --- a/main.go +++ b/main.go @@ -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 }