Revise first draft
j3s j3s@c3f.net
Tue, 09 Jun 2020 22:12:23 -0500
1 files changed,
25 insertions(+),
3 deletions(-)
jump to
M
main.go
→
main.go
@@ -1,9 +1,31 @@
package main import ( - "fmt" + "html/template" + "log" + "net/http" ) -func main(){ - fmt.Println("Hello, world!") +func main() { + http.HandleFunc("/", rootHandler) + + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +func rootHandler(w http.ResponseWriter, r *http.Request) { + render(w, "templates/form.html", nil) +} + +func render(w http.ResponseWriter, filename string, data interface{}) { + tmpl, err := template.ParseFiles(filename) + + if err != nil { + log.Println(err) + http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError) + } + + if err := tmpl.Execute(w, data); err != nil { + log.Println(err) + http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError) + } }