-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.go
66 lines (59 loc) · 1.22 KB
/
utilities.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
package main
import (
"encoding/base64"
"net/http"
"os"
"path/filepath"
"runtime/debug"
)
func base64Must(str string) []byte {
buffer, err := base64.StdEncoding.DecodeString(str)
if err != nil {
log.Fatal("One of the specified auth/crypt keys of your cookie store is invalid")
}
return buffer
}
func recoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
sendError(w, http.StatusInternalServerError)
}
}()
handler(w, r)
}
}
func assertFSDirPtr(dir *string) {
abs, err := filepath.Abs(*dir)
if err != nil {
log.Fatal(err.Error())
}
fi, err := os.Stat(abs)
if err != nil {
log.Fatal(err.Error())
}
if !fi.IsDir() {
log.Fatalf("%s isn't a directory", *dir)
}
*dir = abs
}
func assertFSDir(dir string) string {
assertFSDirPtr(&dir)
return dir
}
func assertFSFilePtr(file *string) {
abs, err := filepath.Abs(*file)
if err != nil {
log.Fatalf(err.Error())
}
_, err = os.Stat(abs)
if err != nil {
log.Fatal(err.Error())
}
*file = abs
}
func assertFSFile(file string) string {
assertFSFilePtr(&file)
return file
}