2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
2018-06-11 01:43:42 +08:00
|
|
|
"mime"
|
2018-05-27 22:40:42 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-06-03 22:49:36 +08:00
|
|
|
"path"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func redirectLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, "/login", 301)
|
|
|
|
}
|
|
|
|
|
2018-06-03 19:12:01 +08:00
|
|
|
type staticAssetPlugs struct {
|
|
|
|
Origin string
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
type staticHtmlPlugs struct {
|
|
|
|
CdnPrefix string
|
2018-06-22 21:57:19 +08:00
|
|
|
Footer template.HTML
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func initStaticRouter(router *mux.Router) error {
|
2018-06-11 17:31:58 +08:00
|
|
|
asset := make(map[string][]byte)
|
|
|
|
gzippedAsset := make(map[string][]byte)
|
2018-06-03 19:12:01 +08:00
|
|
|
|
|
|
|
for _, dir := range []string{"js", "css", "images"} {
|
2018-06-09 16:27:47 +08:00
|
|
|
sl := string(os.PathSeparator)
|
|
|
|
dir = sl + dir
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(os.Getenv("STATIC") + dir)
|
2018-06-03 19:12:01 +08:00
|
|
|
if err != nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
logger.Errorf("cannot read directory %s%s: %v", os.Getenv("STATIC"), dir, err)
|
2018-06-03 19:12:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2018-06-09 16:27:47 +08:00
|
|
|
p := dir + sl + file.Name()
|
2018-06-03 19:12:01 +08:00
|
|
|
|
2018-06-09 16:27:47 +08:00
|
|
|
contents, err := ioutil.ReadFile(os.Getenv("STATIC") + p)
|
2018-06-03 19:12:01 +08:00
|
|
|
if err != nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
logger.Errorf("cannot read file %s%s: %v", os.Getenv("STATIC"), p, err)
|
2018-06-03 19:12:01 +08:00
|
|
|
return err
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:48:30 +08:00
|
|
|
prefix := ""
|
2018-06-09 16:27:47 +08:00
|
|
|
if dir == "/js" {
|
2018-06-16 21:25:27 +08:00
|
|
|
prefix = "window.commentoOrigin='" + os.Getenv("ORIGIN") + "';\n"
|
|
|
|
prefix += "window.commentoCdn='" + os.Getenv("CDN_PREFIX") + "';\n"
|
2018-06-03 19:12:01 +08:00
|
|
|
}
|
|
|
|
|
2018-06-11 17:31:58 +08:00
|
|
|
gzip := (os.Getenv("GZIP_STATIC") == "true")
|
2018-06-03 22:48:30 +08:00
|
|
|
|
2018-06-11 17:31:58 +08:00
|
|
|
asset[p] = []byte(prefix + string(contents))
|
|
|
|
if gzip {
|
|
|
|
gzippedAsset[p], err = gzipStatic(asset[p])
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error gzipping %s: %v", p, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// faster than checking inside the handler
|
|
|
|
if !gzip {
|
|
|
|
router.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path)))
|
|
|
|
w.Write(asset[r.URL.Path])
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
router.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path)))
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
w.Write(gzippedAsset[r.URL.Path])
|
|
|
|
})
|
|
|
|
}
|
2018-06-03 19:12:01 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 21:57:19 +08:00
|
|
|
footer, err := ioutil.ReadFile(os.Getenv("STATIC") + string(os.PathSeparator) + "footer.html")
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read file footer.html: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
pages := []string{
|
|
|
|
"login",
|
2018-06-09 18:07:53 +08:00
|
|
|
"forgot",
|
|
|
|
"reset-password",
|
2018-05-27 22:40:42 +08:00
|
|
|
"signup",
|
|
|
|
"dashboard",
|
2018-06-05 16:53:32 +08:00
|
|
|
"logout",
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
html := make(map[string]string)
|
|
|
|
for _, page := range pages {
|
2018-06-09 16:27:47 +08:00
|
|
|
sl := string(os.PathSeparator)
|
|
|
|
page = sl + page
|
|
|
|
file := page + ".html"
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(os.Getenv("STATIC") + file)
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
logger.Errorf("cannot read file %s%s: %v", os.Getenv("STATIC"), file, err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-03 16:33:45 +08:00
|
|
|
t, err := template.New(page).Delims("<<<", ">>>").Parse(string(contents))
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-06-09 16:27:47 +08:00
|
|
|
logger.Errorf("cannot parse %s%s template: %v", os.Getenv("STATIC"), file, err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2018-06-22 21:57:19 +08:00
|
|
|
t.Execute(&buf, &staticHtmlPlugs{CdnPrefix: os.Getenv("CDN_PREFIX"), Footer: template.HTML(string(footer))})
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-06-09 16:27:47 +08:00
|
|
|
html[page] = buf.String()
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, page := range pages {
|
2018-06-11 01:43:42 +08:00
|
|
|
router.HandleFunc("/"+page, func(w http.ResponseWriter, r *http.Request) {
|
2018-06-03 19:12:37 +08:00
|
|
|
fmt.Fprint(w, html[r.URL.Path])
|
2018-05-27 22:40:42 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-03 19:39:49 +08:00
|
|
|
router.HandleFunc("/", redirectLogin)
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
return nil
|
|
|
|
}
|