1
1
package main
2
2
3
3
import (
4
- "encoding/base64"
5
4
"errors"
6
5
"fmt"
7
6
"io/ioutil"
8
7
"os"
9
- "strconv"
10
8
"strings"
11
9
12
10
log "github.com/Sirupsen/logrus"
13
11
haikunator "github.com/atrox/haikunatorgo"
14
12
"github.com/gin-contrib/sessions"
15
13
"github.com/gin-gonic/gin"
14
+ "github.com/msoedov/hacker-slides/auth"
15
+ "github.com/msoedov/hacker-slides/files"
16
16
)
17
17
18
18
const sessionHeader = "slide-session"
19
19
20
- func Header (c * gin.Context , key string ) string {
21
- if values , _ := c .Request .Header [key ]; len (values ) > 0 {
22
- return values [0 ]
23
- }
24
- return ""
25
- }
26
-
27
- func BasicAuth () gin.HandlerFunc {
28
- realm := "Authorization Required"
29
- realm = "Basic realm=" + strconv .Quote (realm )
30
- user := os .Getenv ("USER" )
31
- password := os .Getenv ("PASSWORD" )
32
- enabled := isEnabled (user , password )
33
- if enabled {
34
- log .Warn ("Auth mode enabled" )
35
- log .Warn (fmt .Sprintf ("Visit http://%s:%s@0.0.0.0:8080" , user , password ))
36
- }
37
- return func (c * gin.Context ) {
38
- header := Header (c , "Authorization" )
39
- if enabled && header != authorizationHeader (user , password ) {
40
- // Credentials doesn't match, we return 401 and abort handlers chain.
41
- c .Header ("WWW-Authenticate" , realm )
42
- c .AbortWithStatus (401 )
43
- return
44
- }
45
- c .Next ()
46
- }
47
- }
48
-
49
- func isEnabled (user , password string ) bool {
50
- switch {
51
- case user == "" :
52
- return false
53
- case password == "" :
54
- return false
55
- default :
56
- return true
57
- }
58
- }
59
-
60
- func authorizationHeader (user , password string ) string {
61
- base := user + ":" + password
62
- return "Basic " + base64 .StdEncoding .EncodeToString ([]byte (base ))
20
+ func SlidePath (name string ) string {
21
+ return fmt .Sprintf ("slides/%s.md" , name )
63
22
}
64
23
65
24
func NewApp () * gin.Engine {
@@ -68,30 +27,38 @@ func NewApp() *gin.Engine {
68
27
69
28
store := sessions .NewCookieStore ([]byte ("secret" ))
70
29
r .Use (sessions .Sessions (sessionHeader , store ))
71
- r .Use (BasicAuth ())
30
+ r .Use (auth . BasicAuth ())
72
31
73
32
r .LoadHTMLGlob ("templates/*.tmpl" )
74
33
r .Static ("/static" , "./static" )
75
34
76
35
r .GET ("/" , func (c * gin.Context ) {
77
-
78
- fname := c . Param ( "name " )
36
+ isNew := c . Query ( "new" )
37
+ latest := files . LatestFileIn ( "slides " )
79
38
log .WithFields (log.Fields {
80
- "name" : fname ,
81
- }).Info ("Restore?" )
39
+ "name" : latest ,
40
+ "isNew" : isNew ,
41
+ }).Info ("Restoring latest point" )
42
+
43
+ var path , name string
44
+ if latest == "" || isNew != "" {
45
+ haikunator := haikunator .New ()
46
+ haikunator .TokenLength = 0
47
+ name = haikunator .Haikunate ()
48
+ } else {
49
+ name = strings .Replace (latest , ".md" , "" , 1 )
50
+ }
51
+ path = SlidePath (name )
82
52
83
- haikunator := haikunator .New ()
84
- haikunator .TokenLength = 0
85
- name := haikunator .Haikunate ()
86
- path := fmt .Sprintf ("slides/%s.md" , name )
87
53
log .WithFields (log.Fields {
88
54
"path" : path ,
89
55
}).Info ("A new session" )
90
56
session := sessions .Default (c )
91
57
session .Set ("name" , path )
92
58
session .Save ()
93
59
94
- c .HTML (200 , "index.tmpl" , gin.H {
60
+ c .Writer .Header ().Set ("Location" , fmt .Sprintf ("/stash/edit/%s" , name ))
61
+ c .HTML (302 , "index.tmpl" , gin.H {
95
62
"pubTo" : path ,
96
63
})
97
64
})
@@ -176,7 +143,7 @@ func NewApp() *gin.Engine {
176
143
if strings .HasSuffix (name , ".md" ) {
177
144
name = name [0 : len (name )- 3 ]
178
145
}
179
- path := fmt . Sprintf ( "slides/%s.md" , name )
146
+ path := SlidePath ( name )
180
147
session := sessions .Default (c )
181
148
session .Set ("name" , path )
182
149
session .Save ()
@@ -196,7 +163,7 @@ func NewApp() *gin.Engine {
196
163
if strings .HasSuffix (name , ".md" ) {
197
164
name = name [0 : len (name )- 3 ]
198
165
}
199
- path := fmt . Sprintf ( "slides/%s.md" , name )
166
+ path := SlidePath ( name )
200
167
session := sessions .Default (c )
201
168
session .Set ("name" , path )
202
169
session .Save ()
0 commit comments