sigma

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2023 License: MIT Imports: 5 Imported by: 0

README

sigma

a small wrapper around go-chi HTTP router.

NOTE: This package is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

API

Router methods.

// Router
type Router interface {
	Endpoint(method, pattern string, handler http.HandlerFunc)
	Use(middlewares ...func(next http.Handler) http.Handler)
	Group(pattern string, fn func(r Router))
	Static(pattern, path string)
	NotFound(handler http.HandlerFunc)
	NotAllowed(handler http.HandlerFunc)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

Param returns the url parameter from a http.Request object.

Param(r *http.Request, key string) string

Example

package main

import (
	"log"
	"net/http"

	"github.com/twiny/sigma"
)

func main() {
	srv := sigma.NewServer(":1234")
	defer srv.Stop()

	router := srv.NewRouter()

	// custom 404 not found handler
	router.NotFound(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("404 - Not Found"))
	})

	// custom 405 not allowed handler
	router.NotAllowed(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusMethodNotAllowed)
		w.Write([]byte("405 - Method Not Allowed"))
	})

	// middleware
	router.Use(func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			r.Header.Add("x-key-1", "main router")
			next.ServeHTTP(w, r)
		})
	})

	// endpoints
	router.Endpoint(http.MethodGet, "/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
		name := sigma.Param(r, "name")
		w.Write([]byte("Hello World " + name))
	})

	// sub router
	router.Group("/v1", func(r sigma.Router) {
		// sub router middleware
		r.Use(func(next http.Handler) http.Handler {
			return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				r.Header.Add("x-key-2", "sub router")
				next.ServeHTTP(w, r)
			})
		})
		//
		r.Endpoint(http.MethodGet, "/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
			name := sigma.Param(r, "name")
			w.Write([]byte("Hello World v1 " + name))
		})
	})

	// static files
	router.Static("/web", "./static")

	log.Fatal(srv.Start())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Param

func Param(r *http.Request, key string) string

Types

type Router

type Router interface {
	Endpoint(method, pattern string, handler http.HandlerFunc)
	Use(middlewares ...func(next http.Handler) http.Handler)
	Group(pattern string, fn func(r Router))
	Static(pattern, path string)
	NotFound(handler http.HandlerFunc)
	NotAllowed(handler http.HandlerFunc)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

type Server

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

func NewServer

func NewServer(addr string) *Server

func (*Server) NewRouter

func (s *Server) NewRouter() Router

func (*Server) Start

func (s *Server) Start() error

func (*Server) Stop

func (s *Server) Stop() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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