Solution Review: Web Application for Serving Guests
Explore how to implement a Go-based web application that serves guests by using HTML templates, handling routing for root and add requests, processing form inputs, and dynamically displaying updated guest lists in the browser. This lesson helps you understand web server setup and template execution in Go.
We'll cover the following...
We'll cover the following...
package main
import (
"fmt"
"html/template"
"net/http"
)
const port = 3000
var guestList []string
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/add", addHandler)
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
// indexHandler serves the main page
func indexHandler(w http.ResponseWriter, req *http.Request) {
t := template.New("index.html")
t, err := t.Parse(indexHTML)
if err != nil {
message := fmt.Sprintf("bad template: %s", err)
http.Error(w, message, http.StatusInternalServerError)
}
// the HTML output is now safe against code injection
t.Execute(w, guestList)
}
// addHandler add a name to the names list
func addHandler(w http.ResponseWriter, req *http.Request) {
guest := req.FormValue("name")
if len(guest) > 0 {
guestList = append(guestList, guest)
}
http.Redirect(w, req, "/", http.StatusFound)
}
var indexHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Guest Book ::Web GUI</title>
</head>
<body>
<h1>Guest Book :: Web GUI</h1>
<form action="/add" method="post">
Name: <input name="name" /><submit value="Sign Guest Book">
</form>
<hr />
<h4>Previous Guests</h4>
<ul>
{{range .}}
<li>{{.}}</li>
{{end}}
</ul>
</body>
</html>
`In the code ...