gohandlr

command module
v0.0.0-...-e0086ea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 1 Imported by: 0

README

gohandlr

gohandlr is a simple and intuitive to streamline the creation of HTTP handlers. It allows developers to focus on the essential data passing in and out of the handler, while providing standard interfaces for common tasks such as parsing request bodies, parameters, and response writers.

Features

  • Simplified HTTP handler creation
  • Automatic parsing of request bodies and parameters
  • Extensible options for customizing request handling
  • Support for multiple content types and response formats
  • Easy integration with existing Go HTTP libraries

Installation

To install gohandlr, use the following command:

go get github.com/epentland/gohandlr

Usage

Here's a simple example of how to use gohandlr to create an HTTP handler:

package main

import (
	"context"
	"net/http"
	"text/template"

	"github.com/epentland/gohandlr"
	"github.com/epentland/gohandlr/options"
)

type User struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

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

type HandleUserParams struct {
	Id  int `path:"id"`
	Age int `query:"age"`
}

func HandleUserRequest(ctx context.Context, body HandleUserBody, params HandleUserParams) (User, error) {
	// Do some processing
	user := User{
		Name: body.Name,
		Age:  params.Age,
	}
	return user, nil
}

func HandleNoBody(ctx context.Context, body gohandlr.Nil, params gohandlr.Nil) (gohandlr.Nil, error) {
	// Do write some processing that doesn't require a return value
	return gohandlr.Nil{}, nil
}

func main() {
	// Create a html template for rendering web pages
	tmplString := "<html><body>Hello, {{.Name}}, you are {{.Age}} years old!</body></html>"
	tmpl, err := template.New("index.html").Parse(tmplString)
	if err != nil {
		panic(err)
	}

	mux := http.NewServeMux()

    	// Works with any router
	gohandlr.Handle(mux.HandleFunc, "POST /user/{id}", HandleUserRequest,
		options.WithDefaults(),
		options.WithJsonWriter(),
		options.WithHTMLTemplateWriter(tmpl, "index.html"),
	)

    	gohandlr.Handle(mux.HandleFunc, "PUT /user", HandleNoBody, options.WithDefaults())

	err = http.ListenAndServe(":8080", mux)
	if err != nil {
		panic(err)
	}
}

In this example, we define a User struct to represent the response data, a HandleUserBody struct for the request body, and a HandleUserParams struct for the request parameters. The HandleUserRequest function is the actual HTTP handler, which takes the request body and parameters as input and returns a User instance.

To create the handler, we use the gohandlr.Handle function, passing in the mux.HandleFunc to register the handler, the HTTP method and path, the handler function, and any additional options (in this case, options.WithDefaults()).

If you don't need to use the request body, parameters, or return value, you can use the gohandlr.Nil type as a placeholder.

Options

gohandlr provides a set of options to customize the behavior of the HTTP handler:

  • options.WithDefaults(): Applies default options suitable for most use cases.
  • options.WithJsonWriter(): Enables JSON response writing.
  • options.WithHTMLTemplateWriter(tmpl, name): Enables HTML template rendering for responses.
  • options.WithJSONBodyReader(): Reads the json body.
  • options.WithParamsReader(): Reads the path, query, and context params using reflection.

You can create your own options by implementing the appropriate interfaces:

  • BodyReader: For parsing request bodies
  • ParamsReader: For parsing request parameters
  • Writer: For writing response data
package options

import "net/http"

type BodyReader interface {
	Reader(*http.Request, any) error
	ContentType() string
}

type ParamsReader interface {
	Reader(*http.Request, any) error
}

type Writer interface {
	Write(http.ResponseWriter, *http.Request, any) error
	Accept() string
}

Contributing

Contributions to gohandlr are welcome! If you find a bug, have a feature request, or want to contribute code, please open an issue or submit a pull request on the GitHub repository.

License

gohandlr is released under the MIT License.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
examples
hello_world command
hello_world/handlr
Code generated by gohandlr.
Code generated by gohandlr.
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL