Coder Social home page Coder Social logo

jsonrest-go's Introduction

jsonrest-go

CircleCI 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)
    }
}

Contributing

Review the contributing guidelines.

jsonrest-go's People

Contributors

jeffreylo avatar johngibb avatar mathildathompson avatar mbranch avatar osadovy avatar raycchan avatar richcq avatar thoeni avatar waynesford avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.