Categorygithub.com/mbranch/jsonrest-go
modulepackage
1.0.0
Repository: https://github.com/mbranch/jsonrest-go.git
Documentation: pkg.go.dev

# README

jsonrest-go

Go Report Card GoDoc go.dev

Package jsonrest implements a microframework for writing RESTful web applications.

Endpoints are defined as:

func(ctx context.Context, req *jsonrest.Request) (interface{}, error)

If an endpoint returns a value along with a nil error, the value will be rendered to the client as JSON.

If an error is returned, it will be sanitized and returned to the client as json. Errors generated by a call to jsonrest.Error(status, code, message) will be rendered in the following form:

{
    "error": {
        "message": message,
        "code": code
    }
}

along with the given HTTP status code.

If the error returned implements HTTPErrorResponse (i.e. has a StatusCode() int method), it will be marshaled as-is to the client with the provided status code. Any other errors will be obfuscated to the caller (unless router.DumpError is enabled).

Example:

func main() {
    r := jsonrest.NewRouter()
    r.Use(logging)
    r.Get("/", hello)
}

func hello(ctx context.Context, req *jsonrest.Request) (interface{}, error) {
    return jsonrest.M{"message": "Hello, world"}, nil
}

func logging(next jsonrest.Endpoint) jsonrest.Endpoint {
    return func(ctx context.Context, req *jsonrest.Request) (interface{}, error) {
        start := time.Now()
        defer func() {
            log.Printf("%s (%v)\n", req.URL().Path, time.Since(start))
        }()
        return next(ctx, req)
    }
}

# Functions

BadRequest returns an HTTP 400 Bad Request error with a custom error message.
Error creates an error that will be rendered directly to the client.
NewRouter returns a new initialized Router.
NewTestRequest allows construction of a Request object with its internal members populated.
NotFound returns an HTTP 404 Not Found error with a custom error message.
Unauthorized returns an HTTP 401 Unauthorized error with a custom error message.
UnprocessableEntity returns an HTTP 422 UnprocessableEntity error with a custom error message.
WithCompressionEnabled is an Option available for NewRouter to configure gzip compression.
WithDisableJSONIndent is an Option available for NewRouter to configure JSON responses without indenting.
WithNotFoundHandler is an Option available for NewRouter to configure the not found handler.

# Constants

No description provided by the author
No description provided by the author

# Structs

HTTPError is an error that will be rendered to the client.
A Request represents a RESTful HTTP request received by the server.
Response is a type that can be returned by the endpoint for setting a custom HTTP success status code with the response body.
A Router is an http.Handler that routes incoming requests to registered endpoints.

# Interfaces

HTTPErrorResponse allows to customize the format of non-200 http responses.

# Type aliases

An Endpoint is an implementation of a RESTful endpoint.
M is a shorthand for map[string]interface{}.
Middleware is a function that wraps an endpoint to add new behaviour.
No description provided by the author
RouteMap is a map of a method-path pair to an endpoint.