router

package
v0.0.0-...-bfbd1c2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 24 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App struct

  • routes: array containg all Route structs

  • port: string port

  • logger: structured logging

  • debugMode: if debugMode is enabled things such as HTML as less static, will be slower but easier to debug.

  • corsMode: if localhost, 127.0.0.1 or no origin do not allow this request.

  • htmlDelims: HTML Delimiters, these can be customized.

  • htmlRender: HTML Renderer, an interface that renders the HTML to the user.

  • funcMap: templates.FuncMap

func New

func New(c ...Config) *App

Create a new default App

func (*App) Add

func (a *App) Add(method Method, path string, params string, handler HandlerFunc, decorator DecoratorFunc)

Add a Route with the method, path, params, handler and decorator

func (*App) Delete

func (a *App) Delete(path string, params string, handler HandlerFunc)

Add Delete route

func (*App) Get

func (a *App) Get(path string, params string, handler HandlerFunc)

Add Get route

func (*App) Head

func (a *App) Head(path string, params string, handler HandlerFunc)

Add Head route

func (*App) LoadHTMLFiles

func (a *App) LoadHTMLFiles(f ...string)

Load a series of HTML files.

func (*App) LoadHTMLGlob

func (a *App) LoadHTMLGlob(p string)

Load a folder of HTML files.

func (*App) Options

func (a *App) Options(path string, params string, handler HandlerFunc)

Add Options route

func (*App) Patch

func (a *App) Patch(path string, params string, handler HandlerFunc)

Add Patch route

func (*App) Post

func (a *App) Post(path string, params string, handler HandlerFunc)

Add Post route

func (*App) Put

func (a *App) Put(path string, params string, handler HandlerFunc)

Add Put route

func (*App) Route

func (a *App) Route(r Route)

Adds a Route to the App

func (*App) Run

func (a *App) Run()

Run the App

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServerHTTP with ResponseWriter and Request

func (*App) Service

func (a *App) Service(s Service)

Adds a service to the App

func (*App) SetHTMLTemplate

func (a *App) SetHTMLTemplate(t *template.Template)

Set the current HTML renderer.

func (*App) Shutdown

func (a *App) Shutdown(f ...ShutdownFunc)

Shutdown the App, should be ran as go Shutdown()

func (*App) Use

func (a *App) Use(m ...MiddlewareFunc)

type Config

type Config struct {
	Port  string
	Debug bool
	CORS  bool
}

Configure routey

  • Port: what port do you want routey to use?

  • Debug: do you want routey to run in debug mode?

  • CORS: do you want to run this in local only mode?

type Context

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

A Context provides

  • writer: write data back to the user

  • request: request data that was given

  • params: the parameters of the request

  • queryCache: a cache of queries for this request

  • queryCached: has the queryCache been made?

func (*Context) Abort

func (c *Context) Abort()

Aborts the current action

func (*Context) AbortWithError

func (c *Context) AbortWithError(s int, e error)

Abort with a status and an error

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(s int)

Abort with a status

func (*Context) Aborted

func (c *Context) Aborted() bool

Has the context been aborted?

func (*Context) BindJSON

func (c *Context) BindJSON(a any) error

Bind JSON to a any

func (*Context) BindTOML

func (c *Context) BindTOML(a any) error

Bind TOML to a any

func (*Context) BindXML

func (c *Context) BindXML(a any) error

Bind XML to a any

func (*Context) BindYAML

func (c *Context) BindYAML(a any) error

Bind YAML to a any

func (*Context) Body

func (c *Context) Body() ([]byte, error)

Get the body of the request

func (*Context) Cookie

func (c *Context) Cookie(n string) (string, error)

Get a value from the cookies of the request

func (*Context) Copy

func (c *Context) Copy() *Context

Copy the current Context and give a pointer to the copy

func (*Context) Decorator

func (c *Context) Decorator() DecoratorFunc

Get the Decorator of the context

func (*Context) Error

func (c *Context) Error(err errs.Error)

Log an error to console

func (*Context) ErrorLog

func (c *Context) ErrorLog(err errs.Error)

Log an error to console

func (*Context) FormFile

func (c *Context) FormFile(n string) (*multipart.FileHeader, error)

