videoconf/main.go

235 lines
4.8 KiB
Go
Raw Normal View History

2016-02-29 20:16:20 +01:00
package main
import (
2017-11-12 23:45:20 +01:00
"encoding/base64"
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"
2017-11-12 23:45:20 +01:00
"strconv"
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-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
2017-11-12 23:45:20 +01:00
func Header(c *gin.Context, key string) string {
if values, _ := c.Request.Header[key]; len(values) > 0 {
return values[0]
}
return ""
}
func BasicAuth() gin.HandlerFunc {
realm := "Authorization Required"
realm = "Basic realm=" + strconv.Quote(realm)
user := os.Getenv("USER")
password := os.Getenv("PASSWORD")
enabled := isEnabled(user, password)
if enabled {
log.Warn("Auth mode enabled")
log.Warn(fmt.Sprintf("Visit http://%s:%s@0.0.0.0:8080", user, password))
}
return func(c *gin.Context) {
header := Header(c, "Authorization")
if enabled && header != authorizationHeader(user, password) {
// Credentials doesn't match, we return 401 and abort handlers chain.
c.Header("WWW-Authenticate", realm)
c.AbortWithStatus(401)
return
}
c.Next()
}
}
func isEnabled(user, password string) bool {
switch {
case user == "":
return false
case password == "":
return false
default:
return true
}
}
func authorizationHeader(user, password string) string {
base := user + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
}
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))
2017-11-12 23:45:20 +01:00
r.Use(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")
2016-02-29 20:29:47 +01:00
r.GET("/", func(c *gin.Context) {
2016-03-02 22:55:10 +01:00
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:01:16 +01:00
"name": latest,
}).Info("Restoring latest point")
var path string
if latest == "" {
haikunator := haikunator.New()
haikunator.TokenLength = 0
name := haikunator.Haikunate()
path = fmt.Sprintf("slides/%s.md", name)
} else {
name := latest
path = fmt.Sprintf("slides/%s", 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
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.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]
}
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()
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
}