Clean up static content routing

This commit is contained in:
Ian Kent 2014-04-27 21:23:40 +01:00
parent 92ff3714ad
commit 034f683d93

View file

@ -3,6 +3,7 @@ package http
import ( import (
"net/http" "net/http"
"strings" "strings"
"log"
"github.com/ian-kent/MailHog/mailhog/config" "github.com/ian-kent/MailHog/mailhog/config"
"github.com/ian-kent/MailHog/mailhog/http/api" "github.com/ian-kent/MailHog/mailhog/http/api"
"github.com/ian-kent/MailHog/mailhog/http/router" "github.com/ian-kent/MailHog/mailhog/http/router"
@ -25,27 +26,27 @@ func web_index(w http.ResponseWriter, r *http.Request, route *router.Route) {
w.Write([]byte(web_render(string(data)))) w.Write([]byte(web_render(string(data))))
} }
func web_jscontroller(w http.ResponseWriter, r *http.Request, route *router.Route) { func web_static(w http.ResponseWriter, r *http.Request, route *router.Route) {
w.Header().Set("Content-Type", "text/javascript") match := route.Pattern.FindStringSubmatch(r.URL.Path)
data, _ := cfg.Assets("assets/js/controllers.js") file := match[1]
w.Write(data) log.Printf("[HTTP] GET %s\n", file)
}
func web_imgcontroller(w http.ResponseWriter, r *http.Request, route *router.Route) { if strings.HasSuffix(file, ".gif") {
w.Header().Set("Content-Type", "image/png") w.Header().Set("Content-Type", "image/gif")
data, _ := cfg.Assets("assets/images/hog.png") } else if strings.HasSuffix(file, ".png") {
w.Write(data) w.Header().Set("Content-Type", "image/png")
} } else if strings.HasSuffix(file, ".js") {
w.Header().Set("Content-Type", "text/javascript")
} else {
w.Header().Set("Content-Type", "text/plain")
}
func web_img_github(w http.ResponseWriter, r *http.Request, route *router.Route) { data, err := cfg.Assets("assets" + file)
w.Header().Set("Content-Type", "image/png") if err != nil {
data, _ := cfg.Assets("assets/images/github.png") w.WriteHeader(404)
w.Write(data) return
} }
func web_img_ajaxloader(w http.ResponseWriter, r *http.Request, route *router.Route) {
w.Header().Set("Content-Type", "image/gif")
data, _ := cfg.Assets("assets/images/ajax-loader.gif")
w.Write(data) w.Write(data)
} }
@ -73,10 +74,10 @@ func Start(exitCh chan int, conf *config.Config) {
} }
r.Get("^/exit/?$", web_exit) r.Get("^/exit/?$", web_exit)
r.Get("^/js/controllers.js$", web_jscontroller) r.Get("^(/js/controllers.js)$", web_static)
r.Get("^/images/hog.png$", web_imgcontroller) r.Get("^(/images/hog.png)$", web_static)
r.Get("^/images/github.png$", web_img_github) r.Get("^(/images/github.png)$", web_static)
r.Get("^/images/ajax-loader.gif$", web_img_ajaxloader) r.Get("^(/images/ajax-loader.gif)$", web_static)
r.Get("^/$", web_index) r.Get("^/$", web_index)
api.CreateAPIv1(exitCh, conf, server) api.CreateAPIv1(exitCh, conf, server)