commento/api/router_static.go

135 lines
2.8 KiB
Go
Raw Normal View History

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"
)
func redirectLogin(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, os.Getenv("ORIGIN")+"/login", 301)
}
2018-12-25 10:49:53 +08:00
type staticPlugs struct {
Origin string
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)
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-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-12-25 10:49:53 +08:00
subdir := pathStrip(os.Getenv("ORIGIN"))
2018-12-25 10:49:53 +08:00
if err = footerInit(); err != nil {
logger.Errorf("error initialising static router: %v", err)
return err
}
2019-02-19 06:33:11 +08:00
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 {
2018-12-25 10:49:53 +08:00
f := dir + "/" + file.Name()
asset[subdir+f], err = fileLoad(os.Getenv("STATIC") + f)
if err != nil {
2018-12-25 10:49:53 +08:00
logger.Errorf("cannot detemplate %s%s: %v", os.Getenv("STATIC"), f, err)
return err
}
}
}
pages := []string{
2018-12-25 10:49:53 +08:00
"/login",
"/forgot",
"/reset-password",
"/signup",
"/confirm-email",
2019-02-19 05:30:54 +08:00
"/unsubscribe",
2018-12-25 10:49:53 +08:00
"/dashboard",
"/logout",
}
for _, page := range pages {
2018-12-25 10:49:53 +08:00
f := page + ".html"
asset[subdir+page], err = fileLoad(os.Getenv("STATIC") + f)
if err != nil {
2018-12-25 10:49:53 +08:00
logger.Errorf("cannot detemplate %s%s: %v", os.Getenv("STATIC"), f, err)
return err
}
2018-12-25 10:49:53 +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 {
contentType[p] = mime.TypeByExtension("html")
}
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-06-03 19:39:49 +08:00
router.HandleFunc("/", redirectLogin)
return nil
}