Skip to content

Commit 90070e5

Browse files
committed
you're doing great
1 parent 46d4aa3 commit 90070e5

File tree

20 files changed

+316
-2
lines changed

20 files changed

+316
-2
lines changed

000_temp/77-web-server/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
)
77

88
var tpl *template.Template
9-
109
func init() {
1110
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
1211
}

000_temp/78/assets/toby.jpg

5.04 KB
Loading

000_temp/78/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"fmt"
6+
"html/template"
7+
"os"
8+
"io"
9+
)
10+
11+
var tpl *template.Template
12+
13+
func init() {
14+
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
15+
}
16+
17+
18+
func main() {
19+
http.HandleFunc("/", foo)
20+
http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets"))))
21+
http.ListenAndServe(":8080", nil)
22+
}
23+
24+
func foo(w http.ResponseWriter, r *http.Request) {
25+
fmt.Println("Method", r.Method)
26+
27+
fn := ""
28+
29+
if r.Method == http.MethodPost {
30+
// open
31+
f, h, err := r.FormFile("q")
32+
if err != nil {
33+
http.Error(w, err.Error(), http.StatusInternalServerError)
34+
return
35+
}
36+
defer f.Close()
37+
38+
fn = h.Filename
39+
40+
df, err := os.Create("./assets/"+fn)
41+
if err != nil {
42+
http.Error(w, err.Error(), http.StatusInternalServerError)
43+
return
44+
}
45+
defer df.Close()
46+
47+
_, err = io.Copy(df, f)
48+
if err != nil {
49+
http.Error(w, err.Error(), http.StatusInternalServerError)
50+
return
51+
}
52+
}
53+
54+
// w.Header().Set("Content-Type", "text/html; charset=utf-8")
55+
56+
tpl.ExecuteTemplate(w, "index.gohtml", fn)
57+
}

000_temp/78/templates/index.gohtml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
</head>
10+
<body>
11+
12+
<form method="POST" enctype="multipart/form-data">
13+
<input type="file" name="q">
14+
<input type="submit">
15+
</form>
16+
<br>
17+
18+
{{if .}}
19+
file name: {{.}}
20+
<img src="/resources/{{.}}">
21+
{{end}}
22+
23+
</body>
24+
</html>

000_temp/79/main.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"html/template"
6+
"net/http"
7+
)
8+
9+
var tpl *template.Template
10+
11+
func init() {
12+
tpl = template.Must(template.ParseGlob("templates/*"))
13+
}
14+
15+
func main() {
16+
http.HandleFunc("/", foo)
17+
http.HandleFunc("/bar", bar)
18+
http.HandleFunc("/barred", barred)
19+
http.Handle("/favicon.ico", http.NotFoundHandler())
20+
http.ListenAndServe(":8080", nil)
21+
}
22+
23+
func foo(w http.ResponseWriter, req *http.Request) {
24+
25+
var s string
26+
27+
if req.Method == http.MethodPost {
28+
s = req.FormValue("fname")
29+
}
30+
31+
fmt.Print("Your request method at foo: ", req.Method, "\n")
32+
fmt.Print("Your FORM VALUE at foo: ", s, "\n\n")
33+
}
34+
35+
func bar(w http.ResponseWriter, req *http.Request) {
36+
fmt.Println("Your request method at bar:", req.Method)
37+
// process form submission here
38+
http.Redirect(w, req, "/", http.StatusSeeOther)
39+
}
40+
41+
func barred(w http.ResponseWriter, req *http.Request) {
42+
fmt.Println("Your request method at barred:", req.Method)
43+
tpl.ExecuteTemplate(w, "index.gohtml", nil)
44+
}

000_temp/79/templates/index.gohtml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
9+
<form method="POST" action="/bar">
10+
<input type="text" name="fname">
11+
<input type="submit">
12+
</form>
13+
14+
</body>
15+
</html>

000_temp/80-renamer/80-renamer.txt

2.1 MB
Binary file not shown.

000_temp/80-renamer/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"strconv"
8+
)
9+
10+
func main() {
11+
12+
dir := "results/"
13+
files, _ := ioutil.ReadDir(dir)
14+
15+
for i, f := range files {
16+
fmt.Println(f.Name())
17+
oldfile := (dir + f.Name())
18+
newfile := (dir + strconv.Itoa(i) + ".txt")
19+
fmt.Println(oldfile, "named it to", newfile, "\n")
20+
21+
err := os.Rename(oldfile, newfile)
22+
if err != nil {
23+
fmt.Println(err)
24+
return
25+
}
26+
}
27+
}

000_temp/80-renamer/results/0.txt

Whitespace-only changes.

000_temp/80-renamer/results/1.txt

Whitespace-only changes.

