Skip to content

Commit

Permalink
completed 003
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Luo committed Jun 19, 2019
1 parent 0ab4cb7 commit 3733a44
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added exercise-003-web/.DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions exercise-003-web/nameTrack/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<title>Sean's Super Cool Web App</title>

<body>
<form action="/signup" method="POST">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" value="Check-In"/>
</form>
</body>
</html>
34 changes: 34 additions & 0 deletions exercise-003-web/nameTrack/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"html/template"
"net/http"
)

var homeT = template.Must(template.ParseFiles("home.html"))

var table = map[string]int{} // global table to hold usernames and counts of visits

func home(w http.ResponseWriter, r *http.Request) {
homeT.Execute(w, nil)

r.ParseForm()
visitor := r.Form.Get("username") //hold current visitor name

if count, isIn := table[visitor]; isIn {
table[visitor] = count + 1 // user has been seen before, increment count
} else {
table[visitor] = 1 // new user, add to dict
}

for name, count := range table {
fmt.Fprintf(w, "<b>User:</b> %v <b>Visits:</b> %v <br><br>", name, count) // print name and # of times visited
}

}

func main() {
http.HandleFunc("/", home) // handle all pages with home()
http.ListenAndServe(":8080", nil) // see web app by visiting "localhost:8080"
}

0 comments on commit 3733a44

Please sign in to comment.