-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
122 lines (104 loc) · 2.85 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/go-kit/kit/log"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/rcholic/CognitoREST/api"
)
var (
port string
zip string
)
var (
HTTPLatency = stdprometheus.NewHistogramVec(stdprometheus.HistogramOpts{
Name: "request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: stdprometheus.DefBuckets,
}, []string{"method", "route", "status_code", "isWS"})
)
const (
ServiceName = "user"
)
func init() {
stdprometheus.MustRegister(HTTPLatency)
// flag.StringVar(&zip, "zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
flag.StringVar(&port, "port", "3000", "Port on which to run")
}
func main() {
flag.Parse()
errc := make(chan error)
// Log domain.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
var tracer stdopentracing.Tracer
{
// Find service local IP.
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
host := strings.Split(localAddr.String(), ":")[0]
defer conn.Close()
if zip == "" {
tracer = stdopentracing.NoopTracer{}
} else {
logger := log.With(logger, "tracer", "Zipkin")
logger.Log("addr", zip)
collector, err := zipkin.NewHTTPCollector(
zip,
zipkin.HTTPLogger(logger),
)
logger.Log("collector is: %v\n", collector)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
// TODO: set up Zipkin tracer server here
tracer, err = zipkin.NewTracer(
zipkin.NewRecorder(collector, false, fmt.Sprintf("%v:%v", host, port), ServiceName),
)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
}
stdopentracing.InitGlobalTracer(tracer)
}
var service api.UserService
{
service = api.NewUserService()
service = api.LoggingMiddleware(logger)(service)
// TODO: instrumenting service
}
endpoints := api.MakeEndpoints(service, tracer)
router := api.MakeHTTPHandler(endpoints, logger, tracer)
go func() {
logger.Log("transport", "HTTP", "port", port)
errc <- http.ListenAndServe(fmt.Sprintf(":%v", port), router)
}()
// Capture interrupts.
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
logger.Log("exit", <-errc)
}
// SPA authentication: https://onedrive.live.com/?authkey=%21AAPTU-jr3dRC4XY&cid=01965DB52C0BCA35&id=1965DB52C0BCA35%211660&parId=1965DB52C0BCA35%211658&o=OneUp
// https://github.com/IdentityServer/IdentityServer3/issues/2039
// video: https://vimeo.com/131636653