MailHog/mailhog/http/server.go

86 lines
2.7 KiB
Go
Raw Normal View History

2014-04-20 15:35:59 +01:00
package http
import (
2014-04-20 17:09:06 +01:00
"regexp"
2014-04-20 19:49:05 +01:00
"net/http"
2014-04-27 16:06:58 +01:00
"strings"
2014-04-24 00:22:50 +01:00
"github.com/ian-kent/MailHog/mailhog/config"
2014-04-20 19:49:05 +01:00
"github.com/ian-kent/MailHog/mailhog/http/api"
"github.com/ian-kent/MailHog/mailhog/http/handler"
2014-04-20 15:35:59 +01:00
)
var exitChannel chan int
2014-04-24 00:22:50 +01:00
var cfg *config.Config
2014-04-20 15:35:59 +01:00
2014-04-27 19:13:22 +01:00
// TODO clean this mess up
2014-04-20 19:49:05 +01:00
func web_exit(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 15:35:59 +01:00
web_headers(w)
2014-04-20 16:05:50 +01:00
w.Write([]byte("Exiting MailHog!"))
2014-04-20 15:35:59 +01:00
exitChannel <- 1
}
2014-04-20 19:49:05 +01:00
func web_index(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 15:35:59 +01:00
web_headers(w)
2014-04-27 16:06:58 +01:00
data, _ := cfg.Assets("assets/templates/index.html")
w.Write([]byte(web_render(string(data))))
2014-04-20 15:35:59 +01:00
}
2014-04-20 19:49:05 +01:00
func web_jscontroller(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 15:35:59 +01:00
w.Header().Set("Content-Type", "text/javascript")
2014-04-27 16:06:58 +01:00
data, _ := cfg.Assets("assets/js/controllers.js")
w.Write(data)
2014-04-20 15:35:59 +01:00
}
2014-04-20 19:49:05 +01:00
func web_imgcontroller(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 15:35:59 +01:00
w.Header().Set("Content-Type", "image/png")
2014-04-27 16:06:58 +01:00
data, _ := cfg.Assets("assets/images/hog.png")
w.Write(data)
2014-04-20 15:35:59 +01:00
}
2014-04-27 19:13:22 +01:00
func web_img_github(w http.ResponseWriter, r *http.Request, route *handler.Route) {
w.Header().Set("Content-Type", "image/png")
data, _ := cfg.Assets("assets/images/github.png")
w.Write(data)
}
2014-04-27 18:19:30 +01:00
func web_img_ajaxloader(w http.ResponseWriter, r *http.Request, route *handler.Route) {
w.Header().Set("Content-Type", "image/gif")
data, _ := cfg.Assets("assets/images/ajax-loader.gif")
w.Write(data)
}
2014-04-20 15:35:59 +01:00
func web_render(content string) string {
2014-04-27 16:06:58 +01:00
data, _ := cfg.Assets("assets/templates/layout.html")
layout := string(data)
html := strings.Replace(layout, "<%= content %>", content, -1)
2014-04-27 19:26:21 +01:00
// TODO clean this up
html = strings.Replace(html, "<%= config[Hostname] %>", cfg.Hostname, -1)
2014-04-27 16:06:58 +01:00
return html
2014-04-20 15:35:59 +01:00
}
func web_headers(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html")
}
2014-04-24 00:22:50 +01:00
func Start(exitCh chan int, conf *config.Config) {
2014-04-20 15:35:59 +01:00
exitChannel = exitCh
2014-04-24 00:22:50 +01:00
cfg = conf
2014-04-20 16:05:50 +01:00
2014-04-20 19:49:05 +01:00
server := &http.Server{
Addr: conf.HTTPBindAddr,
Handler: &handler.RegexpHandler{},
}
2014-04-20 20:01:53 +01:00
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/exit/?$"), web_exit)
2014-04-20 19:49:05 +01:00
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/js/controllers.js$"), web_jscontroller)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/images/hog.png$"), web_imgcontroller)
2014-04-27 19:13:22 +01:00
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/images/github.png$"), web_img_github)
2014-04-27 18:19:30 +01:00
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/images/ajax-loader.gif$"), web_img_ajaxloader)
2014-04-20 19:49:05 +01:00
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/$"), web_index)
2014-04-24 00:22:50 +01:00
api.CreateAPIv1(exitCh, conf, server)
2014-04-20 19:49:05 +01:00
server.ListenAndServe()
2014-04-20 15:35:59 +01:00
}