-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocooking.go
274 lines (245 loc) · 6.97 KB
/
gocooking.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Package cooking contains all the cooking server code.
package cooking
import (
"fmt"
"html"
"html/template"
"net/http"
"path"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
)
var (
// Handlers contains all known http handlers.
// This will be filled with init functions.
Handlers = make(map[string]HandleFunc)
// PublicHandlers are handler which will not have an authentication check.
// this will be filled with init functions.
PublicHandlers = make(map[string]HandleFunc)
)
var (
// EmptyUser is the empty user
EmptyUser = User{}
)
// HandleFunc defines a function which returns a http.HandleFunc
type HandleFunc = func(server *Server) http.HandlerFunc
// Config contains the configuration for the server.
type Config struct {
// Addr is defined as <bind>:<port>
Addr string `yaml:"addr"`
// Templates is the path to the templates folder.
Templates string `yaml:"templates"`
StoragePath string `yaml:"storage-path"`
StaticDir string `yaml:"static-dir"`
}
// User contains
type User struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
authenticated bool `yaml:"-"`
}
const (
// StatusClassSuccess should be returned if the message is positive.
StatusClassSuccess = "toast success"
// StatusClassWarning should be returned if the message is not critical.
StatusClassWarning = "toast warning"
// StatusClassCritical should be returned if the message is critical.
StatusClassCritical = "toast error"
)
// StatusMessage is a message to return to the requester via template.
type StatusMessage struct {
Message string
Class string
}
// HTML returns the html encoded status message.
func (message StatusMessage) HTML() template.HTML {
msg := ""
if message.Message != "" {
msg = fmt.Sprintf(`<div class=%q>%s</div>`, message.Class, html.EscapeString(message.Message))
}
return template.HTML(msg)
}
// Authenticate authenticate a user in
func (user *User) Authenticate(username, password string) bool {
if user.Username == username &&
bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) == nil {
return true
}
return false
}
// GetUser returns the user with
func (server *Server) GetUser(username string) User {
user, err := server.Storage.GetUser(username)
if err != nil {
logrus.Errorf("cannot get user from database: %s", err)
return EmptyUser
}
return user
}
// Server is the server.
type Server struct {
Config *Config
Templates *template.Template
Storage *Storage
SessionStore *sessions.CookieStore
done chan struct{}
}
// DefaultConfig returns a config struct with default values.
func DefaultConfig() *Config {
return &Config{
Addr: "localhost:8080",
Templates: "templates",
StoragePath: "test.db3",
StaticDir: "static",
}
}
// NewServer creates a new server with the given configuration.
func NewServer(config *Config) (*Server, error) {
server := &Server{
Config: config,
}
server.Storage = &Storage{
Path: server.Config.StoragePath,
}
err := server.Storage.Connect()
if err != nil {
return server, fmt.Errorf("cannot connect to storage: %s", err)
}
return server, nil
}
func (server *Server) loadTemplates() (err error) {
logrus.Infof("load templates")
globPattern := path.Join(server.Config.Templates, "*.tmpl")
templates, err := template.ParseGlob(globPattern)
if err == nil {
server.Templates = templates
}
return err
}
func (server *Server) watchTemplates() (err error) {
server.done = make(chan struct{})
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
logrus.Debugf("got event %s", event.String())
if strings.HasSuffix(event.Name, ".go.tmpl") {
err = server.loadTemplates()
if err != nil {
logrus.Errorf("cannot reload templates: %s", err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
logrus.Errorf("got error on watching templates: %s", err)
}
}
}()
logrus.Infof("begin watching %s", server.Config.Templates)
err = watcher.Add(server.Config.Templates)
if err != nil {
return err
}
<-server.done
return nil
}
// Serve creates a http socket and serves the server.
func (server *Server) Serve() error {
key, err := server.Storage.GetOrCreateSessionCookieKey()
if err != nil {
logrus.Errorf("Could not persist session key: %s. Using temporary one...", err)
}
server.SessionStore = sessions.NewCookieStore(key)
go server.watchTemplates()
err = server.loadTemplates()
if err != nil {
return err
}
mux := mux.NewRouter()
for pattern, handleFunc := range Handlers {
logrus.Infof("register %s", pattern)
mux.Handle(pattern, server.sessionHandler(handleFunc(server)))
}
for pattern, handleFunc := range PublicHandlers {
logrus.Infof("register %s", pattern)
mux.Handle(pattern, handleFunc(server))
}
err = http.ListenAndServe(server.Config.Addr, mux)
logrus.Infof("Got error %s, shutting down...", err)
server.done <- struct{}{}
return nil
}
func (server *Server) sessionHandler(handler http.HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
session, err := server.GetSession(writer, request)
if err != nil {
return
}
user, ok := session.Values["username"]
if ok {
logrus.Infof("user %s found", user)
if userData, ok := user.(string); ok {
if userData != "" {
logrus.Infof("user %v could be parsed", userData)
handler(writer, request)
return
}
}
}
http.Redirect(writer, request, "/login", http.StatusTemporaryRedirect)
}
}
// GetSession returns the user session object.
func (server *Server) GetSession(writer http.ResponseWriter, request *http.Request) (*sessions.Session, error) {
session, err := server.SessionStore.Get(request, "session-name")
if err != nil {
logrus.Errorf("cannot get session: %s", err)
err = session.Save(request, writer)
if err != nil {
logrus.Errorf("cannot save session: %s", err)
}
}
return session, nil
}
// GetMessage returns the message from the session if exists.
func (server *Server) GetMessage(writer http.ResponseWriter, request *http.Request) (message StatusMessage, err error) {
session, err := server.GetSession(writer, request)
if err != nil {
return
}
messages := session.Flashes()
if len(messages) > 0 {
message = messages[0].(StatusMessage)
}
return
}
// SetMessage sets the message for the next template call.
func (server *Server) SetMessage(writer http.ResponseWriter, request *http.Request, message string, messageClass string) error {
session, err := server.GetSession(writer, request)
if err != nil {
return err
}
session.AddFlash(StatusMessage{
Message: message,
Class: messageClass,
})
return nil
}
// HashPassword hashes a users password
func (server *Server) HashPassword(password string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(password), 5)
}