marcusirgens / jsonhandler

http.Handler with automatic JSON marshalling

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jsonhandler

Go Reference

The jsonhandler library creates JSON-speaking http.Handlers super fast.

Installation

go get -u github.com/marcusirgens/jsonhandler

Usage

Here's a webserver that greets users:

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/marcusirgens/jsonhandler"
)

type Args struct {
	Name string `json:"name"`
}

type Result struct {
	Greeting string `json:"greeting"`
}

// JSONHandler responds to requests.
func JSONHandler(ctx context.Context, args Args) Result {
	return Result{
		Greeting: fmt.Sprintf("Hello, %s", args.Name),
	}
}

func main() {
	http.Handle("/greet", jsonhandler.NewHandler(JSONHandler))
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Running this, we can make a call to verify that everything works as expected:

$ curl -ifd '{"name": "World"}' "http://localhost:8080/greet"                                                        

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Tue, 03 Aug 2021 19:25:27 GMT
Content-Length: 33

{
  "greeting": "Hello, World"
}

Contributing

Create a pull request.

Thanks

License

MIT

About

http.Handler with automatic JSON marshalling

License:MIT License


Languages

Language:Go 100.0%