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.
55 lines
1.0 KiB
55 lines
1.0 KiB
package main |
|
|
|
import ( |
|
"embed" |
|
"html/template" |
|
"log" |
|
"net/http" |
|
) |
|
|
|
//go:embed static |
|
var staticFiles embed.FS |
|
|
|
type Guestbook struct { |
|
Comments []string |
|
Count int |
|
} |
|
|
|
func indexHandler(writer http.ResponseWriter, request *http.Request) { |
|
html, err := template.ParseFS(staticFiles, "static/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) |
|
|
|
var staticFS = http.FS(staticFiles) |
|
http.Handle("/static/", http.FileServer(staticFS)) |
|
|
|
err := http.ListenAndServe("0.0.0.0:80", nil) |
|
log.Fatal(err) |
|
} |
|
|
|
func check(err error) { |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
}
|
|
|