2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"} {
|
|
|
|
files, err := ioutil.ReadDir("./" + dir)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read directory ./%s: %v", dir, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
sl := string(os.PathSeparator)
|
|
|
|
path := sl + dir + sl + file.Name()
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile("." + path)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read file %s: %v", path, err)
|
|
|
|
return err
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-03 19:12:01 +08:00
|
|
|
if dir == "js" {
|
2018-06-03 19:57:42 +08:00
|
|
|
asset[path] = "window.origin='" + os.Getenv("ORIGIN") + "';" + string(contents)
|
2018-06-03 19:12:01 +08:00
|
|
|
} else {
|
|
|
|
asset[path] = string(contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("routing %s", path)
|
|
|
|
router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprint(w, asset[r.URL.Path])
|
|
|
|
})
|
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pages := []string{
|
|
|
|
"login",
|
|
|
|
"signup",
|
|
|
|
"dashboard",
|
|
|
|
"account",
|
|
|
|
}
|
|
|
|
|
|
|
|
html := make(map[string]string)
|
|
|
|
for _, page := range pages {
|
|
|
|
contents, err := ioutil.ReadFile(page + ".html")
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read file %s.html: %v", page, err)
|
|
|
|
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-03 19:12:37 +08:00
|
|
|
logger.Errorf("cannot parse /%s template: %v", page, 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-03 19:12:37 +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
|
|
|
|
}
|