Go-restful Usage

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Preface

The Go language is a very powerful language to express. There is currently a restful webservice package implemented by Golang, go-restful is simple to use.

2.Demo

Example implementation of a query operation, a more detailed demo see here: Https://github.com/emicklei/go-restful/blob/master/examples/restful-user-resource.go

Package Mainimport ("Log" "net/http"//"StrConv" "github.com/emicklei/go-restful" "github.com/emicklei/go-restful/ Swagger ") type User struct {id,name string}type userresource struct {users map[string]user}func (U userresource) Register ( Container *restful. Container) {WS: = new (restful. WebService) ws. Path ("/users"). Doc ("Manage Users"). Consumes (restful. Mime_xml, restful. Mime_json). Produces (restful. Mime_json, restful. Mime_xml) ws. Route (ws. GET ("/{user-id}"). to (U.finduser).//Docsdoc ("Get a User"). Operation ("Finduser"). Param (ws. Pathparameter ("User-id", "identifier of the user"). DataType ("string")). Writes (user{}))//on the Responsecontainer. The ADD (WS)}func (U userresource) finduser (Request *restful. Request, Response *restful. Response) {id: = Request. Pathparameter ("User-id") usr: = U.users[id]if len (usr. Id) = = 0 {Response. AddHeader ("Content-type", "Text/plain") response. Writeerrorstring (http. Statusnotfound, "404:user could not be found.") Return}response. Writeentity (USR)}func main () {wscontainer: = restFul. Newcontainer () UserMap: = map[string]user{"1": user{"1", "Tom"}, "2": user{"2", "Jerry"},}u: = Userresource{usermap}u. Register (wscontainer) Config: = swagger. Config{webservices:wscontainer.registeredwebservices (),//control what Services is Visiblewebservicesurl: "http: localhost:8080 ", Apipath:"/apidocs.json ",//Optionally, specifiy where the UI is Locatedswaggerpath:"/apidoc S/", Swaggerfilepath:"/users/emicklei/xprojects/swagger-ui/dist "}swagger. Registerswaggerservice (config, wscontainer) log. Printf ("Start listening on localhost:8080") Server: = &http. SERVER{ADDR: "8080", Handler:wscontainer}log. Fatal (server. Listenandserve ())}
3. Summary

Golang in the Go-restful library in the k8s as a apiserver rest framework to achieve the docking of routing and handler, the function is perfect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.