mwr

package module
v0.0.0-...-503d1fb Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 12 Imported by: 3

README

mwr

mca's web router (mwr) is a really barebones HTTP router built upon Go's standard libraries. There is little reason to use it asides from the fact you don't have a reason to use it.

There's nothing cool about it, I wrote it because I noticed I've been using essentially the same code for some projects so I thought why not just abstract a portion of it away?

Features

  • It's not fast: net/http is a great package but it is by no means performant. You may be able to hack in fasthttp but I don't care enough to do so myself.
  • Near zero real world use: I'm the only person that I want using this and I'm likely the only person that's going to. It's not high quality software, I just wanted to stop writing the same code over and over again.
  • Documentation probably sucks: I wrote comments, I really tried.
  • But hey, it plugs into net/http! Not that you would want to use this in the first place

Really the only redeeming factor is how you interface with it because I'm used to some other web frameworks that are popular in the Go world but I don't use anymore because I don't want to use more than the standard library unless absolutely necessary now.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoData      = errors.New("no data in body")
	ErrUnsupported = errors.New("unsupported feature")
)
View Source
var (
	// ErrInvalidParam is returned by newRoute when the paramater name does
	// not make sense, like if it is zero-length.
	ErrInvalidParam = errors.New("invalid param name")
)

Functions

This section is empty.

Types

type Cookie = http.Cookie

type Ctx

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

Ctx is the contextual object of mwr that provides all data one may want to know for a HTTP request.

Ctx should not be used after the end of a handler, nor after the HTTP response has been sent out.

func (*Ctx) BodyParser

func (c *Ctx) BodyParser(d any) error

BodyParser parses the body contents according to the Content-Type. BodyParser supports JSON.

If Content-Type is blank, ErrNoData is returned. If the Content-Type specifies an unsupported format, ErrUnsupported is returned. Otherwise, an error from the appropriate parser may be returned.

func (*Ctx) Context

func (c *Ctx) Context() context.Context

Context returns the request context.

func (*Ctx) Cookie

func (c *Ctx) Cookie(key string, defVal ...string) string

Cookie returns the value of the cookie specified by key.

By default, Cookie will return "" if the cookie is not found, however if a default value is specified then that will be returned.

func (*Ctx) FormValue

func (c *Ctx) FormValue(key string, def ...string) string

FormValue gets the value of a form on a POST or a PUT request.

TODO: def is not implemented. Do not use it.

func (*Ctx) FormValues

func (c *Ctx) FormValues(key string, def ...string) []string

FormValues returns all values for any given key.

func (*Ctx) Get

func (c *Ctx) Get(header string, def ...string) string

Get returns the value of the request header, or a default value.

func (*Ctx) Hijack

func (c *Ctx) Hijack(fn HijackerFunc) error

Hijack hijacks the HTTP portion of this connection after all handlers are run. No data will be sent to the client after Hijack is called.

func (*Ctx) IP

func (c *Ctx) IP() string

IP returns the IP address of the client.

func (*Ctx) Method

func (c *Ctx) Method() string

Method returns the method of this request.

func (*Ctx) Next

func (c *Ctx) Next() error

Next recurses into the next handler and returns the error from it.

This method will never fail unless the request is finished.

func (*Ctx) Param

func (c *Ctx) Param(name string) string

Param returns a parameter from the path, or an empty string.

The returned value may not be used once the request is completed.

func (*Ctx) Params

func (c *Ctx) Params() map[string]string

Params returns all parameters specified in the request by their name.

The returned map may not be used once the request is completed.

func (*Ctx) Path

func (c *Ctx) Path() string

Path returns the path for this request.

func (*Ctx) Queries

func (c *Ctx) Queries(key string) []string

Queries returns a list of query values.

func (*Ctx) Query

func (c *Ctx) Query(key string, def ...string) string

Query returns the value of a query value, or a default value.

func (*Ctx) ReadFrom

func (c *Ctx) ReadFrom(content io.Reader) error

ReadFrom copies all data from an io.Reader to a client.

The Ctx will be unable to send any more data after calling ReadFrom. All calls to functions that would write data will panic.

func (*Ctx) Redirect

func (c *Ctx) Redirect(loc string, code ...int) error

Redirect a client to another page.

This method will panic if the request is finished.

func (*Ctx) Reset

func (c *Ctx) Reset()

Reset empties the send buffer.

func (*Ctx) SendFile

func (c *Ctx) SendFile(path string) error

SendFile sends a file to the end user. Unlike all other functions, this completely bypasses the internal buffer.

This method will never fail as this uses http.ServeFile internally which does not return an error.

The Ctx will be unable to send any more data after calling SendFile. All calls to functions that would write data will panic.

func (*Ctx) SendStatus

func (c *Ctx) SendStatus(code int) error

SendStatus sends headers and a status code to the client.

This method will panic if the request is finished.

func (*Ctx) SendString

func (c *Ctx) SendString(d string) error

SendString is a convenience wrapper around Write.

This method will never fail unless the request is finished.

func (*Ctx) Set

func (c *Ctx) Set(key, val string) *Ctx

Set sets the value of a response header.

This method is chainable. This method will panic if the request is finished.

func (*Ctx) SetCookie

func (c *Ctx) SetCookie(key, val string, ck ...Cookie) *Ctx

SetCookie sets the value of a cookie.

By default, the cookie will have SameSite=Strict, and will not expire. However if a Cookie is passed as an argument, values will be copied from the Cookie. Name and Value will be filled from key and val.

If val is empty, the cookie will be unset.

func (*Ctx) Status

func (c *Ctx) Status(code int) *Ctx

Status sets the status code for the reply to the client.

This method is chainable. This method will panic if the request is finished.

func (*Ctx) URL

func (c *Ctx) URL() *url.URL

URL returns the URL for this request.

func (*Ctx) Write

func (c *Ctx) Write(d []byte) (int, error)

Write writes data to the underlying buffer to be sent to the end user.

This method will never fail unless the request is finished.

type Handler

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

Handler is a net/http.Handler interface that serves as the way you would expose mwr to the web.

func (*Handler) Get

func (h *Handler) Get(path string, fn func(*Ctx) error) error

Get pushes a handler for a GET route onto the handler stack.

func (*Handler) Method

func (h *Handler) Method(method, path string, fn func(*Ctx) error) error

Method creates a route that matches a method and a path and pushes it onto the Handler stack.

Use an empty method value to match all methods.

func (*Handler) Post

func (h *Handler) Post(path string, fn func(*Ctx) error) error

Post pushes a handler for a POST route onto the handler stack.

func (*Handler) ServeHTTP

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

ServeHTTP implements http.Handler, for use on a net/http compatible HTTP server.

func (*Handler) Use

func (h *Handler) Use(fn func(*Ctx) error) error

Use pushes a handler that matches any request onto the handler stack.

type HijackerFunc

type HijackerFunc func(http.ResponseWriter, *http.Request)

HijackerFunc takes over the HTTP connection.

Jump to

Keyboard shortcuts

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