-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
43 lines (36 loc) · 997 Bytes
/
example.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/OsagieDG/mlog/service/middleware"
)
func main() {
router := http.NewServeMux()
router.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Hello Everyone!")
})
router.HandleFunc("GET /welcome", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "You are welcome to use logStack!")
})
mlog := middleware.MLog(
middleware.LogRequest,
middleware.LogResponse,
middleware.RecoverPanic,
)
listenAddr := ":6862"
log.Printf("Server is listening on %s", listenAddr)
if err := http.ListenAndServe(listenAddr, mlog(router)); err != nil {
log.Fatal("HTTP server error:", err)
}
}