router_static.go: prefix JS with Origin
This commit is contained in:
parent
01d1b1dbdf
commit
03f075e75a
@ -14,21 +14,46 @@ func redirectLogin(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/login", 301)
|
||||
}
|
||||
|
||||
type staticAssetPlugs struct {
|
||||
Origin string
|
||||
}
|
||||
|
||||
type staticHtmlPlugs struct {
|
||||
CdnPrefix string
|
||||
}
|
||||
|
||||
func initStaticRouter(router *mux.Router) error {
|
||||
for _, path := range []string{"js", "css", "images"} {
|
||||
router.PathPrefix("/" + path + "/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
f, err := os.Stat("." + r.URL.Path)
|
||||
if err != nil || f.IsDir() {
|
||||
http.NotFound(w, r)
|
||||
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
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, "."+r.URL.Path)
|
||||
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
|
||||
}
|
||||
|
||||
if dir == "js" {
|
||||
asset[path] = "window.API='" + os.Getenv("ORIGIN") + "/api';" + string(contents)
|
||||
} 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])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pages := []string{
|
||||
"login",
|
||||
|
Loading…
Reference in New Issue
Block a user