mux

package module
v0.0.0-...-e6a8880 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2025 License: MIT Imports: 9 Imported by: 0

README

Mux

Tiny wrapper around Go's builtin http.ServeMux with easy routing methods.

Example

package main

import (
	"log/slog"
	"net/http"

	"gitserver.in/patialtech/mux"
)

func main() {
	// create a new router
	r := mux.NewRouter()

	// you can use any middleware that is: "func(http.Handler) http.Handler"
	// so you can use any of it
	// - https://github.com/gorilla/handlers
	// - https://github.com/go-chi/chi/tree/master/middleware

	// add some root level middlewares, these will apply to all routes after it
	r.Use(middleware1, middleware2)

	// let's add a route
	r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("i am route /hello"))
	})
	// r.Post(pattern string, h http.HandlerFunc)
	// r.Put(pattern string, h http.HandlerFunc)
	// ...

	// you can inline middleware(s) to a route
	r.
		With(mwInline).
		GET("/hello-2", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("i am route /hello-2 with my own middleware"))
		})

	// define a resource
	r.Resource("/photos", func(resource *mux.Resource) {
		// rails style resource routes
		// GET       /photos
		// GET       /photos/new
		// POST      /photos
		// GET       /photos/:id
		// GET       /photos/:id/edit
		// PUT 		 /photos/:id
		// PATCH 	 /photos/:id
		// DELETE    /photos/:id
		resource.Index(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("all photos"))
		})

		resource.New(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("upload a new pohoto"))
		})
	})

	// create a group of few routes with their own middlewares
	r.Group(func(grp *mux.Router) {
		grp.Use(mwGroup)
		grp.GET("/group", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("i am route /group"))
		})
	})

	// catches all
	r.GET("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello there"))
	})

	// Serve allows graceful shutdown, you can use it
	r.Serve(func(srv *http.Server) error {
		srv.Addr = ":3001"
		// srv.ReadTimeout = time.Minute
		// srv.WriteTimeout = time.Minute

		slog.Info("listening on http://localhost" + srv.Addr)
		return srv.ListenAndServe()
	})
}

func middleware1(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slog.Info("i am middleware 1")
		h.ServeHTTP(w, r)
	})
}

func middleware2(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slog.Info("i am middleware 2")
		h.ServeHTTP(w, r)
	})
}

func mwInline(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slog.Info("i am inline middleware")
		h.ServeHTTP(w, r)
	})
}

func mwGroup(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slog.Info("i am group middleware")
		h.ServeHTTP(w, r)
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Resource

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

Resource is a resourceful route provides a mapping between HTTP verbs and URLs and controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as Index route

GET /resource-name # index route

GET /resource-name/new # create resource page

POST /resource-name # create resource post

GET /resource-name/:id # view resource

GET /resource-name/:id/edit # edit resource

PUT /resource-name/:id # update resource

DELETE /resource-name/:id # delete resource

func (*Resource) Create

func (r *Resource) Create(h http.HandlerFunc)

Create is POST /resource-name

func (*Resource) Destroy

func (r *Resource) Destroy(h http.HandlerFunc)

func (*Resource) Index

func (r *Resource) Index(h http.HandlerFunc)

Index is GET /resource-name

func (*Resource) New

func (r *Resource) New(h http.HandlerFunc)

New is GET /resource-name/new

func (*Resource) PartialUpdate

func (r *Resource) PartialUpdate(h http.HandlerFunc)

PartialUpdate is PATCH /resource-name/:id

func (*Resource) Show

func (r *Resource) Show(h http.HandlerFunc)

Show is GET /resource-name/:id

func (*Resource) Update

func (r *Resource) Update(h http.HandlerFunc)

Update is PUT /resource-name/:id

func (*Resource) Use

func (r *Resource) Use(middlewares ...func(http.Handler) http.Handler)

Use will register middleware(s) on Router stack.

type Router

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

Router is a wrapper around the go's standard http.ServeMux. It's a lean wrapper with methods to make routing easier

func NewRouter

func NewRouter() *Router

func (*Router) CONNECT

func (r *Router) CONNECT(pattern string, h http.HandlerFunc)

CONNECT method route

func (*Router) DELETE

func (r *Router) DELETE(pattern string, h http.HandlerFunc)

DELETE method route

func (*Router) GET

func (r *Router) GET(pattern string, h http.HandlerFunc)

GET method route

func (*Router) Group

func (r *Router) Group(fn func(grp *Router))

Group adds a new inline-Router along the current routing path, with a fresh middleware stack for the inline-Router.

func (*Router) HEAD

func (r *Router) HEAD(pattern string, h http.HandlerFunc)

HEAD method route

func (*Router) OPTIONS

func (r *Router) OPTIONS(pattern string, h http.HandlerFunc)

func (*Router) PATCH

func (r *Router) PATCH(pattern string, h http.HandlerFunc)

PATCH method route

func (*Router) POST

func (r *Router) POST(pattern string, h http.HandlerFunc)

POST method route

func (*Router) PUT

func (r *Router) PUT(pattern string, h http.HandlerFunc)

PUT method route

func (*Router) Resource

func (r *Router) Resource(pattern string, fn func(resource *Resource))

Resource resourceful route provides a mapping between HTTP verbs for given the pattern

func (*Router) Serve

func (r *Router) Serve(cb ServeCB)

Serve with graceful shutdown

func (*Router) ServeHTTP

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

func (*Router) TRACE

func (r *Router) TRACE(pattern string, h http.HandlerFunc)

TRACE method route

func (*Router) Use

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

Use will register middleware(s) with router stack

func (*Router) With

func (r *Router) With(middleware ...func(http.Handler) http.Handler) *Router

With adds inline middlewares for an endpoint handler.

type ServeCB

type ServeCB func(srv *http.Server) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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