-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove gotcha (fix mailhog/MailHog#42)
- Loading branch information
Showing
4 changed files
with
230 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |