前端框架选型是企业提升开发效率与用户体验的关键因素
1194
2022-10-25
go-restful 是一个 Golang 开发的 RESTful 框架
go-restful
package for building REST-style Web Services using Google Go
Code examples
REST asks developers to use HTTP methods explicitly and in a way that's consistent with the protocol definition. This basic REST design principle establishes a one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods. According to this mapping:
GET = Retrieve a representation of a resourcePOST = Create if you are sending content to the server to create a subordinate of the specified resource collection, using some server-side algorithm.PUT = Create if you are sending the full content of the specified resource (URI).PUT = Update if you are updating the full content of the specified resource.DELETE = Delete if you are requesting the server to delete the resourcePATCH = Update partial content of a resourceOPTIONS = Get information about the communication options for the request URI
Usage
Without Go Modules
All versions up to v2.*.* (on the master) are not supporting Go modules.
import ( restful "github.com/emicklei/go-restful")
Using Go Modules
As of version v3.0.0 (on the v3 branch), this package supports Go modules.
import ( restful "github.com/emicklei/go-restful/v3")
Example
ws := new(restful.WebService)ws. Path("/users"). Consumes(restful.MIME_XML, restful.MIME_jsON). Produces(restful.MIME_JSON, restful.MIME_XML)ws.Route(ws.GET("/{user-id}").To(u.findUser). Doc("get a user"). Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")). Writes(User{})) ... func (u UserResource) findUser(request *restful.Request, response *restful.Response) { id := request.PathParameter("user-id") ...}
Full API of a UserResource
Features
Routes for request → function mapping with path parameter (e.g. {id} but also prefix_{var} and {var}_suffix) supportConfigurable router: (default) Fast routing algorithm that allows static elements, google custom method, regular expressions and dynamic parameters in the URL path (e.g. /resource/name:customVerb, /meetings/{id} or /static/{subpath:*})Routing algorithm after JSR311 that is implemented using (but does not accept) regular expressions Request API for reading structs from JSON/XML and accesing parameters (path,query,header)Response API for writing structs to JSON/XML and setting headersCustomizable encoding using EntityReaderWriter registrationFilters for intercepting the request → response flow on Service or Route levelRequest-scoped variables using attributesContainers for WebServices on different HTTP endpointsContent encoding (gzip,deflate) of request and response payloadsAutomatic responses on OPTIONS (using a filter)Automatic CORS request handling (using a filter)API declaration for Swagger UI (go-restful-openapi, see go-restful-swagger12)Panic recovery to produce HTTP 500, customizable using RecoverHandler(...)Route errors produce HTTP 404/405/406/415 errors, customizable using ServiceErrorHandler(...)Configurable (trace) loggingCustomizable gzip/deflate readers and writers using CompressorProvider registration
How to customize
There are several hooks to customize the behavior of the go-restful package.
Router algorithmPanic recoveryJSON decoderTrace loggingCompressionEncoders for other serializersUse jsoniter by build this package using a tag, e.g. go build -tags=jsoniter .
TODO: write examples of these.
Resources
Example posted on blogDesign explained on blogsourcegraphshowcase: Zazkia - tcp proxy for testing resiliencyshowcase: Mora - MongoDB REST Api server
Type git shortlog -s for a full list of contributors.
© 2012 - 2020, http://ernestmicklei.com. MIT License. Contributions are welcome.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~