You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
956 B
50 lines
956 B
package main |
|
|
|
import ( |
|
"html/template" |
|
"log" |
|
"net/http" |
|
) |
|
|
|
type Guestbook struct { |
|
Comments []string |
|
Count int |
|
} |
|
|
|
func indexHandler(writer http.ResponseWriter, request *http.Request) { |
|
html, err := template.ParseFiles("html/index.html") |
|
check(err) |
|
|
|
store := TxtStore("comments.txt") |
|
guestbook, err := store.GetGuestbook() |
|
check(err) |
|
|
|
err = html.Execute(writer, guestbook) |
|
check(err) |
|
} |
|
|
|
func newHandler(writer http.ResponseWriter, request *http.Request) { |
|
comment := request.FormValue("comment") |
|
store := TxtStore("comments.txt") |
|
|
|
err := store.AddComment(comment) |
|
check(err) |
|
|
|
http.Redirect(writer, request, "/", http.StatusFound) |
|
} |
|
|
|
func main() { |
|
http.HandleFunc("/", indexHandler) |
|
http.HandleFunc("/new", newHandler) |
|
|
|
http.Handle("/html/", http.StripPrefix("/html/", http.FileServer(http.Dir("html")))) |
|
|
|
err := http.ListenAndServe("0.0.0.0:80", nil) |
|
log.Fatal(err) |
|
} |
|
|
|
func check(err error) { |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
}
|
|
|