|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/signal" |
| 11 | + "time" |
| 12 | + |
| 13 | + calc "goa.design/goa/examples/calc" |
| 14 | + calcsvc "goa.design/goa/examples/calc/gen/calc" |
| 15 | + calcsvcsvr "goa.design/goa/examples/calc/gen/http/calc/server" |
| 16 | + openapisvr "goa.design/goa/examples/calc/gen/http/openapi/server" |
| 17 | + goahttp "goa.design/goa/http" |
| 18 | + "goa.design/goa/http/middleware/debugging" |
| 19 | + "goa.design/goa/http/middleware/logging" |
| 20 | + "goa.design/goa/http/middleware/tracing" |
| 21 | + "goa.design/goa/http/middleware/xray" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + // Define command line flags, add any other flag required to configure |
| 26 | + // the service. |
| 27 | + var ( |
| 28 | + addr = flag.String("listen", ":8080", "HTTP listen `address`") |
| 29 | + daemon = flag.String("daemon", "127.0.0.1:2000", "X-Ray daemon address") |
| 30 | + dbg = flag.Bool("debug", false, "Log request and response bodies") |
| 31 | + ) |
| 32 | + flag.Parse() |
| 33 | + |
| 34 | + // Setup logger and goa log adapter. Replace logger with your own using |
| 35 | + // your log package of choice. The goa.design/middleware/logging/... |
| 36 | + // packages define log adapters for common log packages. |
| 37 | + var ( |
| 38 | + logger *log.Logger |
| 39 | + adapter logging.Adapter |
| 40 | + ) |
| 41 | + { |
| 42 | + logger = log.New(os.Stderr, "[calc] ", log.Ltime) |
| 43 | + adapter = logging.Adapt(logger) |
| 44 | + } |
| 45 | + |
| 46 | + // Create the structs that implement the services. |
| 47 | + var ( |
| 48 | + calcsvcSvc calcsvc.Service |
| 49 | + ) |
| 50 | + { |
| 51 | + calcsvcSvc = calc.NewCalc(logger) |
| 52 | + } |
| 53 | + |
| 54 | + // Wrap the services in endpoints that can be invoked from other |
| 55 | + // services potentially running in different processes. |
| 56 | + var ( |
| 57 | + calcsvcEndpoints *calcsvc.Endpoints |
| 58 | + ) |
| 59 | + { |
| 60 | + calcsvcEndpoints = calcsvc.NewEndpoints(calcsvcSvc) |
| 61 | + } |
| 62 | + |
| 63 | + // Provide the transport specific request decoder and response encoder. |
| 64 | + // The goa http package has built-in support for JSON, XML and gob. |
| 65 | + // Other encodings can be used by providing the corresponding functions, |
| 66 | + // see goa.design/encoding. |
| 67 | + var ( |
| 68 | + dec = goahttp.RequestDecoder |
| 69 | + enc = goahttp.ResponseEncoder |
| 70 | + ) |
| 71 | + |
| 72 | + // Build the service HTTP request multiplexer and configure it to serve |
| 73 | + // HTTP requests to the service endpoints. |
| 74 | + var mux goahttp.Muxer |
| 75 | + { |
| 76 | + mux = goahttp.NewMuxer() |
| 77 | + } |
| 78 | + |
| 79 | + // Wrap the endpoints with the transport specific layers. The generated |
| 80 | + // server packages contains code generated from the design which maps |
| 81 | + // the service input and output data structures to HTTP requests and |
| 82 | + // responses. |
| 83 | + var ( |
| 84 | + openapiServer *openapisvr.Server |
| 85 | + calcsvcServer *calcsvcsvr.Server |
| 86 | + ) |
| 87 | + { |
| 88 | + openapiServer = openapisvr.New(nil, mux, dec, enc) |
| 89 | + calcsvcServer = calcsvcsvr.New(calcsvcEndpoints, mux, dec, enc) |
| 90 | + } |
| 91 | + |
| 92 | + // Configure the mux. |
| 93 | + openapisvr.Mount(mux) |
| 94 | + calcsvcsvr.Mount(mux, calcsvcServer) |
| 95 | + |
| 96 | + // Wrap the multiplexer with additional middlewares. Middlewares mounted |
| 97 | + // here apply to all the service endpoints. |
| 98 | + var handler http.Handler = mux |
| 99 | + { |
| 100 | + if *dbg { |
| 101 | + handler = debugging.New(mux, adapter)(handler) |
| 102 | + } |
| 103 | + handler = logging.New(adapter)(handler) |
| 104 | + xrayHndlr, err := xray.New("calc", *daemon) |
| 105 | + if err != nil { |
| 106 | + logger.Printf("[WARN] cannot connect to xray daemon %s: %s", *daemon, err) |
| 107 | + } |
| 108 | + // Wrap the Xray and the tracing handler. The order is very important. |
| 109 | + handler = xrayHndlr(handler) |
| 110 | + handler = tracing.New()(handler) |
| 111 | + } |
| 112 | + |
| 113 | + // Create channel used by both the signal handler and server goroutines |
| 114 | + // to notify the main goroutine when to stop the server. |
| 115 | + errc := make(chan error) |
| 116 | + |
| 117 | + // Setup interrupt handler. This optional step configures the process so |
| 118 | + // that SIGINT and SIGTERM signals cause the service to stop gracefully. |
| 119 | + go func() { |
| 120 | + c := make(chan os.Signal, 1) |
| 121 | + signal.Notify(c, os.Interrupt) |
| 122 | + errc <- fmt.Errorf("%s", <-c) |
| 123 | + }() |
| 124 | + |
| 125 | + // Start HTTP server using default configuration, change the code to |
| 126 | + // configure the server as required by your service. |
| 127 | + srv := &http.Server{Addr: *addr, Handler: handler} |
| 128 | + go func() { |
| 129 | + for _, m := range openapiServer.Mounts { |
| 130 | + logger.Printf("[INFO] service %q file %q mounted on %s %s", openapiServer.Service(), m.Method, m.Verb, m.Pattern) |
| 131 | + } |
| 132 | + for _, m := range calcsvcServer.Mounts { |
| 133 | + logger.Printf("[INFO] service %q method %q mounted on %s %s", calcsvcServer.Service(), m.Method, m.Verb, m.Pattern) |
| 134 | + } |
| 135 | + logger.Printf("[INFO] listening on %s", *addr) |
| 136 | + errc <- srv.ListenAndServe() |
| 137 | + }() |
| 138 | + |
| 139 | + // Wait for signal. |
| 140 | + logger.Printf("exiting (%v)", <-errc) |
| 141 | + |
| 142 | + // Shutdown gracefully with a 30s timeout. |
| 143 | + ctx, _ := context.WithTimeout(context.Background(), 30*time.Second) |
| 144 | + srv.Shutdown(ctx) |
| 145 | + |
| 146 | + logger.Println("exited") |
| 147 | +} |
0 commit comments