Skip to content
Snippets Groups Projects
Commit 93f147d6 authored by Will McCutchen's avatar Will McCutchen
Browse files

Embed templates in binary

parent 701e470f
No related branches found
No related tags found
No related merge requests found
build: dist/go-httpbin
dist/go-httpbin: *.go httpbin/*.go
dist/go-httpbin: assets *.go httpbin/*.go
mkdir -p dist
go build -o dist/go-httpbin
assets: httpbin/templates/*.html
go-bindata -o httpbin/templates.go -pkg=httpbin -prefix=httpbin/templates httpbin/templates
test:
go test -v github.com/mccutchen/go-httpbin/httpbin
......
......@@ -9,33 +9,18 @@ import (
// Index renders an HTML index page
func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) {
t := h.templates.Lookup("index.html")
if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return
}
t.Execute(w, nil)
w.Write(MustAsset("index.html"))
}
// FormsPost renders an HTML form that submits a request to the /post endpoint
func (h *HTTPBin) FormsPost(w http.ResponseWriter, r *http.Request) {
t := h.templates.Lookup("forms-post.html")
if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return
}
t.Execute(w, nil)
w.Write(MustAsset("forms-post.html"))
}
// UTF8 renders an HTML encoding stress test
func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) {
t := h.templates.Lookup("utf8.html")
if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t.Execute(w, nil)
w.Write(MustAsset("utf8.html"))
}
// Get handles HTTP GET requests
......
package httpbin
import (
"html/template"
"log"
"net/http"
"net/url"
)
......@@ -46,8 +44,7 @@ type Options struct {
// HTTPBin contains the business logic
type HTTPBin struct {
options *Options
templates *template.Template
options *Options
}
// Handler returns an http.Handler that exposes all HTTPBin endpoints
......@@ -76,14 +73,7 @@ func NewHTTPBin(options *Options) *HTTPBin {
if options == nil {
options = &Options{}
}
t, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalf("error parsing templates: %s", err)
}
return &HTTPBin{
options: options,
templates: t,
options: options,
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment