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-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:13:35 +00:00
|
|
|
func web_jscontroller(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-20 14:35:59 +00:00
|
|
|
w.Header().Set("Content-Type", "text/javascript")
|
2014-04-27 15:06:58 +00:00
|
|
|
data, _ := cfg.Assets("assets/js/controllers.js")
|
|
|
|
w.Write(data)
|
2014-04-20 14:35:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func web_imgcontroller(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-20 14:35:59 +00:00
|
|
|
w.Header().Set("Content-Type", "image/png")
|
2014-04-27 15:06:58 +00:00
|
|
|
data, _ := cfg.Assets("assets/images/hog.png")
|
|
|
|
w.Write(data)
|
2014-04-20 14:35:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func web_img_github(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-27 18:13:22 +00:00
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
data, _ := cfg.Assets("assets/images/github.png")
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func web_img_ajaxloader(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-27 17:19:30 +00:00
|
|
|
w.Header().Set("Content-Type", "image/gif")
|
|
|
|
data, _ := cfg.Assets("assets/images/ajax-loader.gif")
|
|
|
|
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)
|
|
|
|
r.Get("^/js/controllers.js$", web_jscontroller)
|
|
|
|
r.Get("^/images/hog.png$", web_imgcontroller)
|
|
|
|
r.Get("^/images/github.png$", web_img_github)
|
|
|
|
r.Get("^/images/ajax-loader.gif$", web_img_ajaxloader)
|
|
|
|
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
|
|
|
}
|