-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (39 loc) · 857 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"io"
"net/http"
"os"
"strings"
)
func upTown(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/html; charset=utf-8")
var dogName string
fs := strings.Split(req.URL.Path, "/")
if len(fs) >= 3 {
dogName = fs[2]
}
// the image doesn't serve
io.WriteString(res, `
Dog Name: <strong>`+dogName+`</strong><br>
<img src="/toby.jpg">
`)
}
func dogPic(res http.ResponseWriter, req *http.Request) {
f, err := os.Open("toby.jpg")
if err != nil {
http.Error(res, "file not found", 404)
return
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
http.Error(res, "file not found", 404)
return
}
http.ServeContent(res, req, f.Name(), fi.ModTime(), f)
}
func main() {
http.HandleFunc("/toby.jpg", dogPic)
http.HandleFunc("/dog/", upTown)
http.ListenAndServe(":9000", nil)
}