MailHog/mailhog/http/server.go

87 lines
2.1 KiB
Go
Raw Normal View History

2014-04-20 14:35:59 +00:00
package http
import (
2014-04-20 18:49:05 +00:00
"net/http"
2014-04-27 15:06:58 +00:00
"strings"
2014-04-27 20:23:40 +00:00
"log"
2014-04-23 23:22:50 +00:00
"github.com/ian-kent/MailHog/mailhog/config"
2014-04-20 18:49:05 +00:00
"github.com/ian-kent/MailHog/mailhog/http/api"
2014-04-27 20:13:35 +00:00
"github.com/ian-kent/MailHog/mailhog/http/router"
2014-04-20 14:35:59 +00:00
)
var exitChannel chan int
2014-04-23 23:22:50 +00:00
var cfg *config.Config
2014-04-20 14:35:59 +00:00
2014-04-27 18:13:22 +00:00
// TODO clean this mess up
2014-04-27 20:13:35 +00:00
func web_exit(w http.ResponseWriter, r *http.Request, route *router.Route) {
2014-04-20 14:35:59 +00:00
web_headers(w)
2014-04-20 15:05:50 +00:00
w.Write([]byte("Exiting MailHog!"))
2014-04-20 14:35:59 +00:00
exitChannel <- 1
}
2014-04-27 20:13:35 +00:00
func web_index(w http.ResponseWriter, r *http.Request, route *router.Route) {
2014-04-20 14:35:59 +00:00
web_headers(w)
2014-04-27 15:06:58 +00:00
data, _ := cfg.Assets("assets/templates/index.html")
w.Write([]byte(web_render(string(data))))
2014-04-20 14:35:59 +00:00
}
2014-04-27 20:23:40 +00:00
func web_static(w http.ResponseWriter, r *http.Request, route *router.Route) {
match := route.Pattern.FindStringSubmatch(r.URL.Path)
file := match[1]
log.Printf("[HTTP] GET %s\n", file)
if strings.HasSuffix(file, ".gif") {
w.Header().Set("Content-Type", "image/gif")
} else if strings.HasSuffix(file, ".png") {
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")
}
2014-04-27 18:13:22 +00:00
2014-04-27 20:23:40 +00:00
data, err := cfg.Assets("assets" + file)
if err != nil {
w.WriteHeader(404)
return
}
2014-04-27 17:19:30 +00:00
w.Write(data)
}
2014-04-20 14:35:59 +00:00
func web_render(content string) string {
2014-04-27 15:06:58 +00:00
data, _ := cfg.Assets("assets/templates/layout.html")
layout := string(data)
html := strings.Replace(layout, "<%= content %>", content, -1)
2014-04-27 18:26:21 +00:00
// TODO clean this up
html = strings.Replace(html, "<%= config[Hostname] %>", cfg.Hostname, -1)
2014-04-27 15:06:58 +00:00
return html
2014-04-20 14:35:59 +00:00
}
func web_headers(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html")
}
2014-04-23 23:22:50 +00:00
func Start(exitCh chan int, conf *config.Config) {
2014-04-20 14:35:59 +00:00
exitChannel = exitCh
2014-04-23 23:22:50 +00:00
cfg = conf
2014-04-20 15:05:50 +00:00
2014-04-27 20:13:35 +00:00
r := &router.Router{}
2014-04-20 18:49:05 +00:00
server := &http.Server{
Addr: conf.HTTPBindAddr,
2014-04-27 20:13:35 +00:00
Handler: r,
2014-04-20 18:49:05 +00:00
}
2014-04-27 20:13:35 +00:00
r.Get("^/exit/?$", web_exit)
2014-04-27 20:23:40 +00:00
r.Get("^(/js/controllers.js)$", web_static)
r.Get("^(/images/hog.png)$", web_static)
r.Get("^(/images/github.png)$", web_static)
r.Get("^(/images/ajax-loader.gif)$", web_static)
2014-04-27 20:13:35 +00:00
r.Get("^/$", web_index)
2014-04-20 18:49:05 +00:00
2014-04-23 23:22:50 +00:00
api.CreateAPIv1(exitCh, conf, server)
2014-04-20 18:49:05 +00:00
server.ListenAndServe()
2014-04-20 14:35:59 +00:00
}