router_static.go: set Content-Type header

This commit is contained in:
Adhityaa 2018-06-03 20:19:36 +05:30
parent 7e9713b35e
commit efb23778b8
2 changed files with 9 additions and 6 deletions

View File

@ -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])
}) })
} }

View File

@ -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
} }