forked from TannerGabriel/learning-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.go
38 lines (32 loc) · 832 Bytes
/
Server.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
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
// Associate URLs requested to functions that handle the requests
http.HandleFunc("/hello", helloRequest)
http.HandleFunc("/", getRequest)
http.HandleFunc("/headers", headers)
// Start the web server
log.Println("Listening on http://localhost:8080/")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// Basic handler for /hello requests
func helloRequest(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
return
}
func getRequest(w http.ResponseWriter, r *http.Request) {
file_requested := "./" + r.URL.Path
http.ServeFile(w, r, file_requested)
return
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}