Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
615ed601
Commit
615ed601
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Add /forms/post, refactor template handling
parent
f77f01c3
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
main.go
+14
-6
14 additions, 6 deletions
main.go
main_test.go
+10
-0
10 additions, 0 deletions
main_test.go
middleware.go
+17
-0
17 additions, 0 deletions
middleware.go
templates/forms-post.html
+29
-0
29 additions, 0 deletions
templates/forms-post.html
with
70 additions
and
6 deletions
main.go
+
14
−
6
View file @
615ed601
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
main_test.go
+
10
−
0
View file @
615ed601
...
@@ -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"
...
...
This diff is collapsed.
Click to expand it.
middleware.go
+
17
−
0
View file @
615ed601
...
@@ -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
)
}
}
}
This diff is collapsed.
Click to expand it.
templates/forms-post.html
0 → 100644
+
29
−
0
View file @
615ed601
<!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>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment