• Stars
    star
    4,871
  • Rank 8,625 (Top 0.2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

package for building REST-style Web Services using Go

go-restful

package for building REST-style Web Services using Google Go

Build Status Go Report Card GoDoc 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, 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) logging
  • Customizable gzip/deflate readers and writers using CompressorProvider registration
  • Inject your own http.Handler using the HttpMiddlewareHandlerToFilter function

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 jsoniter by building this package using a build tag, e.g. go build -tags=jsoniter .
  • Use the variable MergePathStrategy to change the behaviour of composing the Route path given a root path and a local route path
    • versions >= 3.10.1 has set the value to PathJoinStrategy that fixes a reported security issue but may cause your services not to work correctly anymore.
    • versions <= 3.9 had the behaviour that can be restored in newer versions by setting the value to TrimSlashStrategy.
    • you can set value to a custom implementation (must implement MergePathStrategyFunc)

Resources

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

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

More Repositories

1

proto

parser for Google ProtocolBuffers definition
Go
577
star
2

mora

MongoDB generic REST server in Go
Go
315
star
3

dot

Go package for writing descriptions using the Graphviz DOT and Mermaid language
Go
268
star
4

hopwatch

webbased debugging for Go programs
Go
258
star
5

melrose

interactive programming of melodies, producing MIDI
Go
179
star
6

go-restful-openapi

OpenAPI extension in Go for the go-restful package
Go
133
star
7

forest

REST Api Testing package for writing integration tests in Go
Go
123
star
8

hazana

package to build load tests for services (http, gRPC, tcp) by implementing an Attacker
Go
73
star
9

zazkia

tcp proxy to simulate connection problems
Go
69
star
10

rango

a REPL program for the Go language
Go
56
star
11

go-restful-swagger12

Swagger 1.2 extension to the go-restful package
Go
50
star
12

rendersnake

Java library for creating components and pages that produce HTML using only Java
Java
36
star
13

gmig

Google Cloud Platform migrations tool for infrastructure-as-code
Go
27
star
14

proto-contrib

contributed tools and other packages on top of the Go proto package
Go
24
star
15

pgtalk

generics package and code generator to access PostgreSQL tables and views on top of pgx
Go
20
star
16

protobuf2map

Decoder for a ProtocolBuffers message that produces a Go map[string]interface{}
Go
15
star
17

eventbus

Eventbus, a lightweight in-app event communication bus for Go
Go
15
star
18

gws

command line tool for using the Google Workspace Admin (formerly GSuite)
Go
14
star
19

swagger2

Go implementation of the Swagger 2.0 specification
Go
12
star
20

v8dispatcher

Go package that adds a message layer on top of v8worker (binding for the Javascript V8 engine)
Go
11
star
21

critter

http proxy for testing purposes
Java
9
star
22

licenser

tool to put a copyright notice header in source files
Go
9
star
23

landskape

general purpose webservice for the registation of applications and their connections
Go
8
star
24

renderbee

package for creating HTML renderable components in Go
Go
8
star
25

artreyu

simple tool for artifact management
Go
7
star
26

validate

simple validation library for Go
Go
7
star
27

parzello

Parzello is a delay and retry service on top of Google Pub Sub to publish messages with a time delay to a topic.
Go
6
star
28

karina

realtime image-resizing service in Go
Go
6
star
29

go-selfdiagnose

package for health checking a Go application (simplified port of its Java implementation)
Go
6
star
30

tviewplus

package that adds extra components to build Terminal applications on top of https://github.com/rivo/tview
Go
5
star
31

talks

public collection of presentations about different topics using different tools and languages
Go
5
star
32

https-proxy

https reverse proxy (with x-forwarded-* headers)
Go
4
star
33

xconnect

exploring network connectivity in a microservice landscape
Go
4
star
34

genika

collection of code generation tools
Go
4
star
35

mxgraph

package in Go for writing mxgraph XML models
Go
3
star
36

tokenman

small Go library for JWT token management
Go
3
star
37

gcloudx

extra features for accessing the Google Cloud Platform
Go
3
star
38

go-bitbucket

client package to access the bitbucket.org REST api 2.0
Go
3
star
39

tre

Go package for tracing an error in the call stack
Go
3
star
40

graphql-client-gen

A schema-driven GraphQL zero-dependencies client generator for Go
Go
3
star
41

simone

package for building backends to simone_fui
Go
2
star
42

hazana-grafana-monitoring

Hazana extension that sends metrics to Graphite during the execution of the load test
Go
2
star
43

gioui-mdi

experiment with MDI support for Gio UI
Go
2
star
44

dollar

The $1 Unistroke Recognizer (Go version)
JavaScript
2
star
45

zenna

Go packages for 2D and 3D scenes and animations, to render by external programs. (Port from my older Smalltalk packages PhilemonSmallScript3D and Zenna)
Go
2
star
46

assert

writing unit tests should be simple, require minimal effort and be helpful in explaining why they fail
Go
2
star
47

dgraph-access

helper package on top of the official Go client for DGraph
Go
2
star
48

dgraph-parser

DGraph schema parser
Go
1
star
49

atlas

Atlassian provisioning tool
Go
1
star
50

gdep

tool to explore dependencies of resources in the Google Cloud Platform
Go
1
star
51

gspeech

simple tool that uses the Text-to-Speech API in the Google Cloud Platform
Go
1
star
52

log15-stackdriver-handler

logging handler for log15 that uses the StackDriver Logging API of the Google Cloud Platform
Go
1
star
53

moqi

mgo query interceptor (mgo is the MongoDB driver for the Go language)
Go
1
star
54

gcppub

additional tooling for Google Cloud Pub/Sub
Go
1
star
55

artreyu-nexus

plugin for artreyu, tool for artifact assemly, that uses a Sonatype Nexus repository
Go
1
star
56

xconnect-store

microservice for persisting xconnect configurations
Go
1
star
57

melrose-osx

build scripts to create a Mac OSX package
Makefile
1
star
58

kiya-webapp

Web UI for kiya for GCP deployment
Go
1
star
59

minio-probe

minio / probe - a simple mechanism to trace and return errors in large programs.
Go
1
star
60

htmllog

a simple logger that safely produces an append-only, auto-scrolling-on-refresh HTML file.
Go
1
star
61

artreyu-google-cloud-storage

plugin for artreyu, an artifact assembly and storage tool
Go
1
star
62

v8worker-docker

Dockerfile for building v8worker based applications
1
star
63

tank

Java
1
star
64

selfdiagnose

a Java library of diagnotistic tasks that can verify the availability of external resources required for the execution of a J2EE application.
Java
1
star
65

nanny

Records a sliding window of Go slog events with all attribute values for remote inspection through HTTP.
Go
1
star