000_temp/81-cookie-counter/main.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"html/template"
6+
"strconv"
7+
"fmt"
8+
)
9+
10+
var tpl *template.Template
11+
12+
func init() {
13+
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
14+
}
15+
16+
func main() {
17+
http.HandleFunc("/", foo)
18+
http.HandleFunc("/bar", bar)
19+
http.ListenAndServe(":8080", nil)
20+
}
21+
22+
func foo(w http.ResponseWriter, r *http.Request) {
23+
c, err := cookieCounter(r)
24+
if err != nil {
25+
http.Error(w, "bad request", http.StatusBadRequest)
26+
return
27+
}
28+
http.SetCookie(w, c)
29+
30+
tpl.ExecuteTemplate(w, "index.gohtml", c.Value)
31+
}
32+
33+
34+
func bar(w http.ResponseWriter, r *http.Request) {
35+
c, err := cookieCounter(r)
36+
if err != nil {
37+
fmt.Println(err)
38+
http.Error(w, "bad request", http.StatusBadRequest)
39+
return
40+
}
41+
http.SetCookie(w, c)
42+
43+
tpl.ExecuteTemplate(w, "about.gohtml", c.Value)
44+
}
45+
46+
func cookieCounter(r *http.Request) (*http.Cookie, error) {
47+
var c *http.Cookie
48+
49+
c, err := r.Cookie("wackadoodle")
50+
if err != nil {
51+
c = &http.Cookie{
52+
Name: "wackadoodle",
53+
Value: "0",
54+
Path: "/",
55+
}
56+
}
57+
58+
i, err := strconv.Atoi(c.Value)
59+
if err != nil {
60+
return c, err
61+
}
62+
i++
63+
c.Value = strconv.Itoa(i)
64+
65+
return c, nil
66+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>ABOUT</title>
9+
</head>
10+
<body>
11+
12+
<h1>about</h1>
13+
VALUE OF COOKIE {{.}}
14+
15+
</body>
16+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
</head>
10+
<body>
11+
12+
<h1>home</h1>
13+
VALUE OF COOKIE {{.}}
14+
15+
</body>
16+
</html>

000_temp/82/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x uint
7+
x = 18446744073709551615
8+
fmt.Println(x)
9+
fmt.Printf("%T\n\n",x)
10+
}

029_cookies/01_set_get/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func set(w http.ResponseWriter, req *http.Request) {
1616
http.SetCookie(w, &http.Cookie{
1717
Name: "my-cookie",
1818
Value: "some value",
19+
Path: "/",
1920
})
2021
fmt.Fprintln(w, "COOKIE WRITTEN - CHECK YOUR BROWSER")
2122
fmt.Fprintln(w, "in chrome go to: dev tools / application / cookies")

029_cookies/02_multiple/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func set(w http.ResponseWriter, req *http.Request) {
1818
http.SetCookie(w, &http.Cookie{
1919
Name: "my-cookie",
2020
Value: "some value",
21+
Path: "/",
2122
})
2223
fmt.Fprintln(w, "COOKIE WRITTEN - CHECK YOUR BROWSER")
2324
fmt.Fprintln(w, "in chrome go to: dev tools / application / cookies")

029_cookies/03_hands-on/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1-
Using cookies, track how many times a user has been to your website domain.
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func main() {
9+
http.HandleFunc("/", set)
10+
http.HandleFunc("/read", read)
11+
http.Handle("/favicon.ico", http.NotFoundHandler())
12+
http.ListenAndServe(":8080", nil)
13+
}
14+
15+
func set(w http.ResponseWriter, req *http.Request) {
16+
http.SetCookie(w, &http.Cookie{
17+
Name: "my-cookie",
18+
Value: "some value",
19+
Path: "/",
20+
})
21+
fmt.Fprintln(w, "COOKIE WRITTEN - CHECK YOUR BROWSER")
22+
fmt.Fprintln(w, "in chrome go to: dev tools / application / cookies")
23+
}
24+
25+
func read(w http.ResponseWriter, req *http.Request) {
26+
27+
c, err := req.Cookie("my-cookie")
28+
if err != nil {
29+
http.Error(w, http.StatusText(400), http.StatusBadRequest)
30+
return
31+
}
32+
33+
fmt.Fprintln(w, "YOUR COOKIE:", c)
34+
}
35+
36+
// Using cookies, track how many times a user has been to your website domain.

029_cookies/04_solution/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func foo(res http.ResponseWriter, req *http.Request) {
2121
cookie = &http.Cookie{
2222
Name: "my-cookie",
2323
Value: "0",
24+
Path: "/",
2425
}
2526
}
2627

029_cookies/05_maxage/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func set(w http.ResponseWriter, req *http.Request) {
2121
http.SetCookie(w, &http.Cookie{
2222
Name: "session",
2323
Value: "some value",
24+
Path: "/",
2425
})
2526
fmt.Fprintln(w, `<h1><a href="/read">read</a></h1>`)
2627
}

029_cookies/06_path/03_templates/02/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func bowzer(w http.ResponseWriter, r *http.Request) {
3535
Name: "user-cookie",
3636
Value: "this would be the value",
3737
Path: "/",
38+
//Path: "/dog/bowzer/",
3839
}
3940
http.SetCookie(w, c)
4041
tpl.ExecuteTemplate(w, "bowzer.gohtml", c)

0 commit comments

Comments
 (0)