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 build: dist/go-httpbin
dist/go-httpbin: *.go httpbin/*.go dist/go-httpbin: assets *.go httpbin/*.go
mkdir -p dist mkdir -p dist
go build -o dist/go-httpbin go build -o dist/go-httpbin
assets: httpbin/templates/*.html
go-bindata -o httpbin/templates.go -pkg=httpbin -prefix=httpbin/templates httpbin/templates
test: test:
go test -v github.com/mccutchen/go-httpbin/httpbin go test -v github.com/mccutchen/go-httpbin/httpbin
......
...@@ -9,33 +9,18 @@ import ( ...@@ -9,33 +9,18 @@ import (
// Index renders an HTML index page // Index renders an HTML index page
func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) { func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) {
t := h.templates.Lookup("index.html") w.Write(MustAsset("index.html"))
if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return
}
t.Execute(w, nil)
} }
// FormsPost renders an HTML form that submits a request to the /post endpoint // FormsPost renders an HTML form that submits a request to the /post endpoint
func (h *HTTPBin) FormsPost(w http.ResponseWriter, r *http.Request) { func (h *HTTPBin) FormsPost(w http.ResponseWriter, r *http.Request) {
t := h.templates.Lookup("forms-post.html") w.Write(MustAsset("forms-post.html"))
if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return
}
t.Execute(w, nil)
} }
// UTF8 renders an HTML encoding stress test // UTF8 renders an HTML encoding stress test
func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) { 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") w.Header().Set("Content-Type", "text/html; charset=utf-8")
t.Execute(w, nil) w.Write(MustAsset("utf8.html"))
} }
// Get handles HTTP GET requests // Get handles HTTP GET requests
......
package httpbin package httpbin
import ( import (
"html/template"
"log"
"net/http" "net/http"
"net/url" "net/url"
) )
...@@ -46,8 +44,7 @@ type Options struct { ...@@ -46,8 +44,7 @@ type Options struct {
// HTTPBin contains the business logic // HTTPBin contains the business logic
type HTTPBin struct { type HTTPBin struct {
options *Options options *Options
templates *template.Template
} }
// Handler returns an http.Handler that exposes all HTTPBin endpoints // Handler returns an http.Handler that exposes all HTTPBin endpoints
...@@ -76,14 +73,7 @@ func NewHTTPBin(options *Options) *HTTPBin { ...@@ -76,14 +73,7 @@ func NewHTTPBin(options *Options) *HTTPBin {
if options == nil { if options == nil {
options = &Options{} options = &Options{}
} }
t, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalf("error parsing templates: %s", err)
}
return &HTTPBin{ return &HTTPBin{
options: options, options: options,
templates: t,
} }
} }
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