forked from microservices-demo/catalogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (136 loc) · 3.95 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/go-kit/kit/log"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
"net"
"net/http"
"path/filepath"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/microservices-demo/catalogue"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/middleware"
"golang.org/x/net/context"
)
const (
ServiceName = "catalogue"
)
var (
HTTPLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: prometheus.DefBuckets,
}, []string{"method", "path", "status_code", "isWS"})
)
func init() {
prometheus.MustRegister(HTTPLatency)
}
func main() {
var (
port = flag.String("port", "80", "Port to bind HTTP listener") // TODO(pb): should be -addr, default ":80"
images = flag.String("images", "./images/", "Image path")
dsn = flag.String("DSN", "catalogue_user:default_password@tcp(catalogue-db:3306)/socksdb", "Data Source Name: [username[:password]@][protocol[(address)]]/dbname")
zip = flag.String("zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
)
flag.Parse()
fmt.Fprintf(os.Stderr, "images: %q\n", *images)
abs, err := filepath.Abs(*images)
fmt.Fprintf(os.Stderr, "Abs(images): %q (%v)\n", abs, err)
pwd, err := os.Getwd()
fmt.Fprintf(os.Stderr, "Getwd: %q (%v)\n", pwd, err)
files, _ := filepath.Glob(*images + "/*")
fmt.Fprintf(os.Stderr, "ls: %q\n", files) // contains a list of all files in the current directory
// Mechanical stuff.
errc := make(chan error)
ctx := context.Background()
// Log domain.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
}
var tracer stdopentracing.Tracer
{
if *zip == "" {
tracer = stdopentracing.NoopTracer{}
} else {
// 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()
logger := log.NewContext(logger).With("tracer", "Zipkin")
logger.Log("addr", zip)
collector, err := zipkin.NewHTTPCollector(
*zip,
zipkin.HTTPLogger(logger),
)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
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)
}
// Data domain.
db, err := sqlx.Open("mysql", *dsn)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
defer db.Close()
// Check if DB connection can be made, only for logging purposes, should not fail/exit
err = db.Ping()
if err != nil {
logger.Log("Error", "Unable to connect to Database", "DSN", dsn)
}
// Service domain.
var service catalogue.Service
{
service = catalogue.NewCatalogueService(db, logger)
service = catalogue.LoggingMiddleware(logger)(service)
}
// Endpoint domain.
endpoints := catalogue.MakeEndpoints(service, tracer)
// HTTP router
router := catalogue.MakeHTTPHandler(ctx, endpoints, *images, logger, tracer)
httpMiddleware := []middleware.Interface{
middleware.Instrument{
Duration: HTTPLatency,
RouteMatcher: router,
},
}
// Handler
handler := middleware.Merge(httpMiddleware...).Wrap(router)
// Create and launch the HTTP server.
go func() {
logger.Log("transport", "HTTP", "port", *port)
errc <- http.ListenAndServe(":"+*port, handler)
}()
// 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)
}