1
0
Fork 0
editiCollaborativo/main.go

218 lines
4.4 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"
2018-02-24 03:28:31 +01:00
"sort"
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"
"github.com/gin-contrib/sessions"
2016-02-29 20:16:20 +01:00
"github.com/gin-gonic/gin"
2018-02-24 19:06:48 +01:00
cache "github.com/hashicorp/golang-lru"
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"))
2018-02-24 19:06:48 +01:00
arc, err := cache.NewARC(10)
if err != nil {
log.Fatalf("Failied to allocate cache %#v", err)
}
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")
2018-03-06 02:24:43 +01:00
emptySession := errors.New("Empty 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
}
2018-02-24 19:06:48 +01:00
var slide string
cached, ok := arc.Get(path)
if ok {
slide = string(cached.([]byte))
} else {
body, err := ioutil.ReadFile(path)
if err != nil {
log.Errorf("Failied to read file %#v", err)
c.Abort()
return
}
slide = string(body)
2016-02-29 20:16:20 +01:00
}
2018-02-24 19:06:48 +01:00
c.String(200, slide)
2016-02-29 20:16:20 +01:00
})
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)
2018-02-24 19:06:48 +01:00
arc.Add(path, body)
go ioutil.WriteFile(path, body, 0644)
2016-03-01 19:14:44 +01:00
log.WithFields(log.Fields{
"size": len(body),
"file": path,
2018-02-24 19:06:48 +01:00
}).Info("Async 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)
}
2018-02-24 03:28:31 +01:00
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().Unix() > files[j].ModTime().Unix()
})
2016-03-02 22:55:10 +01:00
var stash []string
for _, file := range files {
if file.IsDir() {
continue
}
stash = append(stash, file.Name())
2016-03-02 22:55:10 +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
}