Commits

Miki Tebeka  committed 151647b

Lock

  • Participants
  • Parent commits 4a9e8c4

Comments (0)

Files changed (1)

 	"html/template"
 	"net/http"
 	"os/exec"
+	"sync"
 	"time"
 )
 
 // probably want some persistence
 var guestList []string
 
+// Lock for mutating guest list
+var guestLock sync.Mutex
+
 // indexHandler serves the main page
 func indexHandler(w http.ResponseWriter, req *http.Request) {
 	t := template.New("index.html")
 		http.Error(w, message, http.StatusInternalServerError)
 	}
 
+	guestLock.Lock()
+	defer guestLock.Unlock()
 	t.Execute(w, guestList)
-
 }
 
 // addHandler add a name to the names list, then redirects back to /
 func addHandler(w http.ResponseWriter, req *http.Request) {
 	guest := req.FormValue("name")
 	if len(guest) > 0 {
+		guestLock.Lock()
 		guestList = append(guestList, guest)
+		defer guestLock.Unlock()
 	}
 
 	http.Redirect(w, req, "/", http.StatusFound)