forked from apex/up-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (50 loc) · 1.25 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/json"
)
func init() {
log.SetHandler(json.Default)
}
func main() {
addr := ":" + os.Getenv("PORT")
http.HandleFunc("/text/", handleText)
http.HandleFunc("/json/", handleJSON)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatalf("error: %s", err)
}
}
// text logs.
//
// Note that the indentation here matters, as it
// will be treated as a single log by Up.
func handleText(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request:\n")
fmt.Printf(" - method: %s\n", r.Method)
fmt.Printf(" - url: %s\n", r.URL)
fmt.Printf(" - header:\n")
for k := range r.Header {
fmt.Printf(" - %s: %s\n", k, r.Header.Get(k))
}
fmt.Fprintln(w, "Hello World from Go")
}
// json logs.
//
// Note that the apex/log json handler already supports
// the format which Up expects in terms of .level, .message
// and .fields properties.
func handleJSON(w http.ResponseWriter, r *http.Request) {
ctx := log.WithFields(log.Fields{
"method": r.Method,
"url": r.URL.String(),
"route": "handleJSON",
})
ctx.Info("doing some work")
time.Sleep(time.Millisecond * 250)
ctx.Info("did some work")
fmt.Fprintln(w, "Hello World from Go")
}