MailHog/mailhog/http/server.go

64 lines
1.8 KiB
Go
Raw Normal View History

2014-04-20 14:35:59 +00:00
package http
import (
2014-04-20 16:09:06 +00:00
"regexp"
2014-04-20 18:49:05 +00:00
"net/http"
2014-04-20 14:35:59 +00:00
"github.com/ian-kent/MailHog/mailhog"
"github.com/ian-kent/MailHog/mailhog/templates"
"github.com/ian-kent/MailHog/mailhog/templates/images"
"github.com/ian-kent/MailHog/mailhog/templates/js"
2014-04-20 18:49:05 +00:00
"github.com/ian-kent/MailHog/mailhog/http/api"
"github.com/ian-kent/MailHog/mailhog/http/handler"
2014-04-20 14:35:59 +00:00
)
var exitChannel chan int
2014-04-20 15:05:50 +00:00
var config *mailhog.Config
2014-04-20 14:35:59 +00:00
2014-04-20 18:49:05 +00:00
func web_exit(w http.ResponseWriter, r *http.Request, route *handler.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-20 18:49:05 +00:00
func web_index(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 14:35:59 +00:00
web_headers(w)
2014-04-20 15:05:50 +00:00
w.Write([]byte(web_render(templates.Index())))
2014-04-20 14:35:59 +00:00
}
2014-04-20 18:49:05 +00:00
func web_jscontroller(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 14:35:59 +00:00
w.Header().Set("Content-Type", "text/javascript")
2014-04-20 15:05:50 +00:00
w.Write([]byte(js.Controllers()))
2014-04-20 14:35:59 +00:00
}
2014-04-20 18:49:05 +00:00
func web_imgcontroller(w http.ResponseWriter, r *http.Request, route *handler.Route) {
2014-04-20 14:35:59 +00:00
w.Header().Set("Content-Type", "image/png")
w.Write(images.Hog())
}
func web_render(content string) string {
return templates.Layout(content)
}
func web_headers(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html")
}
func Start(exitCh chan int, conf *mailhog.Config) {
exitChannel = exitCh
2014-04-20 15:05:50 +00:00
config = conf
2014-04-20 18:49:05 +00:00
server := &http.Server{
Addr: conf.HTTPBindAddr,
Handler: &handler.RegexpHandler{},
}
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/exit$"), web_exit)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/js/controllers.js$"), web_jscontroller)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/images/hog.png$"), web_imgcontroller)
server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/$"), web_index)
api.CreateAPIv1(exitCh, conf, server)
server.ListenAndServe()
2014-04-20 14:35:59 +00:00
}