Get a form file

func (*Context) Get

func (c *Context) Get(k string) (any, bool)

Gets a value stored within the context

func (*Context) GetFile

func (c *Context) GetFile(p string)

Serve the user a local file

func (*Context) GetHeader

func (c *Context) GetHeader(k string) string

Get the header of a request using a key

func (*Context) HTML

func (c *Context) HTML(s int, n string, d any)

Render HTML with a given file

func (*Context) Handler

func (c *Context) Handler() HandlerFunc

Get the Handler of the context

func (*Context) Header

func (c *Context) Header(k string, v string)

Add to the header of a response with a key and value

func (*Context) JSON

func (c *Context) JSON(s int, b any)

Render response in a JSON format from a body

func (*Context) Method

func (c *Context) Method() Method

Get the method of the route

func (*Context) MultipartForm

func (c *Context) MultipartForm() (*multipart.Form, error)

Get a multipart form

func (*Context) MustBindWith

func (c *Context) MustBindWith(a any, b binding.Binder) error

Must bind with the given struct

func (*Context) MustGet

func (c *Context) MustGet(k string) (any, error)

A value must be returned from this otherwise an error will occur

func (*Context) MustSet

func (c *Context) MustSet(k string, v string) error

Sets a value only if it does not exist

func (*Context) Param

func (c *Context) Param(k string) (string, error)

Get a string from the parameters using a key

func (*Context) ParamAll

func (c *Context) ParamAll() (map[string]string, error)

Get all of the params of the request

func (*Context) ParamBool

func (c *Context) ParamBool(k string) (bool, error)

Get a boolean value from the params

func (*Context) ParamFloat

func (c *Context) ParamFloat(k string) (float64, error)

Get a float value from the params

func (*Context) ParamInt

func (c *Context) ParamInt(k string) (int, error)

Get an integer from the parameters using a key

func (*Context) Path

func (c *Context) Path() string

Get the path of the context

func (*Context) Protocol

func (c *Context) Protocol() string

Get the protocol used by the request

func (*Context) Query

func (c *Context) Query(k string) (string, error)

Query a string from the context's query

func (*Context) QueryAll

func (c *Context) QueryAll() (map[string]string, error)

Get all the queries from the request

func (*Context) QueryBool

func (c *Context) QueryBool(k string) (bool, error)

Get a boolean value from the query

func (*Context) QueryFloat

func (c *Context) QueryFloat(k string) (float64, error)

Get a float value from the query

func (*Context) QueryInt

func (c *Context) QueryInt(k string) (int, error)

Query an integer from the context's query

func (*Context) Redirect

func (c *Context) Redirect(s int, l string)

Redirects to the given location with the given status

func (*Context) Render

func (c *Context) Render(s int, b string)

Render a string body with status

func (*Context) RenderBytes

func (c *Context) RenderBytes(s int, b []byte)

Render a byte array with status

func (*Context) RequestAddress

func (c *Context) RequestAddress() (string, error)

Get the IP off the requester

func (*Context) Reset

func (c *Context) Reset()

Reset the current Context

func (*Context) Route

func (c *Context) Route() *Route

Get the route of the matched route

func (*Context) SaveFile

func (c *Context) SaveFile(f *multipart.FileHeader, p string) error

Save a file that was uploaded

func (*Context) Secure

func (c *Context) Secure() bool

Is the request secure?

func (*Context) Set

func (c *Context) Set(k string, v any)

Sets a value within the context

func (*Context) ShouldBindJSON

func (c *Context) ShouldBindJSON(a any) error

Wrapper for ShouldBindWith(a, binding.JSON)

func (*Context) ShouldBindTOML

func (c *Context) ShouldBindTOML(a any) error

Wrapper for ShouldBindWith(a, binding.TOML)

func (*Context) ShouldBindWith

func (c *Context) ShouldBindWith(a any, b binding.Binder) error

Using the provided Binder, binds the context body with the a any

func (*Context) ShouldBindXML

func (c *Context) ShouldBindXML(a any) error

Wrapper for ShouldBindWith(a, binding.XML)

func (*Context) ShouldBindYAML

