main.go 4.9 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. isNew := c.Query("new")
  67. latest := files.LatestFileIn("slides")
  68. log.WithFields(log.Fields{
  69. "name": latest,
  70. "isNew": isNew,
  71. }).Info("Restoring latest point")
  72. var path, name string
  73. if latest == "" || isNew != "" {
  74. haikunator := haikunator.New()
  75. haikunator.TokenLength = 0
  76. name = haikunator.Haikunate()
  77. } else {
  78. name = strings.Replace(latest, ".md", "", 1)
  79. }
  80. path = fmt.Sprintf("slides/%s.md", name)
  81. log.WithFields(log.Fields{
  82. "path": path,
  83. }).Info("A new session")
  84. session := sessions.Default(c)
  85. session.Set("name", path)
  86. session.Save()
  87. c.Writer.Header().Set("Location", fmt.Sprintf("/stash/edit/%s", name))
  88. c.HTML(302, "index.tmpl", gin.H{
  89. "pubTo": path,
  90. })
  91. })
  92. mustHaveSession := func(c *gin.Context) (string, error) {
  93. session := sessions.Default(c)
  94. val := session.Get("name")
  95. emptySession := errors.New("Emtpy session")
  96. if val == nil {
  97. c.String(400, "No context")
  98. return "", emptySession
  99. }
  100. log.WithFields(log.Fields{
  101. "path": val,
  102. }).Info("Got session")
  103. path, ok := val.(string)
  104. if !ok {
  105. c.String(400, "No context")
  106. return "", emptySession
  107. }
  108. return path, nil
  109. }
  110. r.GET("/slides.md", func(c *gin.Context) {
  111. path, err := mustHaveSession(c)
  112. if err != nil {
  113. return
  114. }
  115. if _, err := os.Stat(path); err != nil {
  116. // copy sample markdown file to the path
  117. body, err := ioutil.ReadFile("initial-slides.md")
  118. if err != nil {
  119. panic(err)
  120. }
  121. ioutil.WriteFile(path, body, 0644)
  122. c.String(200, string(body))
  123. return
  124. }
  125. body, err := ioutil.ReadFile(path)
  126. if err != nil {
  127. panic(err)
  128. }
  129. c.String(200, string(body))
  130. })
  131. r.PUT("/slides.md", func(c *gin.Context) {
  132. path, err := mustHaveSession(c)
  133. if err != nil {
  134. return
  135. }
  136. body, _ := ioutil.ReadAll(c.Request.Body)
  137. ioutil.WriteFile(path, body, 0644)
  138. log.WithFields(log.Fields{
  139. "size": len(body),
  140. "file": path,
  141. }).Info("Wrote to file")
  142. c.String(200, "")
  143. })
  144. r.GET("/stash", func(c *gin.Context) {
  145. files, err := ioutil.ReadDir("slides")
  146. if err != nil {
  147. log.Fatal(err)
  148. }
  149. var stash []string
  150. for _, file := range files {
  151. stash = append(stash, file.Name())
  152. }
  153. c.HTML(200, "stash.tmpl", gin.H{
  154. "stash": stash,
  155. })
  156. })
  157. r.GET("/stash/edit/:name", func(c *gin.Context) {
  158. name := c.Param("name")
  159. log.WithFields(log.Fields{
  160. "name": name,
  161. }).Info("Restore session?")
  162. if strings.HasSuffix(name, ".md") {
  163. name = name[0 : len(name)-3]
  164. }
  165. path := fmt.Sprintf("slides/%s.md", name)
  166. session := sessions.Default(c)
  167. session.Set("name", path)
  168. session.Save()
  169. c.HTML(200, "index.tmpl", gin.H{
  170. "pubTo": path,
  171. })
  172. })
  173. r.GET("/published/slides/:name", func(c *gin.Context) {
  174. name := c.Param("name")
  175. log.WithFields(log.Fields{
  176. "name": name,
  177. }).Info("Published")
  178. if strings.HasSuffix(name, ".md") {
  179. name = name[0 : len(name)-3]
  180. }
  181. path := fmt.Sprintf("slides/%s.md", name)
  182. session := sessions.Default(c)
  183. session.Set("name", path)
  184. session.Save()
  185. c.HTML(200, "slides.tmpl", gin.H{
  186. "pubTo": path,
  187. })
  188. })
  189. return r
  190. }
  191. func main() {
  192. r := NewApp()
  193. port := "8080"
  194. if len(os.Args) > 1 {
  195. port = os.Args[1]
  196. } else {
  197. envPort := os.Getenv("PORT")
  198. if len(envPort) > 0 {
  199. port = envPort
  200. }
  201. }
  202. log.Info("Started http://0.0.0.0:8080")
  203. r.Run(fmt.Sprintf(":%s", port))
  204. }