router_static.go: set Content-Type header
This commit is contained in:
parent
7e9713b35e
commit
efb23778b8
@ -8,6 +8,8 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"mime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func redirectLogin(w http.ResponseWriter, r *http.Request) {
|
func redirectLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -34,11 +36,11 @@ func initStaticRouter(router *mux.Router) error {
|
|||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
sl := string(os.PathSeparator)
|
sl := string(os.PathSeparator)
|
||||||
path := sl + dir + sl + file.Name()
|
p := sl + dir + sl + file.Name()
|
||||||
|
|
||||||
contents, err := ioutil.ReadFile("." + path)
|
contents, err := ioutil.ReadFile("." + p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("cannot read file %s: %v", path, err)
|
logger.Errorf("cannot read file %s: %v", p, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +51,8 @@ func initStaticRouter(router *mux.Router) error {
|
|||||||
|
|
||||||
asset[p] = prefix + string(contents);
|
asset[p] = prefix + string(contents);
|
||||||
|
|
||||||
router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
router.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path)))
|
||||||
fmt.Fprint(w, asset[r.URL.Path])
|
fmt.Fprint(w, asset[r.URL.Path])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,10 @@ func stripDomain(domain string) string {
|
|||||||
return noProtocol
|
return noProtocol
|
||||||
}
|
}
|
||||||
|
|
||||||
var path = regexp.MustCompile(`(https?://[^/]*)`)
|
var pathMatch = regexp.MustCompile(`(https?://[^/]*)`)
|
||||||
|
|
||||||
func stripPath(url string) string {
|
func stripPath(url string) string {
|
||||||
strippedPath := path.ReplaceAllString(url, ``)
|
strippedPath := pathMatch.ReplaceAllString(url, ``)
|
||||||
|
|
||||||
return strippedPath
|
return strippedPath
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user