2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"io/ioutil"
|
2018-12-25 10:49:53 +08:00
|
|
|
"mime"
|
2018-12-29 01:14:11 +08:00
|
|
|
"net/http"
|
2018-12-25 10:49:53 +08:00
|
|
|
"os"
|
2018-12-29 01:14:11 +08:00
|
|
|
"path"
|
|
|
|
"strings"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func redirectLogin(w http.ResponseWriter, r *http.Request) {
|
2018-08-08 14:46:27 +08:00
|
|
|
http.Redirect(w, r, os.Getenv("ORIGIN")+"/login", 301)
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-25 10:49:53 +08:00
|
|
|
type staticPlugs struct {
|
2018-08-08 14:46:27 +08:00
|
|
|
Origin string
|
2018-05-27 22:40:42 +08:00
|
|
|
CdnPrefix string
|
2018-12-25 10:49:53 +08:00
|
|
|
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)
|
2019-04-21 08:59:30 +08:00
|
|
|
x = strings.Replace(x, "[[[.Version]]]", version, -1)
|
2018-12-25 10:49:53 +08:00
|
|
|
|
|
|
|
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)
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 14:46:30 +08:00
|
|
|
func staticRouterInit(router *mux.Router) error {
|
2018-12-25 10:49:53 +08:00
|
|
|
var err error
|
2018-09-26 15:18:36 +08:00
|
|
|
|
2018-12-25 10:49:53 +08:00
|
|
|
subdir := pathStrip(os.Getenv("ORIGIN"))
|
2018-06-03 19:12:01 +08:00
|
|
|
|
2018-12-25 10:49:53 +08:00
|
|
|
if err = footerInit(); err != nil {
|
|
|
|
logger.Errorf("error initialising static router: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2018-06-09 16:27:47 +08:00
|
|
|
|
2019-02-19 06:33:11 +08:00
|
|
|
for _, dir := range []string{"/js", "/css", "/images", "/fonts"} {
|
2018-06-09 16:27:47 +08:00
|
|
|
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-12-25 10:49:53 +08:00
|
|
|
f := dir + "/" + file.Name()
|
|
|
|
asset[subdir+f], err = fileLoad(os.Getenv("STATIC") + f)
|
2018-06-03 19:12:01 +08:00
|
|
|
if err != nil {
|
2018-12-25 10:49:53 +08:00
|
|
|
logger.Errorf("cannot detemplate %s%s: %v", os.Getenv("STATIC"), f, err)
|
2018-06-03 19:12:01 +08:00
|
|
|
return err
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
2018-06-03 19:12:01 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pages := []string{
|
2018-12-25 10:49:53 +08:00
|
|
|
"/login",
|
|
|
|
"/forgot",
|
2019-06-06 16:27:42 +08:00
|
|
|
"/reset",
|
2018-12-25 10:49:53 +08:00
|
|
|
"/signup",
|
|
|
|
"/confirm-email",
|
2019-02-19 05:30:54 +08:00
|
|
|
"/unsubscribe",
|
2018-12-25 10:49:53 +08:00
|
|
|
"/dashboard",
|
|
|
|
"/logout",
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:18:36 +08:00
|
|
|
for _, page := range pages {
|
2018-12-25 10:49:53 +08:00
|
|
|
f := page + ".html"
|
|
|
|
asset[subdir+page], err = fileLoad(os.Getenv("STATIC") + f)
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-12-25 10:49:53 +08:00
|
|
|
logger.Errorf("cannot detemplate %s%s: %v", os.Getenv("STATIC"), f, err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
2018-12-25 10:49:53 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-12-29 01:14:11 +08:00
|
|
|
for p, _ := range asset {
|
2018-12-25 10:49:53 +08:00
|
|
|
if path.Ext(p) != "" {
|
|
|
|
contentType[p] = mime.TypeByExtension(path.Ext(p))
|
|
|
|
} else {
|
2019-02-21 00:07:29 +08:00
|
|
|
contentType[p] = "text/html; charset=utf-8"
|
2018-09-26 15:18:36 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-12-25 10:49:53 +08:00
|
|
|
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])
|
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
|
|
|
|
}
|