router

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: Unlicense Imports: 5 Imported by: 0

README

Router

codecov

A simple wrapper for httprouter that supports middleware & sub-routers. Additionally, it uses the new Go 1.22 http.Request feature to set Path values on the http.Request.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthCheckHandler added in v0.2.0

func HealthCheckHandler() http.Handler

HealthCheckHandler returns an http.Handler that handles health check requests. It sets the Content-Type header to "text/plain; charset=utf-8" and writes "OK" as the response body.

func NewServer added in v0.2.0

func NewServer(addr string, handler http.Handler) *http.Server

NewServer creates a new HTTP server with the specified address and handler. The server has a read timeout of 60 seconds, a write timeout of 60 seconds, and an idle timeout of 5 minutes.

func Run

func Run(ctx context.Context, server *http.Server) error

Run runs the HTTP server using the provided context and server configuration. It returns an error if the server fails to start or encounters an error during shutdown.

func RunServer added in v0.2.0

func RunServer(ctx context.Context, in RunInput) error

RunServer runs the server using the provided context and input parameters. It starts a goroutine to execute the Run method of the input object, which accepts the passed server. It waits for the server to complete or for the context to be canceled. If the context is canceled, it stops the server using the Stop method of the input object. Returns an error if the server fails to start or if the context is canceled.

Types

type Router

type Router struct {
	// contains filtered or unexported fields
}
Example
r := router.New()

// Register the HealthCheckHandler to the "/health-check" route, before the middleware.
// This will remove the middleware overhead from the HealthCheck endpoint
r.Handle(http.MethodGet, "/health-check", router.HealthCheckHandler())

r.Use(middleware.RequestID(), middleware.Logger(slog.LevelInfo))

// All routes will be prefixed with "/api"
r.Sub("/api", func(sub *router.Router) {
	sub.Use(middleware.NoCache())

	// Full path /api/posts/:id
	sub.HandleFunc("GET", "/posts/:id", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("id:" + r.PathValue("id")))
	})
})

s := router.NewServer(":3000", r)

go func() {
	<-time.After(100 * time.Millisecond)
	s.Shutdown(context.Background())
}()

err := router.Run(context.Background(), s)

fmt.Println(err)
Output:

http: Server closed

func New

func New() *Router

func (*Router) Handle

func (r *Router) Handle(method, path string, h http.Handler)

func (*Router) HandleFunc

func (r *Router) HandleFunc(method, path string, h func(w http.ResponseWriter, r *http.Request))

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) Sub

func (r *Router) Sub(path string, fn func(*Router))

func (*Router) SubRouter added in v0.2.0

func (r *Router) SubRouter(path string) *Router

func (*Router) Use

func (r *Router) Use(m ...func(http.Handler) http.Handler)

type RunInput added in v0.2.0

type RunInput struct {
	Server *http.Server                              // The HTTP Server to run
	Run    func(context.Context, *http.Server) error // The function called to run the server
	Stop   func(context.Context, *http.Server) error // The function called if the context is canceled to stop the server
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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