-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_mux_router.go
executable file
·68 lines (57 loc) · 1.57 KB
/
http_mux_router.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
arguments := os.Args
listenAddr := ":8080"
//initialize a logger for server messages output
logger := log.New(os.Stdout, "HTTP_SERVER: ", log.LstdFlags)
if len(arguments) == 1 {
logger.Println("No port number provided as parameter, using 8080 as a default!")
} else {
listenAddr = ":" + arguments[1]
}
logger.Printf("Starting HTTP server on port %s", listenAddr)
mux := http.NewServeMux()
mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprintln(w, "User GET")
}
if r.Method == http.MethodPost {
fmt.Fprintln(w, "User POST")
}
})
mux.HandleFunc("/time", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
now := time.Now()
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"time\":\"%s\"}", now.Format(time.RFC3339))
} else {
w.WriteHeader(http.StatusBadRequest)
}
})
// separate handler
itemMux := http.NewServeMux()
itemMux.HandleFunc("/items/clothes", func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, "Clothes")
})
mux.Handle("/items/", itemMux)
// Admin handlers
adminMux := http.NewServeMux()
adminMux.HandleFunc("/ports", func(w http.ResponseWriter,
r *http.Request) {
fmt.Fprintln(w, "Ports")
})
mux.Handle("/admin/", http.StripPrefix("/admin",
adminMux))
// Default server
if err := http.ListenAndServe(listenAddr, mux); err != http.ErrServerClosed {
logger.Fatalf("Could not listen on %q: %s\n", listenAddr, err)
}
}