-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_static.go
137 lines (114 loc) · 2.89 KB
/
router_static.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
package main
import (
"github.com/gorilla/mux"
"io/ioutil"
"mime"
"net/http"
"os"
"path"
"strings"
)
func redirectLogin(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, os.Getenv("ORIGIN")+"/login", 301)
}
type staticPlugs struct {
Origin string
CdnPrefix string
Footer string
}
var asset map[string][]byte = make(map[string][]byte)
var contentType map[string]string = make(map[string]string)
var footer string
var compress bool
func fileDetemplate(f string) ([]byte, error) {
contents, err := ioutil.ReadFile(f)
if err != nil {
logger.Errorf("cannot read file %s: %v", f, err)
return []byte{}, err
}
x := string(contents)
x = strings.Replace(x, "[[[.Origin]]]", os.Getenv("ORIGIN"), -1)
x = strings.Replace(x, "[[[.CdnPrefix]]]", os.Getenv("CDN_PREFIX"), -1)
x = strings.Replace(x, "[[[.Footer]]]", footer, -1)
x = strings.Replace(x, "[[[.Version]]]", version, -1)
return []byte(x), nil
}
func footerInit() error {
contents, err := fileDetemplate(os.Getenv("STATIC") + "/footer.html")
if err != nil {
logger.Errorf("cannot init footer: %v", err)
return err
}
footer = string(contents)
return nil
}
func fileLoad(f string) ([]byte, error) {
b, err := fileDetemplate(f)
if err != nil {
logger.Errorf("cannot load file %s: %v", f, err)
return []byte{}, err
}
if !compress {
return b, nil
}
return gzipStatic(b)
}
func staticRouterInit(router *mux.Router) error {
var err error
subdir := pathStrip(os.Getenv("ORIGIN"))
if err = footerInit(); err != nil {
logger.Errorf("error initialising static router: %v", err)
return err
}
for _, dir := range []string{"/js", "/css", "/images", "/fonts"} {
files, err := ioutil.ReadDir(os.Getenv("STATIC") + dir)
if err != nil {
logger.Errorf("cannot read directory %s%s: %v", os.Getenv("STATIC"), dir, err)
return err
}
for _, file := range files {
f := dir + "/" + file.Name()
asset[subdir+f], err = fileLoad(os.Getenv("STATIC") + f)
if err != nil {
logger.Errorf("cannot detemplate %s%s: %v", os.Getenv("STATIC"), f, err)
return err
}
}
}
pages := []string{
"/login",
"/forgot",
"/reset",
"/signup",
"/confirm-email",
"/unsubscribe",
"/dashboard",
"/settings",
"/logout",
"/profile",
}
for _, page := range pages {
f := page + ".html"
asset[subdir+page], err = fileLoad(os.Getenv("STATIC") + f)
if err != nil {
logger.Errorf("cannot detemplate %s%s: %v", os.Getenv("STATIC"), f, err)
return err
}
}
for p, _ := range asset {
if path.Ext(p) != "" {
contentType[p] = mime.TypeByExtension(path.Ext(p))
} else {
contentType[p] = "text/html; charset=utf-8"
}
router.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", contentType[r.URL.Path])
if compress {
w.Header().Set("Content-Encoding", "gzip")
}
w.Write(asset[r.URL.Path])
})
}
router.HandleFunc("/", redirectLogin)
return nil
}