-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
78 lines (66 loc) · 1.76 KB
/
utils.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
/*
Generic utility functions for GoPics.
Copyright (c) 2015, Luca Chiricozzi. All rights reserved.
Released under the MIT License.
http://opensource.org/licenses/MIT
*/
package main
import (
"html/template"
"net/http"
"path/filepath"
"time"
"github.com/lucachr/gopics/flash"
)
// renderTemplate executes a template with the data contained in the given
// page.
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) *appError {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
return &appError{
Err: err,
Code: http.StatusInternalServerError,
}
}
return nil
}
// httpAppError send an error reponse to the user if
// is not nil.
func httpAppError(w http.ResponseWriter, ae *appError) {
if ae != nil {
http.Error(w, ae.Error(), ae.Code)
}
}
// unixTimeNow returns the current Unix time.
func unixTimeNow() int64 {
return time.Now().Unix()
}
// buildFilePath builds the absolute path to file, given the directory
// tree as a slice of strings.
func buildFilePath(path []string, file string) string {
return filepath.Join(append(path, file)...)
}
// buildTemplates parses the given templates.
func buildTemplates(filenames ...string) *template.Template {
// Build a slice of paths
fs := []string{}
for _, fn := range filenames {
f := buildFilePath(templatesPath, fn)
fs = append(fs, f)
}
return template.Must(template.ParseFiles(fs...))
}
// setFlashAndRedirect sets a flash cookie with a value of msg and redirects
// the user to the given URL.
func setFlashAndRedirect(w http.ResponseWriter, r *http.Request,
url, msg string) *appError {
err := flash.SetCookie(w, msg)
if err != nil {
return &appError{
Err: err,
Code: http.StatusInternalServerError,
}
}
http.Redirect(w, r, url, http.StatusSeeOther)
return nil
}