/
main.go
182 lines (164 loc) · 4.53 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"strings"
"sync"
calcapi "github.com/DataDog/trace-examples/go/goa-basic"
calc "github.com/DataDog/trace-examples/go/goa-basic/gen/calc"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
func main() {
tracer.Start(tracer.WithServiceName("goa-basic"))
defer tracer.Stop()
// Define command line flags, add any other flag required to configure the
// service.
var (
hostF = flag.String("host", "development", "Server host (valid values: development, production)")
domainF = flag.String("domain", "", "Host domain name (overrides host domain specified in service design)")
httpPortF = flag.String("http-port", "", "HTTP port (overrides host HTTP port specified in service design)")
grpcPortF = flag.String("grpc-port", "", "gRPC port (overrides host gRPC port specified in service design)")
versionF = flag.String("version", "v1", "API version")
secureF = flag.Bool("secure", false, "Use secure scheme (https or grpcs)")
dbgF = flag.Bool("debug", false, "Log request and response bodies")
)
flag.Parse()
// Setup logger. Replace logger with your own log package of choice.
var (
logger *log.Logger
)
{
logger = log.New(os.Stderr, "[calcapi] ", log.Ltime)
}
// Initialize the services.
var (
calcSvc calc.Service
)
{
calcSvc = calcapi.NewCalc(logger)
}
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var (
calcEndpoints *calc.Endpoints
)
{
calcEndpoints = calc.NewEndpoints(calcSvc)
}
// Create channel used by both the signal handler and server goroutines
// to notify the main goroutine when to stop the server.
errc := make(chan error)
// Setup interrupt handler. This optional step configures the process so
// that SIGINT and SIGTERM signals cause the services to stop gracefully.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
errc <- fmt.Errorf("%s", <-c)
}()
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
// Start the servers and send errors (if any) to the error channel.
switch *hostF {
case "development":
{
addr := "http://localhost:8000/calc"
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
if *secureF {
u.Scheme = "https"
}
if *domainF != "" {
u.Host = *domainF
}
if *httpPortF != "" {
h := strings.Split(u.Host, ":")[0]
u.Host = h + ":" + *httpPortF
} else if u.Port() == "" {
u.Host += ":80"
}
handleHTTPServer(ctx, u, calcEndpoints, &wg, errc, logger, *dbgF)
}
{
addr := "grpc://localhost:8080"
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
if *secureF {
u.Scheme = "grpcs"
}
if *domainF != "" {
u.Host = *domainF
}
if *grpcPortF != "" {
h := strings.Split(u.Host, ":")[0]
u.Host = h + ":" + *grpcPortF
} else if u.Port() == "" {
u.Host += ":8080"
}
handleGRPCServer(ctx, u, calcEndpoints, &wg, errc, logger, *dbgF)
}
case "production":
{
addr := "https://{version}.goa.design/calc"
addr = strings.Replace(addr, "{version}", *versionF, -1)
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
if *secureF {
u.Scheme = "https"
}
if *domainF != "" {
u.Host = *domainF
}
if *httpPortF != "" {
h := strings.Split(u.Host, ":")[0]
u.Host = h + ":" + *httpPortF
} else if u.Port() == "" {
u.Host += ":443"
}
handleHTTPServer(ctx, u, calcEndpoints, &wg, errc, logger, *dbgF)
}
{
addr := "grpcs://{version}.goa.design"
addr = strings.Replace(addr, "{version}", *versionF, -1)
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
if *secureF {
u.Scheme = "grpcs"
}
if *domainF != "" {
u.Host = *domainF
}
if *grpcPortF != "" {
h := strings.Split(u.Host, ":")[0]
u.Host = h + ":" + *grpcPortF
} else if u.Port() == "" {
u.Host += ":8443"
}
handleGRPCServer(ctx, u, calcEndpoints, &wg, errc, logger, *dbgF)
}
default:
fmt.Fprintf(os.Stderr, "invalid host argument: %q (valid hosts: development|production)\n", *hostF)
}
// Wait for signal.
logger.Printf("exiting (%v)", <-errc)
// Send cancellation signal to the goroutines.
cancel()
wg.Wait()
logger.Println("exited")
}