-
Notifications
You must be signed in to change notification settings - Fork 0
/
microservice.go
executable file
·66 lines (52 loc) · 1.47 KB
/
microservice.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
package main
import (
"fmt"
"log"
"microservice/api"
"net/http"
"os"
"go.uber.org/zap"
//"github.com/PacktPublishing/Cloud-Native-Go/api"
)
var build string
func main() {
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
// compile passing -ldflags "-X main.Build <build sha1>"
// fmt.Printf("Using build: %s\n", Build)
sugar.Infof("Using build: %s", build)
http.HandleFunc("/", index)
http.HandleFunc("/api/echo", api.EchoHandleFunc)
http.HandleFunc("/api/hello", api.HelloHandleFunc)
http.HandleFunc("/api/books", api.BooksHandleFunc)
http.HandleFunc("/api/books/", api.BookHandleFunc)
// var url = "http://website.com"
// sugar.Infow("failed to fetch URL",
// // Structured context as loosely typed key-value pairs.
// "url", url,
// "attempt", 3,
// "backoff", time.Second,
// )
sugar.Infof("starting Listener")
http.ListenAndServe(port(), nil)
sugar.Infof("stopping Listener")
}
func port() string {
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
return ":" + port
}
func index(w http.ResponseWriter, r *http.Request) {
logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Welcome to Cloud Native Go (Update).")
sugar.Infow("calling index")
}