alanis

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

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 10 Imported by: 2

README

HTTP router for Go

This http router is based off of bunrouter.

But it uses an interface for context allowing easy extension of contexts for new e.g.

  • templates
  • turbo streams.
  • etc...
go get git.sr.ht/~tompinn23/alanis

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

func JSON

func JSON(w http.ResponseWriter, value interface{}) error

JSON marshals the value as JSON and writes it to the response writer.

Don't hesitate to copy-paste this function to your project and customize it as necessary.

func Version

func Version() string

Version is the current release version.

Types

type CompatGroup

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

CompatGroup is like Group, but it works with http.HandlerFunc instead of bunrouter handler.

func (CompatGroup) DELETE

func (g CompatGroup) DELETE(path string, handler http.HandlerFunc)

func (CompatGroup) GET

func (g CompatGroup) GET(path string, handler http.HandlerFunc)

func (CompatGroup) HEAD

func (g CompatGroup) HEAD(path string, handler http.HandlerFunc)

func (CompatGroup) Handle

func (g CompatGroup) Handle(method string, path string, handler http.HandlerFunc)

func (CompatGroup) NewGroup

func (g CompatGroup) NewGroup(path string, opts ...GroupOption) *CompatGroup

func (CompatGroup) OPTIONS

func (g CompatGroup) OPTIONS(path string, handler http.HandlerFunc)

func (CompatGroup) PATCH

func (g CompatGroup) PATCH(path string, handler http.HandlerFunc)

func (CompatGroup) POST

func (g CompatGroup) POST(path string, handler http.HandlerFunc)

func (CompatGroup) PUT

func (g CompatGroup) PUT(path string, handler http.HandlerFunc)

func (CompatGroup) WithGroup

func (g CompatGroup) WithGroup(path string, fn func(g *CompatGroup))

func (CompatGroup) WithMiddleware

func (g CompatGroup) WithMiddleware(middleware MiddlewareFunc) *CompatGroup

type Context

type Context struct {
	Writer  http.ResponseWriter
	Request *Request
	// contains filtered or unexported fields
}

func (*Context) AddHeader

func (c *Context) AddHeader(header string, value string)

AddHeader will attempt to append the value to the specified HTTP header creating the slice if no header existed for this response.

func (*Context) Copy

func (c *Context) Copy() *Context

func (*Context) HTML

func (c *Context) HTML(code int, name string, data interface{}) error

HTML will render the given template in name with the specifed data and http status code. On any kind of template error etc... the error will be returned.

func (*Context) JSON

func (c *Context) JSON(code int, h H) error

JSON encodes the JSON data in h with the specified http status code for response:

func (*Context) SetContentType

func (c *Context) SetContentType(ty string)

SetContentType will apply the given content type in ty to the response

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie sets a Set-Cookie Header on the HTTP response corresponding to the cookie parameter, Invalid cookies may be silently dropped.

func (*Context) SetHeader

func (c *Context) SetHeader(header string, value string)

SetHeader will blindly set the relevant HTTP header to the value specified.

func (*Context) String

func (c *Context) String(code int, val string)

func (*Context) TemplateContext

func (c *Context) TemplateContext(ctx H)

func (*Context) TurboStream

func (c *Context) TurboStream(code int, name string, data interface{}) error

TurboStream will render the given template in name with the specifed data and http status code. Specially it will set the content type to the turbo-stream type for the turbo library.

https://turbo.hotwired.dev/ On any kind of template error etc... the error will be returned.

type Engine

type Engine interface {
	SetDebug(debug bool)
	Instance(name string, ctx *H, data interface{}) (Render, error)
}

type Group

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

Group is a group of routes and middlewares.

func (*Group) Compat

func (g *Group) Compat() *CompatGroup

func (*Group) DELETE

func (g *Group) DELETE(path string, handler HandlerFunc)

Syntactic sugar for Handle("DELETE", path, handler)

func (*Group) GET

func (g *Group) GET(path string, handler HandlerFunc)

Syntactic sugar for Handle("GET", path, handler)

func (*Group) HEAD

func (g *Group) HEAD(path string, handler HandlerFunc)

Syntactic sugar for Handle("HEAD", path, handler)

func (*Group) Handle

func (g *Group) Handle(meth string, path string, handler HandlerFunc)

func (*Group) NewGroup

func (g *Group) NewGroup(path string, opts ...GroupOption) *Group

NewGroup adds a sub-group to this group.

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, handler HandlerFunc)

Syntactic sugar for Handle("OPTIONS", path, handler)

func (*Group) PATCH

func (g *Group) PATCH(path string, handler HandlerFunc)

Syntactic sugar for Handle("PATCH", path, handler)

func (*Group) POST

func (g *Group) POST(path string, handler HandlerFunc)

Syntactic sugar for Handle("POST", path, handler)

func (*Group) PUT

func (g *Group) PUT(path string, handler HandlerFunc)

Syntactic sugar for Handle("PUT", path, handler)

func (*Group) Use

func (g *Group) Use(middlewares ...MiddlewareFunc) *Group

func (*Group) Verbose

