ngamux

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: MPL-2.0 Imports: 11 Imported by: 24

README

ngamux

Simple HTTP router for Go

made-with-Go Go Version GoDoc Reference GoReportCard Coverage Status


Installation

Run this command with correctly configured Go toolchain.

go get github.com/ngamux/ngamux

Examples

package main

import(
  "net/http"
  "github.com/ngamux/ngamux"
)

func main() {
  mux := ngamux.New()
  mux.Get("/", func(rw http.ResponseWriter, r *http.Request) error {
    return ngamux.Res(rw).
      Status(http.StatusOK).
      Json(ngamux.Map{
        "message": "welcome!",
      })
  })
  
  http.ListenAndServe(":8080", mux)
}

See more examples!

Provided Middlewares

License

This project is licensed under the Mozilla Public License 2.0.

Contributors

Thanks to all contributors!

Contributors

Documentation

Overview

Package ngamux is simple HTTP router for Go that compatible with net/http, the standard library to serve HTTP. Designed to make everything goes in simple way.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrorNotFound is errors object when searching failure
	ErrorNotFound = errors.New("not found")

	// ErrorMethodNotAllowed is errors object when there access to invalid method
	ErrorMethodNotAllowed = errors.New("method not allowed")
)

Functions

func WithErrorHandler added in v1.1.0

func WithErrorHandler(globalErrorHandler Handler) func(*Config)

WithErrorHandler returns function that adds GlobalErrorHandler into config

func WithLogLevel added in v1.4.0

func WithLogLevel(level LogLevel) func(*Config)

WithLogLevel returns function that adds GlobalErrorHandler into config

func WithTrailingSlash added in v1.1.0

func WithTrailingSlash() func(*Config)

WithTrailingSlash returns function that adds RemoveTrailingSlash into config

Types

type Config

type Config struct {
	RemoveTrailingSlash bool
	GlobalErrorHandler  Handler
	LogLevel            LogLevel
}

Config define ngamux global configuration

func NewConfig added in v1.1.0

func NewConfig() Config

NewConfig returns Config with some default values

type Handler added in v1.0.1

type Handler func(rw http.ResponseWriter, r *http.Request) error

Handler describe function handler

func (Handler) ServeHTTP added in v1.0.1

func (h Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP same as original Handler but for built in HTTP HandlerFunc

type KeyContext

type KeyContext int

KeyContext describe key type for ngamux context

const (
	// KeyContextParams is key context for url params
	KeyContextParams KeyContext = 1 << iota
)

type LogLevel added in v1.4.0

type LogLevel int
const (
	LogLevelQuiet LogLevel = iota
	LogLevelInfo
	LogLevelWarn
	LogLevelError
)

func (LogLevel) String added in v1.4.0

func (l LogLevel) String() string

type Map added in v1.0.2

type Map map[string]any

Map is key value type to store any data

type MiddlewareFunc

type MiddlewareFunc func(next Handler) Handler

MiddlewareFunc describe middleware function

func WithMiddlewares

func WithMiddlewares(middleware ...MiddlewareFunc) MiddlewareFunc

WithMiddlewares returns single middleware from multiple middleware

type Ngamux

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

Ngamux describe structure of ngamux object

func New added in v1.0.4

func New(opts ...func(*Config)) *Ngamux

New returns new ngamux object

func (*Ngamux) All added in v1.0.1

func (mux *Ngamux) All(url string, handler Handler)

All register route for a url with any request method

func (Ngamux) Config added in v1.5.0

func (mux Ngamux) Config() Config

Config returns registered config (read only)

func (*Ngamux) Delete

func (mux *Ngamux) Delete(url string, handler Handler)

Delete register route for a url with Delete request method

func (*Ngamux) Get

func (mux *Ngamux) Get(url string, handler Handler)

Get register route for a url with Get request method

func (*Ngamux) Group

func (mux *Ngamux) Group(url string, middlewares ...MiddlewareFunc) *Ngamux

Group returns new nested ngamux object

func (*Ngamux) Head added in v1.5.0

func (mux *Ngamux) Head(url string, handler Handler)

Head register route for a url with Head request method

func (Ngamux) Log added in v1.4.0

func (m Ngamux) Log(level LogLevel, message string, data ...any)

func (*Ngamux) Patch

func (mux *Ngamux) Patch(url string, handler Handler)

Patch register route for a url with Patch request method

func (*Ngamux) Post

func (mux *Ngamux) Post(url string, handler Handler)

Post register route for a url with Post request method

func (*Ngamux) Put

func (mux *Ngamux) Put(url string, handler Handler)

Put register route for a url with Put request method

func (*Ngamux) ServeHTTP

func (mux *Ngamux) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP run ngamux router matcher

func (*Ngamux) Use

func (mux *Ngamux) Use(middlewares ...MiddlewareFunc)

Use register global middleware

func (*Ngamux) With added in v1.1.0

func (mux *Ngamux) With(middlewares ...MiddlewareFunc) *Ngamux

With register middlewares and returns router

type Request added in v1.3.0

type Request struct {
	*http.Request
}

Request define single request manager

func Req added in v1.3.0

func Req(r *http.Request) *Request

Req needs *http.Request and returns *Request object

func (Request) FormFile added in v1.3.0

func (r Request) FormFile(key string, maxFileSize ...int64) (*multipart.FileHeader, error)

FormFile returns file from form using a key

func (Request) FormValue added in v1.3.0

func (r Request) FormValue(key string, fallback ...string) string

FormValue returns data from form using a key

func (*Request) IsLocalhost added in v1.4.0

func (r *Request) IsLocalhost() bool

IsLocalhost returns true if hostname is localhost or 127.0.0.1

func (Request) JSON added in v1.3.0

func (r Request) JSON(store any) error

JSON get json data from request body and store to variable reference

func (*Request) Locals added in v1.3.0

func (r *Request) Locals(key any, value ...any) any

Locals needs key and optional value It returns any if only key and no value given It insert value to context if key and value is given

func (Request) Params added in v1.3.0

func (r Request) Params(key string) string

Params returns parameter from url using a key

func (Request) Query added in v1.3.0

func (r Request) Query(key string, fallback ...string) string

Query returns data from query params using a key

type Response added in v1.2.0

type Response struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

Response define single response manager

func Res added in v1.2.0

func Res(rw http.ResponseWriter) *Response

Res needs http.ResponseWriter and returns *Response object

func (*Response) Html added in v1.6.0

func (r *Response) Html(path string, data any) error

Html write text/html data with HTML string as response body

func (*Response) Json added in v1.6.0

func (r *Response) Json(data any) error

Json write application/json data with json encoded string as response body

func (*Response) Status added in v1.2.0

func (r *Response) Status(status int) *Response

Status write status code

func (*Response) Text added in v1.6.0

func (r *Response) Text(data string) error

Text writes text/plain data with simple string as response body

type Route

type Route struct {
	RawPath    string
	Path       string
	Method     string
	Handler    Handler
	Params     [][]string
	URLMatcher *regexp.Regexp
	// contains filtered or unexported fields
}

Route describe a route object

Jump to

Keyboard shortcuts

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