/
main.go
119 lines (106 loc) · 3.23 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
package main
import (
"context"
"flag"
"fmt"
"net"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
"github.com/go-kit/log"
archiverapi "goa.design/plugins/v3/goakit/examples/fetcher/archiver"
archiver "goa.design/plugins/v3/goakit/examples/fetcher/archiver/gen/archiver"
health "goa.design/plugins/v3/goakit/examples/fetcher/archiver/gen/health"
)
func main() {
// Define command line flags, add any other flag required to configure the
// service.
var (
hostF = flag.String("host", "localhost", "Server host (valid values: localhost)")
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)")
secureF = flag.Bool("secure", false, "Use secure scheme (https or grpcs)")
dbgF = flag.Bool("debug", false, "Log request and response bodies")
)
flag.Parse()
// Setup gokit logger.
var (
logger log.Logger
)
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
// Initialize the services.
var (
archiverSvc archiver.Service
healthSvc health.Service
)
{
archiverSvc = archiverapi.NewArchiver(logger)
healthSvc = archiverapi.NewHealth(logger)
}
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var (
archiverEndpoints *archiver.Endpoints
healthEndpoints *health.Endpoints
)
{
archiverEndpoints = archiver.NewEndpoints(archiverSvc)
healthEndpoints = health.NewEndpoints(healthSvc)
}
// 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, syscall.SIGINT, syscall.SIGTERM)
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 "localhost":
{
addr := "http://localhost:80"
u, err := url.Parse(addr)
if err != nil {
logger.Log("fatal", fmt.Sprintf("invalid URL %#v: %s\n", addr, err))
os.Exit(1)
}
if *secureF {
u.Scheme = "https"
}
if *domainF != "" {
u.Host = *domainF
}
if *httpPortF != "" {
h, _, err := net.SplitHostPort(u.Host)
if err != nil {
logger.Log("fatal", fmt.Sprintf("invalid URL %#v: %s\n", u.Host, err))
os.Exit(1)
}
u.Host = net.JoinHostPort(h, *httpPortF)
} else if u.Port() == "" {
u.Host = net.JoinHostPort(u.Host, "80")
}
handleHTTPServer(ctx, u, archiverEndpoints, healthEndpoints, &wg, errc, logger, *dbgF)
}
default:
logger.Log("fatal", fmt.Sprintf("invalid host argument: %q (valid hosts: localhost)\n", *hostF))
os.Exit(1)
}
// Wait for signal.
logger.Log("info", fmt.Sprintf("exiting (%v)", <-errc))
// Send cancellation signal to the goroutines.
cancel()
wg.Wait()
logger.Log("info", "exited")
}