func (c *Context) ShouldBindYAML(a any) error

Wrapper for ShouldBindWith(a, binding.YAML)

func (*Context) Status

func (c *Context) Status(s int)

Respond with just a status

func (*Context) String

func (c *Context) String(s int, f string, v ...any)

Render a string with optional values

func (*Context) TOML

func (c *Context) TOML(s int, b any)

Render TOML

func (*Context) Write

func (c *Context) Write(s string) (int, error)

Write a string

func (*Context) WriteBytes

func (c *Context) WriteBytes(body []byte) (int, error)

Write bytes

func (*Context) XML

func (c *Context) XML(s int, b any)

Render XML

func (*Context) YAML

func (c *Context) YAML(s int, b any)

Render YAML

type DecoratorFunc

type DecoratorFunc func(f HandlerFunc) HandlerFunc

Decorate a HandlerFunc

type HTML

type HTML struct {
	Template *template.Template
	Name     string
	Data     any
}

Default HTML renderer.

func (HTML) Render

func (h HTML) Render(w http.ResponseWriter) error

Render the HTML

func (HTML) WriteContentType

func (h HTML) WriteContentType(w http.ResponseWriter)

Write the content type

type HTMLDebug

type HTMLDebug struct {
	Files   []string
	Glob    string
	Delims  HTMLDelims
	FuncMap template.FuncMap
}

Debug HTML renderer, allows for editing of HTML files at runtime.

func (HTMLDebug) Instance

func (h HTMLDebug) Instance(n string, d any) Renderer

Create an instance of the HTMLRenderer, for the debugging Renderer

type HTMLDelims

type HTMLDelims struct {
	Left  string
	Right string
}

Delimiters used in the HTML file, defaults to {{ ... }}

type HTMLRender

type HTMLRender struct {
	Template *template.Template
	Delims   HTMLDelims
}

HTML Renderer implementor

func (HTMLRender) Instance

func (h HTMLRender) Instance(n string, d any) Renderer

Create an instance of the HTMLRenderer

type HTMLRenderer

type HTMLRenderer interface {
	Instance(string, any) Renderer
}

Interface for Rendering HTML

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc takes a routey.Context pointer

func Wrap

Wrap a standard http Handler in a routey HandlerFunc

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(c *Context)

Serves the HandlerFunc

type M

type M map[string]any

type Method

type Method int
const (
	Undefined Method = iota // 0
	Get
	Post
	Put
	Patch
	Delete
	Head
	Options
)

func (Method) String

func (m Method) String() string

type MiddlewareFunc

type MiddlewareFunc func(c *Context)

Middleware function, run before every request

func (MiddlewareFunc) Serve

func (m MiddlewareFunc) Serve(c *Context)

Serves the middleware function

type Redirect

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

Redirect struct

func (Redirect) Render

func (r Redirect) Render(w http.ResponseWriter) error

Render the redirect, just sends the requester to the specified location

func (Redirect) WriteContentType

func (r Redirect) WriteContentType(http.ResponseWriter)

Write the content type, there is no content

type Renderer

type Renderer interface {
	Render(http.ResponseWriter) error
	WriteContentType(http.ResponseWriter)
}

An interface for Rendering in a response

type Route

type Route struct {
	Path          string
	Params        string
	Method        Method
	HandlerFunc   HandlerFunc
	DecoratorFunc DecoratorFunc
	// contains filtered or unexported fields
}

Route struct

  • Path: path of the route request

  • Params: params of the route start with :

  • Method: method of the route

  • HandlerFunc: handler function of the route

  • DecoratorFunc: decorator function of the route

  • regexp: regexp used for params

func (*Route) Copy

func (r *Route) Copy() *Route

Copy the route

func (*Route) Format

func (r *Route) Format() error

Format the params of the route

func (*Route) Match

func (r *Route) Match(c *Context) bool

Match a Route with a Context

type Service

type Service interface {
	Add() []Route
}

type ShutdownFunc

type ShutdownFunc func()

Func for shutting down the router

type State

type State int
const (
	Healthy   State = iota // 0
	UnHealthy              // 1
	Aborted                // 2
	Restored               // 3
)

Jump to

Keyboard shortcuts

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