File tree 2 files changed +16
-2
lines changed
2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -16,15 +16,23 @@ func main() {
16
16
})
17
17
templates = template .Must (template .ParseGlob ("templates/*.html" ))
18
18
r := mux .NewRouter ()
19
- r .HandleFunc ("/" , indexHandler ).Methods ("GET" )
19
+ r .HandleFunc ("/" , indexGetHandler ).Methods ("GET" )
20
+ r .HandleFunc ("/" , indexPostHandler ).Methods ("POST" )
20
21
http .Handle ("/" , r )
21
22
http .ListenAndServe (":8080" , nil )
22
23
}
23
24
24
- func indexHandler (w http.ResponseWriter , r * http.Request ) {
25
+ func indexGetHandler (w http.ResponseWriter , r * http.Request ) {
25
26
comments , err := client .LRange ("comments" , 0 , 10 ).Result ()
26
27
if err != nil {
27
28
return
28
29
}
29
30
templates .ExecuteTemplate (w , "index.html" , comments )
30
31
}
32
+
33
+ func indexPostHandler (w http.ResponseWriter , r * http.Request ) {
34
+ r .ParseForm ()
35
+ comment := r .PostForm .Get ("comment" )
36
+ client .LPush ("comments" , comment )
37
+ http .Redirect (w , r , "/" , 302 )
38
+ }
Original file line number Diff line number Diff line change 4
4
</ head >
5
5
< body >
6
6
< h1 > Comments</ h1 >
7
+ < form method ="POST ">
8
+ < textarea name ="comment "> </ textarea >
9
+ < div >
10
+ < button type ="submit "> Post Comment</ button >
11
+ </ div >
12
+ </ form >
7
13
{{ range . }}
8
14
< div > {{ . }}</ div >
9
15
{{ end }}
You can’t perform that action at this time.
0 commit comments