forked from PacktPublishing/Mastering-Go-Second-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestWWW.go
41 lines (34 loc) · 809 Bytes
/
testWWW.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
package main
import (
"fmt"
"net/http"
"os"
)
func CheckStatusOK(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `Fine!`)
}
func StatusNotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Serving: %s\n", r.URL.Path)
fmt.Printf("Served: %s\n", r.Host)
}
func main() {
PORT := ":8001"
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Using default port number: ", PORT)
} else {
PORT = ":" + arguments[1]
}
http.HandleFunc("/CheckStatusOK", CheckStatusOK)
http.HandleFunc("/StatusNotFound", StatusNotFound)
http.HandleFunc("/", MyHandler)
err := http.ListenAndServe(PORT, nil)
if err != nil {
fmt.Println(err)
return
}
}