Skip to content

Instantly share code, notes, and snippets.

@ikawaha
Created June 13, 2019 00:28
Show Gist options
  • Save ikawaha/7317274ea5bbf0dd2d51c22cf6240b28 to your computer and use it in GitHub Desktop.
Save ikawaha/7317274ea5bbf0dd2d51c22cf6240b28 to your computer and use it in GitHub Desktop.
package design
import (
. "goa.design/goa/v3/dsl"
cors "goa.design/plugins/v3/cors/dsl"
)
var _ = API("calc", func() {
Title("Calculator Service")
Description("Service for adding numbers, a Goa teaser")
Server("calc", func() {
Host("localhost", func() {
URI("http://localhost:8000")
URI("grpc://localhost:8080")
})
})
cors.Origin("/.*localhost.*/", func() {
cors.Headers("Content-Type", "api_key", "Authorization")
cors.Methods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS")
cors.MaxAge(600)
})
})
var _ = Service("calc", func() {
Description("The calc service performs operations on numbers.")
Method("add", func() {
Payload(func() {
Field(1, "a", Int, "Left operand")
Field(2, "b", Int, "Right operand")
Required("a", "b")
})
Result(Int)
HTTP(func() {
GET("/add/{a}/{b}")
})
GRPC(func() {
})
})
})
var _ = Service("calc2", func() {
Description("The calc service performs operations on numbers.")
Method("add2", func() {
Payload(func() {
Field(1, "a", Int, "Left operand")
Field(2, "b", Int, "Right operand")
Required("a", "b")
})
Result(Int)
HTTP(func() {
GET("/add2/{a}/{b}")
})
GRPC(func() {
})
})
})
var _ = Service("swagger", func(){
Files("/openapi.json", "./gen/http/openapi.json", func(){
Meta("swagger:generate", "false")
})
})
@ikawaha
Copy link
Author

ikawaha commented Jun 13, 2019

calc/cmd/calc/http.go

package main

import (
	calc "calc/gen/calc"
	calc2 "calc/gen/calc2"
	calcsvr "calc/gen/http/calc/server"
	calc2svr "calc/gen/http/calc2/server"
	swaggersvr "calc/gen/http/swagger/server"
	"context"
	"log"
	"net/http"
	"net/url"
	"os"
	"sync"
	"time"

	goahttp "goa.design/goa/v3/http"
	httpmdlwr "goa.design/goa/v3/http/middleware"
	"goa.design/goa/v3/middleware"
)

// handleHTTPServer starts configures and starts a HTTP server on the given
// URL. It shuts down the server if any error is received in the error channel.
func handleHTTPServer(ctx context.Context, u *url.URL, calcEndpoints *calc.Endpoints, calc2Endpoints *calc2.Endpoints, wg *sync.WaitGroup, errc chan error, logger *log.Logger, debug bool) {

	// Setup goa log adapter.
	var (
		adapter middleware.Logger
	)
	{
		adapter = middleware.NewLogger(logger)
	}

	// Provide the transport specific request decoder and response encoder.
	// The goa http package has built-in support for JSON, XML and gob.
	// Other encodings can be used by providing the corresponding functions,
	// see goa.design/encoding.
	var (
		dec = goahttp.RequestDecoder
		enc = goahttp.ResponseEncoder
	)

	// Build the service HTTP request multiplexer and configure it to serve
	// HTTP requests to the service endpoints.
	var mux goahttp.Muxer
	{
		mux = goahttp.NewMuxer()
	}

	// Wrap the endpoints with the transport specific layers. The generated
	// server packages contains code generated from the design which maps
	// the service input and output data structures to HTTP requests and
	// responses.
	var (
		calcServer    *calcsvr.Server
		calc2Server   *calc2svr.Server
		swaggerServer *swaggersvr.Server
	)
	{
		eh := errorHandler(logger)
		calcServer = calcsvr.New(calcEndpoints, mux, dec, enc, eh)
		calc2Server = calc2svr.New(calc2Endpoints, mux, dec, enc, eh)
		swaggerServer = swaggersvr.New(nil, mux, dec, enc, eh)
	}
	// Configure the mux.
	calcsvr.Mount(mux, calcServer)
	calc2svr.Mount(mux, calc2Server)
	swaggersvr.Mount(mux)

	// Wrap the multiplexer with additional middlewares. Middlewares mounted
	// here apply to all the service endpoints.
	var handler http.Handler = mux
	{
		if debug {
			handler = httpmdlwr.Debug(mux, os.Stdout)(handler)
		}
		handler = httpmdlwr.Log(adapter)(handler)
		handler = httpmdlwr.RequestID()(handler)
	}

	// Start HTTP server using default configuration, change the code to
	// configure the server as required by your service.
	srv := &http.Server{Addr: u.Host, Handler: handler}
	for _, m := range calcServer.Mounts {
		logger.Printf("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
	}
	for _, m := range calc2Server.Mounts {
		logger.Printf("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
	}
	for _, m := range swaggerServer.Mounts {
		logger.Printf("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
	}

	(*wg).Add(1)
	go func() {
		defer (*wg).Done()

		// Start HTTP server in a separate goroutine.
		go func() {
			logger.Printf("HTTP server listening on %q", u.Host)
			errc <- srv.ListenAndServe()
		}()

		<-ctx.Done()
		logger.Printf("shutting down HTTP server at %q", u.Host)

		// Shutdown gracefully with a 30s timeout.
		ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
		defer cancel()

		srv.Shutdown(ctx)
	}()
}

// errorHandler returns a function that writes and logs the given error.
// The function also writes and logs the error unique ID so that it's possible
// to correlate.
func errorHandler(logger *log.Logger) func(context.Context, http.ResponseWriter, error) {
	return func(ctx context.Context, w http.ResponseWriter, err error) {
		id := ctx.Value(middleware.RequestIDKey).(string)
		w.Write([]byte("[" + id + "] encoding: " + err.Error()))
		logger.Printf("[%s] ERROR: %s", id, err.Error())
	}
}

@ikawaha
Copy link
Author

ikawaha commented Jun 13, 2019

		{"server-hosting-service-with-file-server", ctestdata.ServerHostingServiceWithFileServerDSL, testdata.ServerHostingServiceWithFileServerHandlerCode},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment