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

Add /forms/post, refactor template handling

parent f77f01c3
No related branches found
No related tags found
No related merge requests found
...@@ -37,13 +37,19 @@ type UserAgentResp struct { ...@@ -37,13 +37,19 @@ type UserAgentResp struct {
UserAgent string `json:"user-agent"` UserAgent string `json:"user-agent"`
} }
func index(w http.ResponseWriter, r *http.Request) { // Index must be wrapped by the withTemplates middleware before it can be used
t, err := template.ParseGlob("templates/*.html") func index(w http.ResponseWriter, r *http.Request, t *template.Template) {
if err != nil { t = t.Lookup("index.html")
http.Error(w, fmt.Sprintf("error parsing templates: %s", err), http.StatusInternalServerError) if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return return
} }
t = t.Lookup("index.html") t.Execute(w, nil)
}
// FormsPost must be wrapped by withTemplates middleware before it can be used
func formsPost(w http.ResponseWriter, r *http.Request, t *template.Template) {
t = t.Lookup("forms-post.html")
if t == nil { if t == nil {
http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("error looking up index.html"), http.StatusInternalServerError)
return return
...@@ -83,7 +89,9 @@ func headers(w http.ResponseWriter, r *http.Request) { ...@@ -83,7 +89,9 @@ func headers(w http.ResponseWriter, r *http.Request) {
func app() http.Handler { func app() http.Handler {
h := http.NewServeMux() h := http.NewServeMux()
h.HandleFunc("/", methods(index, "GET")) templateWrapper := withTemplates("templates/*.html")
h.HandleFunc("/", methods(templateWrapper(index), "GET"))
h.HandleFunc("/forms/post", methods(templateWrapper(formsPost), "GET"))
h.HandleFunc("/get", methods(get, "GET")) h.HandleFunc("/get", methods(get, "GET"))
h.HandleFunc("/ip", ip) h.HandleFunc("/ip", ip)
h.HandleFunc("/user-agent", userAgent) h.HandleFunc("/user-agent", userAgent)
......
...@@ -19,6 +19,16 @@ func TestIndex(t *testing.T) { ...@@ -19,6 +19,16 @@ func TestIndex(t *testing.T) {
} }
} }
func TestFormsPost(t *testing.T) {
r, _ := http.NewRequest("GET", "/forms/post", nil)
w := httptest.NewRecorder()
app().ServeHTTP(w, r)
if !strings.Contains(w.Body.String(), `<form method="post" action="/post">`) {
t.Fatalf("expected <form> in body")
}
}
func TestGet__Basic(t *testing.T) { func TestGet__Basic(t *testing.T) {
r, _ := http.NewRequest("GET", "/get", nil) r, _ := http.NewRequest("GET", "/get", nil)
r.Host = "localhost" r.Host = "localhost"
......
...@@ -2,6 +2,7 @@ package main ...@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"log" "log"
"net/http" "net/http"
) )
...@@ -50,3 +51,19 @@ func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc { ...@@ -50,3 +51,19 @@ func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc {
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
} }
} }
// Simplify function signatures below
type templateHandlerFunc func(http.ResponseWriter, *http.Request, *template.Template)
func withTemplates(path string) func(templateHandlerFunc) http.HandlerFunc {
t, err := template.ParseGlob(path)
if err != nil {
log.Fatalf("error parsing templates: %s", err)
}
return func(handler templateHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handler(w, r, t)
}
}
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<!-- Example form from HTML5 spec http://www.w3.org/TR/html5/forms.html#writing-a-form's-user-interface -->
<form method="post" action="/post">
<p><label>Customer name: <input name="custname"></label></p>
<p><label>Telephone: <input type=tel name="custtel"></label></p>
<p><label>E-mail address: <input type=email name="custemail"></label></p>
<fieldset>
<legend> Pizza Size </legend>
<p><label> <input type=radio name=size value="small"> Small </label></p>
<p><label> <input type=radio name=size value="medium"> Medium </label></p>
<p><label> <input type=radio name=size value="large"> Large </label></p>
</fieldset>
<fieldset>
<legend> Pizza Toppings </legend>
<p><label> <input type=checkbox name="topping" value="bacon"> Bacon </label></p>
<p><label> <input type=checkbox name="topping" value="cheese"> Extra Cheese </label></p>
<p><label> <input type=checkbox name="topping" value="onion"> Onion </label></p>
<p><label> <input type=checkbox name="topping" value="mushroom"> Mushroom </label></p>
</fieldset>
<p><label>Preferred delivery time: <input type=time min="11:00" max="21:00" step="900" name="delivery"></label></p>
<p><label>Delivery instructions: <textarea name="comments"></textarea></label></p>
<p><button>Submit order</button></p>
</form>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment