gohttputil

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

README

go-httputil

Simple HTTP utilities like middleware, pragmatic route builders based on standard library facilities

Documentation

Overview

Example (Cors)

Example_cors sets up CORS preflight handler globally.

package main

import (
	"log"
	"net/http"

	gohttputil "github.com/asif-mahmud/go-httputil"
	"github.com/rs/cors"
)

func main() {
	// create mux instance
	m := gohttputil.New()

	// enable CORS handler globally with default
	// all allowed option
	m.EnableCORS()

	// or enable CORS handler with your own option
	m.EnableCORS(cors.Options{})

	// you can use the created mux now
	log.Fatal(http.ListenAndServe(":3000", m))

	// or you can use http.Server with handler set to the mux
	server := http.Server{
		Handler: m,
		Addr:    ":3000",
	}
	log.Fatal(server.ListenAndServe())
}
Output:

Example (Group)

Example_group shows grouping multiple routes

// create mux instance
m := gohttputil.New()

// define a group of routes
m.Group("/api/v1").
	// group level middlewares. these middlewares
	// will be applied to all routes defined in this group
	Use(groupMiddleware1).
	Use(groupMiddleware2).

	// define handlers for a route
	Route("/orders", func(rh gohttputil.RouteHandler) {
		rh.
			// no route level middleware will be applied to GET handler
			Get(handler1).

			// middleware1 will be applied to POST handler
			Use(middleware1).
			Post(handler1).

			// no middleware applied to PUT method handler
			Put(handler1)
	}).

	// define handlers for another route under same group
	Route("/users", func(rh gohttputil.RouteHandler) {
		rh.
			// no route level middleware will be applied to GET handler
			Get(handler1).

			// middleware1 will be applied to POST handler
			Use(middleware1).
			Post(handler1).

			// no middleware applied to PUT method handler
			Put(handler1)
	})
Output:

Example (SimpleRouting)

Example_simpleRouting shows defining routes

// create mux instance
m := gohttputil.New()

// add global middlewares.
// these middlewares will be applied to all routes.
m.
	Use(globalMiddleware1).
	Use(globalMiddleware2)

// define routes
m.Route("/api/order").
	// no middleware applied to GET method handler
	Get(handler1).

	// middleware1 and middleware2 are applied to POST method handler
	Use(middleware1).
	Use(middleware2).
	Post(handler2).

	// no middleware applied to PUT method handler
	Put(handler1).

	// only middleware1 applied to DELETE method handler
	Use(middleware1).
	Delete(handler2)

// you can use the created mux now
log.Fatal(http.ListenAndServe(":3000", m))

// or you can use http.Server with handler set to the mux
server := http.Server{
	Handler: m,
	Addr:    ":3000",
}
log.Fatal(server.ListenAndServe())
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group interface {
	// Use adds middlewares to group level middleware list.
	// These middlewares are applied to all routes defined under this group.
	// This acts like global middlewares but applied only for this group.
	Use(...Middleware) Group

	// Route creates a new RouteHandler under the current Group and calls GroupRouter with it.
	// This lets user to define one or more routes under the current Group.
	Route(string, GroupRouter) Group
}

Group defines group level routing methods.

type GroupRouter

type GroupRouter func(RouteHandler)

GroupRouter defines function signature for group level router.

type Grouper

type Grouper interface {
	Group(string) Group
}

Grouper defines interface to create a new Group.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware defines simple middleware signature

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux is a wrapper around the http.ServeMux. It provides a http.Handler implementation that basically calls http.ServeMux.ServeHTTP with or without CORS wrapper.

Any route handler attached through this Mux adds that route to the internal http.ServeMux by wrapping middlewares from different levels (global, group level or route level).

Global middlewares are added by Mux.Use method. These middlewares are applied to all routes defined by this instance of the Mux.

Group level middlewares are only applied to the group routes.

Route level middlewares are applied per route per method.

func New

func New() *Mux

New creates a new instance of Mux.

func (*Mux) EnableCORS added in v0.9.0

func (m *Mux) EnableCORS(opt ...cors.Options)

EnableCORS wraps the internal http.ServeMux with CORS handler. Without any option in argument, it allows all methods, origins and headers.

func (*Mux) Group

func (m *Mux) Group(prefix string) Group

Group implements Grouper.

func (*Mux) Route

func (r *Mux) Route(route string) RouteHandler

Route implements Router.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler. This calls the internal http.ServeMux.ServeHTTP with or without CORS wrapper handler.

func (*Mux) Use

func (r *Mux) Use(middlewares ...Middleware) *Mux

Use appends middlewares to global middlewares.

type RouteHandler

type RouteHandler interface {
	// Use adds middlewares to be used for the current route and current method.
	// This should be called before calling any of the http method handlers (Get,
	// Post, Put, Patch or Delete method). After calling an http method handler
	// this list of middlewares are cleared so that user can define different set
	// of middlewares for next http method handler. Soel middlewares
	// are defined per route per http method.
	Use(...Middleware) RouteHandler

	// Get attaches handler to http GET method
	Get(http.HandlerFunc) RouteHandler

	// Post attaches handler to http POST method
	Post(http.HandlerFunc) RouteHandler

	// Put attaches handler to http PUT method
	Put(http.HandlerFunc) RouteHandler

	// Patch attaches handler to http PATCH method
	Patch(http.HandlerFunc) RouteHandler

	// Delete attaches handler to http DELETE method
	Delete(http.HandlerFunc) RouteHandler
}

RouteHandler interface to configure route handlers.

type Router

type Router interface {
	Route(string) RouteHandler
}

Router interface to create a RouteHandler

Directories

Path Synopsis
Package helpers contains utility functions which are highly opionated and probably only applicable for specific use cases.
Package helpers contains utility functions which are highly opionated and probably only applicable for specific use cases.

Jump to

Keyboard shortcuts

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