在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:goadesign/goa开源软件地址:https://github.com/goadesign/goa开源编程语言:Go 99.8%开源软件介绍:OverviewGoa takes a different approach to building services by making it possible to describe the design of the service API using a simple Go DSL. Goa uses the description to generate specialized service helper code, client code and documentation. Goa is extensible via plugins, for example the goakit plugin generates code that leverage the Go kit library. The service design describes the transport independent layer of the services in the form of simple methods that accept a context and a payload and return a result and an error. The design also describes how the payloads, results and errors are serialized in the transport (HTTP or gRPC). For example a service method payload may be built from an HTTP request by extracting values from the request path, headers and body. This clean separation of layers makes it possible to expose the same service using multiple transports. It also promotes good design where the service business logic concerns are expressed and implemented separately from the transport logic. The Goa DSL consists of Go functions so that it may be extended easily to avoid repetition and promote standards. The design code itself can easily be shared across multiple services by simply importing the corresponding Go package again promoting reuse and standardization across services. Code GenerationThe Goa tool accepts the Go design package import path as input and produces the interface as well as the glue that binds the service and client code with the underlying transport. The code is specific to the API so that for example there is no need to cast or "bind" any data structure prior to using the request payload or response result. The design may define validations in which case the generated code takes care of validating the incoming request payload prior to invoking the service method on the server, and validating the response prior to invoking the client code. Installationgo install goa.design/goa/v3/cmd/goa@v3
Current Release: Teaser1. DesignCreate a new Goa project: mkdir -p calcsvc/design
cd calcsvc
go mod init calcsvc Create the file package design
import . "goa.design/goa/v3/dsl"
// API describes the global properties of the API server.
var _ = API("calc", func() {
Title("Calculator Service")
Description("HTTP service for multiplying numbers, a goa teaser")
Server("calc", func() {
Host("localhost", func() { URI("http://localhost:8088") })
})
})
// Service describes a service
var _ = Service("calc", func() {
Description("The calc service performs operations on numbers")
// Method describes a service method (endpoint)
Method("multiply", func() {
// Payload describes the method payload
// Here the payload is an object that consists of two fields
Payload(func() {
// Attribute describes an object field
Attribute("a", Int, "Left operand")
Attribute("b", Int, "Right operand")
// Both attributes must be provided when invoking "multiply"
Required("a", "b")
})
// Result describes the method result
// Here the result is a simple integer value
Result(Int)
// HTTP describes the HTTP transport mapping
HTTP(func() {
// Requests to the service consist of HTTP GET requests
// The payload fields are encoded as path parameters
GET("/multiply/{a}/{b}")
// Responses use a "200 OK" HTTP status
// The result is encoded in the response body
Response(StatusOK)
})
})
}) This file contains the design for a 2. ImplementNow that the design is done, let's run goa gen calcsvc/design This produces a
The
The tool generated the Let's implement our service by providing a proper implementation for the // Multiply returns the multiplied value of attributes a and b of p.
func (s *calcsrvc) Multiply(ctx context.Context, p *calc.MultiplyPayload) (res int, err error) {
return p.A * p.B, nil
} That's it! we have now a full-fledged HTTP service with a corresponding OpenAPI specification and a client tool. 3. RunNow let's compile and run the service: cd cmd/calc
go build
./calc
[calcapi] 16:10:47 HTTP "Multiply" mounted on GET /multiply/{a}/{b}
[calcapi] 16:10:47 HTTP server listening on "localhost:8088" Open a new console and compile the generated CLI tool: cd calcsvc/cmd/calc-cli
go build and run it: ./calc-cli calc multiply -a 2 -b 3
6 The tool includes contextual help: ./calc-cli --help Help is also available on each command: ./calc-cli calc multiply --help Now let's see how robust our code is and try to use non integer values: ./calc-cli calc multiply -a 1 -b foo
invalid value for b, must be INT
run './calccli --help' for detailed usage. The generated code validates the command line arguments against the types defined in the design. The server also validates the types when decoding incoming requests so that your code only has to deal with the business logic. 4. DocumentThe The specification can easily be served from the service itself using a file
server. The Files DSL
function makes it possible to serve a static file. Edit the file
var _ = Service("openapi", func() {
// Serve the file gen/http/openapi3.json for requests sent to
// /openapi.json. The HTTP file system is created below.
Files("/openapi.json", "openapi3.json")
}) Re-run All we need to do is mount the handler on the service mux. Add the corresponding
import statement to import openapisvr "calcsvc/gen/http/openapi/server" and mount the handler by adding the following line in the same file and after
the mux creation (e.g. one the line after the svr := openapisvr.New(nil, mux, dec, enc, nil, nil, http.Dir("../../gen/http"))
openapisvr.Mount(mux, svr) That's it! we now have a self-documenting service. Stop the running service
with CTRL-C. Rebuild and re-run it then make requests to the newly added
^C[calcapi] 16:17:37 exiting (interrupt)
[calcapi] 16:17:37 shutting down HTTP server at "localhost:8088"
[calcapi] 16:17:37 exited
go build
./calc In a different console: curl localhost:8088/openapi.json
{"openapi":"3.0.3","info":{"title":"Calculator Service","description":... ResourcesDocsThe goa.design website provides a high level overview of Goa and the DSL. In particular the page Implementing a Goa Service explains how to leverage the generated code to implement an HTTP or gRPC service. The contains a fully documented reference of all the DSL functions. Instrumentation and System ExampleThe clue project provides observability packages that work in tandem with Goa. The packages cover logging, tracing, metrics, health checks and service client mocking. clue also includes a fully featured example consisting of three instrumented Goa microservices that communicate with each other. Getting Started GuidesA couple of Getting Started guides produced by the community. Joseph Ocol from Pelmorex Corp. goes through a complete example writing a server and client service using both HTTP and gRPC transports. Gleidson Nascimento goes through how to create a complete service that using both CORS and JWT based authentication to secure access. ExamplesThe examples directory contains simple examples illustrating basic concepts. TroubleshootingQ: I'm seeing an error that says:
How do I fix this? A: If you are vendoring your dependencies Goa will not attempt to satisfy its
dependencies by retrieving them with To fix, ensure that ContributingSee CONTRIBUTING. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论