Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

003-v1 #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added exercise-003-web/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions exercise-003-web/nameTrack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Instructions:

From within the nameTrack directory, run the command

"go run main.go"

Go to "localhost:8080" in your web browser
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"
}