func (g *Group) Verbose() *VerboseGroup

func (*Group) WithGroup

func (g *Group) WithGroup(path string, fn func(g *Group))

func (*Group) WithMiddleware

func (g *Group) WithMiddleware(middleware MiddlewareFunc) *Group

type GroupOption

type GroupOption interface {
	Option
	// contains filtered or unexported methods
}

func Use

func Use(fns ...MiddlewareFunc) GroupOption

Use is an alias for WithMiddleware.

func WithGroup

func WithGroup(fn func(g *Group)) GroupOption

WithGroup calls the fn with the current Group.

func WithHandler

func WithHandler(fn HandlerFunc) GroupOption

WithHandler is like WithMiddleware, but the handler can't modify the request.

func WithMiddleware

func WithMiddleware(fns ...MiddlewareFunc) GroupOption

WithMiddleware adds a middleware handler to the Group's middleware stack.

type H

type H map[string]interface{}

type HandlerFunc

type HandlerFunc func(ctx *Context) error

func HTTPHandler

func HTTPHandler(handler http.Handler) HandlerFunc

HTTPHandler converts http.Handler to bunrouter.HandlerFunc.

func HTTPHandlerFunc

func HTTPHandlerFunc(handler http.HandlerFunc) HandlerFunc

HTTPHandlerFunc converts http.HandlerFunc to bunrouter.HandlerFunc.

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithDebugMode

func WithDebugMode() Option

func WithHTMLRenderer

func WithHTMLRenderer(html Engine) Option

func WithMethodNotAllowedHandler

func WithMethodNotAllowedHandler(handler HandlerFunc) Option

MethodNotAllowedHandler is called when a route matches, but that route does not have a handler for the requested method. The default handler just writes the status code http.StatusMethodNotAllowed.

func WithNotFoundHandler

func WithNotFoundHandler(handler HandlerFunc) Option

WithNotFoundHandler is called when there is no a matching pattern. The default NotFoundHandler is http.NotFound.

type Param

type Param struct {
	Key   string
	Value string
}

type Params

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

func ParamsFromContext

func ParamsFromContext(ctx context.Context) Params

func (Params) ByName

func (ps Params) ByName(name string) string

func (Params) Get

func (ps Params) Get(name string) (string, bool)

func (Params) Int32

func (ps Params) Int32(name string) (int32, error)

func (Params) Int64

func (ps Params) Int64(name string) (int64, error)

func (Params) IsZero

func (ps Params) IsZero() bool

func (Params) Map

func (ps Params) Map() map[string]string

func (Params) Route

func (ps Params) Route() string

func (Params) Slice

func (ps Params) Slice() []Param

func (Params) Uint32

func (ps Params) Uint32(name string) (uint32, error)

func (Params) Uint64

func (ps Params) Uint64(name string) (uint64, error)

type Render

type Render interface {
	Render(w http.ResponseWriter) error
}

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(req *http.Request) *Request

func (Request) Param

func (req Request) Param(key string) string

func (Request) Params

func (req Request) Params() Params

func (Request) Route

func (req Request) Route() string

func (Request) WithContext

func (req Request) WithContext(ctx context.Context) *Request

type Router

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

func New

func New(opts ...Option) *Router

func (*Router) Debug

func (r *Router) Debug() bool

func (*Router) ServeHTTP

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

ServeHTTP implements http.Handler interface.

func (*Router) ServeHTTPError

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

ServeHTTPError is like ServeHTTP, but it also returns the error returned by the matched route handler.

type VerboseGroup

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

VerboseGroup is like Group, but it works with VerboseHandlerFunc instead of bunrouter handler.

func (VerboseGroup) DELETE

func (g VerboseGroup) DELETE(path string, handler VerboseHandlerFunc)

func (VerboseGroup) GET

func (g VerboseGroup) GET(path string, handler VerboseHandlerFunc)

func (VerboseGroup) HEAD

func (g VerboseGroup) HEAD(path string, handler VerboseHandlerFunc)

func (VerboseGroup) Handle

func (g VerboseGroup) Handle(method string, path string, handler VerboseHandlerFunc)

func (VerboseGroup) NewGroup

func (g VerboseGroup) NewGroup(path string, opts ...GroupOption) *VerboseGroup

func (VerboseGroup) OPTIONS

func (g VerboseGroup) OPTIONS(path string, handler VerboseHandlerFunc)

func (VerboseGroup) PATCH

func (g VerboseGroup) PATCH(path string, handler VerboseHandlerFunc)

func (VerboseGroup) POST

func (g VerboseGroup) POST(path string, handler VerboseHandlerFunc)

func (VerboseGroup) PUT

func (g VerboseGroup) PUT(path string, handler VerboseHandlerFunc)

func (VerboseGroup) WithGroup

func (g VerboseGroup) WithGroup(path string, fn func(g *VerboseGroup))

func (VerboseGroup) WithMiddleware

func (g VerboseGroup) WithMiddleware(middleware MiddlewareFunc) *VerboseGroup

type VerboseHandlerFunc

type VerboseHandlerFunc func(w http.ResponseWriter, req *http.Request, ps Params)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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