main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package main
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "strconv"
  9. "strings"
  10. log "github.com/Sirupsen/logrus"
  11. haikunator "github.com/atrox/haikunatorgo"
  12. "github.com/gin-contrib/sessions"
  13. "github.com/gin-gonic/gin"
  14. "github.com/msoedov/hacker-slides/files"
  15. )
  16. const sessionHeader = "slide-session"
  17. func Header(c *gin.Context, key string) string {
  18. if values, _ := c.Request.Header[key]; len(values) > 0 {
  19. return values[0]
  20. }
  21. return ""
  22. }
  23. func BasicAuth() gin.HandlerFunc {
  24. realm := "Authorization Required"
  25. realm = "Basic realm=" + strconv.Quote(realm)
  26. user := os.Getenv("USER")
  27. password := os.Getenv("PASSWORD")
  28. enabled := isEnabled(user, password)
  29. if enabled {
  30. log.Warn("Auth mode enabled")
  31. log.Warn(fmt.Sprintf("Visit http://%s:%s@0.0.0.0:8080", user, password))
  32. }
  33. return func(c *gin.Context) {
  34. header := Header(c, "Authorization")
  35. if enabled && header != authorizationHeader(user, password) {
  36. // Credentials doesn't match, we return 401 and abort handlers chain.
  37. c.Header("WWW-Authenticate", realm)
  38. c.AbortWithStatus(401)
  39. return
  40. }
  41. c.Next()
  42. }
  43. }
  44. func isEnabled(user, password string) bool {
  45. switch {
  46. case user == "":
  47. return false
  48. case password == "":
  49. return false
  50. default:
  51. return true
  52. }
  53. }
  54. func authorizationHeader(user, password string) string {
  55. base := user + ":" + password
  56. return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
  57. }
  58. func NewApp() *gin.Engine {
  59. r := gin.Default()
  60. store := sessions.NewCookieStore([]byte("secret"))
  61. r.Use(sessions.Sessions(sessionHeader, store))
  62. r.Use(BasicAuth())
  63. r.LoadHTMLGlob("templates/*.tmpl")
  64. r.Static("/static", "./static")
  65. r.GET("/", func(c *gin.Context) {
  66. latest := files.LatestFileIn("slides")
  67. log.WithFields(log.Fields{
  68. "name": latest,
  69. }).Info("Restoring latest point")
  70. var path string
  71. if latest == "" {
  72. haikunator := haikunator.New()
  73. haikunator.TokenLength = 0
  74. name := haikunator.Haikunate()
  75. path = fmt.Sprintf("slides/%s.md", name)
  76. } else {
  77. name := latest
  78. path = fmt.Sprintf("slides/%s", name)
  79. }
  80. log.WithFields(log.Fields{
  81. "path": path,
  82. }).Info("A new session")
  83. session := sessions.Default(c)
  84. session.Set("name", path)
  85. session.Save()
  86. c.HTML(200, "index.tmpl", gin.H{
  87. "pubTo": path,
  88. })
  89. })
  90. mustHaveSession := func(c *gin.Context) (string, error) {
  91. session := sessions.Default(c)
  92. val := session.Get("name")
  93. emptySession := errors.New("Emtpy session")
  94. if val == nil {
  95. c.String(400, "No context")
  96. return "", emptySession
  97. }
  98. log.WithFields(log.Fields{
  99. "path": val,
  100. }).Info("Got session")
  101. path, ok := val.(string)
  102. if !ok {
  103. c.String(400, "No context")
  104. return "", emptySession
  105. }
  106. return path, nil
  107. }
  108. r.GET("/slides.md", func(c *gin.Context) {
  109. path, err := mustHaveSession(c)
  110. if err != nil {
  111. return
  112. }
  113. if _, err := os.Stat(path); err != nil {
  114. // copy sample markdown file to the path
  115. body, err := ioutil.ReadFile("initial-slides.md")
  116. if err != nil {
  117. panic(err)
  118. }
  119. ioutil.WriteFile(path, body, 0644)
  120. c.String(200, string(body))
  121. return
  122. }
  123. body, err := ioutil.ReadFile(path)
  124. if err != nil {
  125. panic(err)
  126. }
  127. c.String(200, string(body))
  128. })
  129. r.PUT("/slides.md", func(c *gin.Context) {
  130. path, err := mustHaveSession(c)
  131. if err != nil {
  132. return
  133. }
  134. body, _ := ioutil.ReadAll(c.Request.Body)
  135. ioutil.WriteFile(path, body, 0644)
  136. log.WithFields(log.Fields{
  137. "size": len(body),
  138. "file": path,
  139. }).Info("Wrote to file")
  140. c.String(200, "")
  141. })
  142. r.GET("/stash", func(c *gin.Context) {
  143. files, err := ioutil.ReadDir("slides")
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. var stash []string
  148. for _, file := range files {
  149. stash = append(stash, file.Name())
  150. }
  151. c.HTML(200, "stash.tmpl", gin.H{
  152. "stash": stash,
  153. })
  154. })
  155. r.GET("/stash/edit/:name", func(c *gin.Context) {
  156. name := c.Param("name")
  157. log.WithFields(log.Fields{
  158. "name": name,
  159. }).Info("Restore session?")
  160. if strings.HasSuffix(name, ".md") {
  161. name = name[0 : len(name)-3]
  162. }
  163. path := fmt.Sprintf("slides/%s.md", name)
  164. session := sessions.Default(c)
  165. session.Set("name", path)
  166. session.Save()
  167. c.HTML(200, "index.tmpl", gin.H{
  168. "pubTo": path,
  169. })
  170. })
  171. r.GET("/published/slides/:name", func(c *gin.Context) {
  172. name := c.Param("name")
  173. log.WithFields(log.Fields{
  174. "name": name,
  175. }).Info("Published")
  176. if strings.HasSuffix(name, ".md") {
  177. name = name[0 : len(name)-3]
  178. }
  179. path := fmt.Sprintf("slides/%s.md", name)
  180. session := sessions.Default(c)
  181. session.Set("name", path)
  182. session.Save()
  183. c.HTML(200, "slides.tmpl", gin.H{
  184. "pubTo": path,
  185. })
  186. })
  187. return r
  188. }
  189. func main() {
  190. r := NewApp()
  191. port := "8080"
  192. if len(os.Args) > 1 {
  193. port = os.Args[1]
  194. } else {
  195. envPort := os.Getenv("PORT")
  196. if len(envPort) > 0 {
  197. port = envPort
  198. }
  199. }
  200. log.Info("Started http://0.0.0.0:8080")
  201. r.Run(fmt.Sprintf(":%s", port))
  202. }