2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-06-03 22:49:36 +08:00
|
|
|
"path"
|
|
|
|
"mime"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func initStaticRouter(router *mux.Router) error {
|
2018-06-03 19:12:01 +08:00
|
|
|
asset := make(map[string]string)
|
|
|
|
|
|
|
|
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-05 18:01:14 +08:00
|
|
|
prefix = "window.commento_origin='" + os.Getenv("ORIGIN") + "';\n"
|
|
|
|
prefix += "window.commento_cdn='" + os.Getenv("CDN_PREFIX") + "';\n"
|
2018-06-03 19:12:01 +08:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:48:30 +08:00
|
|
|
asset[p] = prefix + string(contents);
|
|
|
|
|
2018-06-03 22:49:36 +08:00
|
|
|
router.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path)))
|
2018-06-03 19:12:01 +08:00
|
|
|
fmt.Fprint(w, asset[r.URL.Path])
|
|
|
|
})
|
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pages := []string{
|
|
|
|
"login",
|
|
|
|
"signup",
|
|
|
|
"dashboard",
|
2018-06-05 16:53:32 +08:00
|
|
|
"logout",
|
2018-05-27 22:40:42 +08:00
|
|
|
"account",
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
t.Execute(&buf, &staticHtmlPlugs{CdnPrefix: os.Getenv("CDN_PREFIX")})
|
|
|
|
|
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-03 19:12:37 +08:00
|
|
|
router.HandleFunc("/" + page, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|