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"
|
2017-11-06 19:18:26 +01:00
|
|
|
"github.com/gin-contrib/sessions"
|
2016-02-29 20:16:20 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2018-01-19 02:22:15 +01:00
|
|
|
"github.com/msoedov/hacker-slides/auth"
|
2018-01-19 02:01:16 +01:00
|
|
|
"github.com/msoedov/hacker-slides/files"
|
2016-02-29 20:16:20 +01:00
|
|
|
)
|
|
|
|
|
2016-03-04 10:20:26 +01:00
|
|
|
const sessionHeader = "slide-session"
|
2016-03-01 19:14:44 +01:00
|
|
|
|
2018-01-19 02:18:50 +01:00
|
|
|
func SlidePath(name string) string {
|
|
|
|
return fmt.Sprintf("slides/%s.md", name)
|
|
|
|
}
|
|
|
|
|
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-04 10:20:26 +01:00
|
|
|
r.Use(sessions.Sessions(sessionHeader, store))
|
2018-01-19 02:22:15 +01:00
|
|
|
r.Use(auth.BasicAuth())
|
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")
|
2018-02-22 18:34:58 +01:00
|
|
|
r.Static("/images", "./slides/images")
|
2016-02-29 20:29:47 +01:00
|
|
|
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
2018-01-19 02:15:44 +01:00
|
|
|
isNew := c.Query("new")
|
2018-01-19 02:01:16 +01:00
|
|
|
latest := files.LatestFileIn("slides")
|
2016-03-02 22:55:10 +01:00
|
|
|
log.WithFields(log.Fields{
|
2018-01-19 02:15:44 +01:00
|
|
|
"name": latest,
|
|
|
|
"isNew": isNew,
|
2018-01-19 02:01:16 +01:00
|
|
|
}).Info("Restoring latest point")
|
|
|
|
|
2018-01-19 02:15:44 +01:00
|
|
|
var path, name string
|
|
|
|
if latest == "" || isNew != "" {
|
2018-01-19 02:01:16 +01:00
|
|
|
haikunator := haikunator.New()
|
|
|
|
haikunator.TokenLength = 0
|
2018-01-19 02:15:44 +01:00
|
|
|
name = haikunator.Haikunate()
|
2018-01-19 02:01:16 +01:00
|
|
|
} else {
|
2018-01-19 02:15:44 +01:00
|
|
|
name = strings.Replace(latest, ".md", "", 1)
|
2018-01-19 02:01:16 +01:00
|
|
|
}
|
2018-01-19 02:18:50 +01:00
|
|
|
path = SlidePath(name)
|
2016-03-02 22:55:10 +01:00
|
|
|
|
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
|
|
|
|
2018-01-19 02:15:44 +01:00
|
|
|
c.Writer.Header().Set("Location", fmt.Sprintf("/stash/edit/%s", name))
|
|
|
|
c.HTML(302, "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 {
|
2018-02-23 11:55:49 +01:00
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
stash = append(stash, file.Name())
|
2016-03-02 22:55:10 +01:00
|
|
|
}
|
2017-11-06 19:57:13 +01:00
|
|
|
c.HTML(200, "stash.tmpl", gin.H{
|
|
|
|
"stash": stash,
|
2016-03-02 22:55:10 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|
2018-01-19 02:18:50 +01:00
|
|
|
path := SlidePath(name)
|
2016-03-02 22:55:10 +01:00
|
|
|
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]
|
|
|
|
}
|
2018-01-19 02:18:50 +01:00
|
|
|
path := SlidePath(name)
|
2016-03-02 22:55:10 +01:00
|
|
|
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()
|
2017-03-02 01:08:04 +01:00
|
|
|
port := "8080"
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
port = os.Args[1]
|
|
|
|
} else {
|
|
|
|
envPort := os.Getenv("PORT")
|
|
|
|
if len(envPort) > 0 {
|
|
|
|
port = envPort
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 02:03:07 +01:00
|
|
|
log.Info("Started http://0.0.0.0:8080")
|
2017-03-02 01:08:04 +01:00
|
|
|
r.Run(fmt.Sprintf(":%s", port))
|
2016-02-29 20:16:20 +01:00
|
|
|
}
|