MCPcopy
hub / github.com/emicklei/go-restful

github.com/emicklei/go-restful @v3.13.0 sqlite

repository ↗ · DeepWiki ↗ · release v3.13.0 ↗
691 symbols 2,571 edges 79 files 370 documented · 54%
README

go-restful

package for building REST-style Web Services using Google Go

Go Report Card Go Reference codecov

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 resource
  • POST = 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 resource
  • PATCH = Update partial content of a resource
  • OPTIONS = 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) support
  • Configurable 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 accessing parameters (path,query,header)
  • Response API for writing structs to JSON/XML and setting headers
  • Customizable encoding using EntityReaderWriter registration
  • Filters for intercepting the request → response flow on Service or Route level
  • Request-scoped variables using attributes
  • Containers for WebServices on different HTTP endpoints
  • Content encoding (gzip,deflate) of request and response payloads
  • Automatic responses on OPTIONS (using a filter)
  • Automatic CORS request handling (using a filter)
  • API declaration for Swagger UI (go-restful-openapi)
  • Panic recovery to produce HTTP 500, customizable using RecoverHandler(...)
  • Route errors produce HTTP 404/405/406/415 errors, customizable using ServiceErrorHandler(...)
  • Configurable (trace) logging
  • Customizable gzip/deflate readers and writers using CompressorProvider registration
  • Inject your own http.Handler using the HttpMiddlewareHandlerToFilter function
  • Added SetPathTokenCacheEnabled and SetCustomVerbCacheEnabled to disable regexp caching (default=true)

How to customize

There are several hooks to customize the behavior of the go-restful package.

  • Router algorithm
  • Panic recovery
  • JSON decoder
  • Trace logging
  • Compression
  • Encoders for other serializers
  • Use the package variable TrimRightSlashEnabled (default true) to control the behavior of matching routes that end with a slash /

Resources

Type git shortlog -s for a full list of contributors.

© 2012 - 2023, http://ernestmicklei.com. MIT License. Contributions are welcome.

Extension points exported contracts — how you extend this code

EntityReaderWriter (Interface)
EntityReaderWriter can read and write values using an encoding such as JSON,XML. [4 implementers]
entity_accessors.go
RouteSelector (Interface)
A RouteSelector finds the best matching Route given the input HTTP Request RouteSelectors can optionally also implement [2 …
router.go
PathProcessor (Interface)
Copyright 2018 Ernest Micklei. All rights reserved. Use of this source code is governed by a license that can be found i [2 …
path_processor.go
CompressorProvider (Interface)
CompressorProvider describes a component that can provider compressors for the std methods. [2 implementers]
compressors.go
RouteReader (Interface)
Copyright 2021 Ernest Micklei. All rights reserved. Use of this source code is governed by a license that can be found i [1 …
route_reader.go
StdLogger (Interface)
StdLogger corresponds to a minimal subset of the interface satisfied by stdlib log.Logger [1 implementers]
log/log.go
FilterFunction (FuncType)
FilterFunction definitions must call ProcessFilter on the FilterChain to pass on the control and eventually call the Rou
filter.go
RouteFunction (FuncType)
RouteFunction declares the signature of a function that can be bound to a Route.
route.go

Core symbols most depended-on inside this repo

To
called by 120
route_builder.go
Route
called by 114
web_service.go
Path
called by 77
route_reader.go
Error
called by 74
response.go
GET
called by 72
web_service.go
Add
called by 38
container.go
Filter
called by 36
container.go
dispatch
called by 34
container.go

Shape

Function 335
Method 275
Struct 65
FuncType 7
Interface 6
TypeAlias 3

Languages

Go100%

Modules by API surface

web_service_test.go44 symbols
web_service.go40 symbols
route_builder.go34 symbols
parameter.go31 symbols
response.go26 symbols
container.go24 symbols
route_reader.go21 symbols
response_test.go21 symbols
jsr311.go20 symbols
jsr311_test.go19 symbols
filter_test.go18 symbols
entity_accessors.go17 symbols

Dependencies from manifests, versioned

github.com/emicklei/go-restful-openapi/v2v2.10.2 · 1×
github.com/ggicci/httpinv0.18.0 · 1×
github.com/ggicci/owlv0.8.2 · 1×
github.com/go-openapi/specv0.21.0 · 1×
github.com/gorilla/schemav1.3.0 · 1×
google.golang.org/appenginev1.6.8 · 1×
google.golang.org/protobufv1.33.0 · 1×
gopkg.in/check.v1v1.0.0-2020113013444 · 1×
gopkg.in/vmihailenco/msgpack.v2v2.9.2 · 1×

For agents

$ claude mcp add go-restful \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact