forked from microservices-demo/user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (102 loc) · 2.66 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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/TUB-CNPE-TB/user/api"
"github.com/TUB-CNPE-TB/user/db"
"github.com/TUB-CNPE-TB/user/db/mongodb"
"github.com/go-kit/kit/log"
stdopentracing "github.com/opentracing/opentracing-go"
stdprometheus "github.com/prometheus/client_golang/prometheus"
)
var (
port string
zip string
)
var (
HTTPLatency = stdprometheus.NewHistogramVec(stdprometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: stdprometheus.DefBuckets,
}, []string{"method", "path", "status_code", "isWS"})
)
const (
ServiceName = "user"
)
func init() {
stdprometheus.MustRegister(HTTPLatency)
flag.StringVar(&zip, "zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
flag.StringVar(&port, "port", "8084", "Port on which to run")
db.Register("mongodb", &mongodb.Mongo{})
}
func main() {
flag.Parse()
// Mechanical stuff.
errc := make(chan error)
// Log domain.
var logger log.Logger
{
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
// Find service local IP.
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
_ = logger.Log("msg", "Unable to find service local IP", "error", err)
os.Exit(1)
} else {
_ = logger.Log("local_ip", conn.LocalAddr())
}
//localAddr := conn.LocalAddr().(*net.UDPAddr)
//host := strings.Split(localAddr.String(), ":")[0]
defer conn.Close()
var tracer stdopentracing.Tracer
{
if zip == "" {
_ = logger.Log("zipkin_status", "No tracer")
tracer = stdopentracing.NoopTracer{}
}
stdopentracing.InitGlobalTracer(tracer)
}
dbconn := false
for !dbconn {
err := db.Init()
if err != nil {
if err == db.ErrNoDatabaseSelected {
_ = logger.Log("db_conn", "No database selected", "error", err)
os.Exit(1)
}
_ = logger.Log("db_conn", "Database init failed", "error", err)
} else {
dbconn = true
}
}
// Service domain.
var service api.Service
{
service = api.NewFixedService()
service = api.LoggingMiddleware(logger)(service)
}
// Endpoint domain.
endpoints := api.MakeEndpoints(service, tracer)
// HTTP router
router := api.MakeHTTPHandler(endpoints, logger, tracer)
// Create and launch the HTTP server.
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)
}