Skip to content

Commit

Permalink
Remove gotcha (fix mailhog/MailHog#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-kent committed Apr 19, 2015
1 parent 66b87f8 commit 46d5e8b
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 172 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ui:
go install .

bindata: bindata-deps
rm assets/assets.go
go-bindata -o assets/assets.go -pkg assets assets/...

bindata-deps:
Expand All @@ -17,7 +18,6 @@ fmt:
deps: bindata-deps
#FIXME cleanup this
go get github.com/mailhog/http
go get github.com/ian-kent/gotcha/gotcha
go get github.com/ian-kent/go-log/log
go get github.com/ian-kent/envconf
go get github.com/ian-kent/goose
Expand Down
279 changes: 138 additions & 141 deletions assets/assets.go

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"flag"
"os"

gohttp "net/http"

"github.com/gorilla/pat"
"github.com/ian-kent/go-log/log"
gotcha "github.com/ian-kent/gotcha/app"
"github.com/mailhog/MailHog-UI/assets"
"github.com/mailhog/MailHog-UI/config"
"github.com/mailhog/MailHog-UI/web"
Expand All @@ -29,8 +31,8 @@ func main() {
// FIXME need to make API URL configurable

exitCh = make(chan int)
cb := func(app *gotcha.App) {
web.CreateWeb(conf, app)
cb := func(r gohttp.Handler) {
web.CreateWeb(conf, r.(*pat.Router), assets.Asset)
}
go http.Listen(conf.UIBindAddr, assets.Asset, exitCh, cb)

Expand Down
113 changes: 86 additions & 27 deletions web/web.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,108 @@
package web

import (
"bytes"
"html/template"
"log"
"mime"
"net/http"
"strings"

gotcha "github.com/ian-kent/gotcha/app"
"github.com/ian-kent/gotcha/events"
"github.com/ian-kent/gotcha/http"
"github.com/gorilla/pat"
"github.com/mailhog/MailHog-UI/config"
)

var APIHost string

type Web struct {
config *config.Config
app *gotcha.App
asset func(string) ([]byte, error)
}

func CreateWeb(cfg *config.Config, app *gotcha.App) *Web {
app.On(events.BeforeHandler, func(session *http.Session, next func()) {
session.Stash["config"] = cfg
next()
})

r := app.Router
func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, error)) *Web {
web := &Web{
config: cfg,
asset: asset,
}

r.Get("/images/(?P<file>.*)", r.Static("assets/images/{{file}}"))
r.Get("/css/(?P<file>.*)", r.Static("assets/css/{{file}}"))
r.Get("/js/(?P<file>.*)", r.Static("assets/js/{{file}}"))
r.Get("/fonts/(?P<file>.*)", r.Static("assets/fonts/{{file}}"))
r.Get("/", Index)
pat.Path("/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}"))
pat.Path("/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}"))
pat.Path("/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}"))
pat.Path("/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}"))
pat.Path("/").Methods("GET").HandlerFunc(web.Index())

app.Config.LeftDelim = "[:"
app.Config.RightDelim = ":]"
return web
}

return &Web{
config: cfg,
app: app,
func (web Web) Static(pattern string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
fp := strings.TrimSuffix(pattern, "{{file}}") + req.URL.Query().Get(":file")
if b, err := web.asset(fp); err == nil {
w.Header().Set("Content-Type", mime.TypeByExtension(fp))
w.WriteHeader(200)
w.Write(b)
return
}
log.Printf("[UI] File not found: %s", fp)
w.WriteHeader(404)
}
}

func Index(session *http.Session) {
html, _ := session.RenderTemplate("index.html")
func (web Web) Index() func(http.ResponseWriter, *http.Request) {
tmpl := template.New("index.html")
tmpl.Delims("[:", ":]")

session.Stash["Page"] = "Browse"
session.Stash["Content"] = template.HTML(html)
session.Stash["APIHost"] = APIHost
session.Render("layout.html")
asset, err := web.asset("assets/templates/index.html")
if err != nil {
log.Fatalf("[UI] Error loading index.html: %s", err)
}

tmpl, err = tmpl.Parse(string(asset))
if err != nil {
log.Fatalf("[UI] Error parsing index.html: %s", err)
}

layout := template.New("layout.html")
layout.Delims("[:", ":]")

asset, err = web.asset("assets/templates/layout.html")
if err != nil {
log.Fatalf("[UI] Error loading layout.html: %s", err)
}

layout, err = layout.Parse(string(asset))
if err != nil {
log.Fatalf("[UI] Error parsing layout.html: %s", err)
}

return func(w http.ResponseWriter, req *http.Request) {
data := map[string]interface{}{
"config": web.config,
"Page": "Browse",
"APIHost": APIHost,
}

b := new(bytes.Buffer)
err := tmpl.Execute(b, data)

if err != nil {
log.Printf("[UI] Error executing template: %s", err)
w.WriteHeader(500)
return
}

data["Content"] = template.HTML(b.String())

b = new(bytes.Buffer)
err = layout.Execute(b, data)

if err != nil {
log.Printf("[UI] Error executing template: %s", err)
w.WriteHeader(500)
return
}

w.WriteHeader(200)
w.Write(b.Bytes())
}
}

0 comments on commit 46d5e8b

Please sign in to comment.