router

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 6 Imported by: 0

README

DWork Web - Routes

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DEBUG_FLAG bool = false

Functions

func ParseParamType

func ParseParamType(value string, paramType ParamTypes, path string) (interface{}, error)

Types

type DWorkContext

type DWorkContext struct {
	Params   RouteParams
	Response http.ResponseWriter
	Request  *http.Request
}

type HTTPVerb

type HTTPVerb int
const (
	GET HTTPVerb = iota
	POST
	PUT
	PATCH
	DELETE
	HEAD
	OPTIONS
)

func (HTTPVerb) Parse

func (verb HTTPVerb) Parse() (string, error)

type InvalidHttpVerb

type InvalidHttpVerb struct{}

func (InvalidHttpVerb) Error

func (err InvalidHttpVerb) Error() string

type InvalidParamStruct

type InvalidParamStruct struct {
	Param string
	Path  string
}

func (InvalidParamStruct) Error

func (err InvalidParamStruct) Error() string

type InvalidParamType

type InvalidParamType struct {
	Type  any
	Param string
	Path  string
}

func (InvalidParamType) Error

func (err InvalidParamType) Error() string

type ParamTypes

type ParamTypes string
const (
	EMPTY  ParamTypes = ""
	NULL   ParamTypes = "NULL"
	STRING ParamTypes = "string"
	INT    ParamTypes = "int"
	FLOAT  ParamTypes = "float"
	BOOL   ParamTypes = "bool"
	UUID   ParamTypes = "uuid"
)

func StringToParamType

func StringToParamType(value string) ParamTypes

type ParamsConflict

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

func (ParamsConflict) Error

func (err ParamsConflict) Error() string

type PathAlreadyExist

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

func (PathAlreadyExist) Error

func (err PathAlreadyExist) Error() string

type RepeatedParameter

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

func (RepeatedParameter) Error

func (err RepeatedParameter) Error() string

type Route

type Route struct {
	Kind      string
	Path      string
	Param     string
	ParamType ParamTypes
	Handler   RouteHandler
	Routes    Routes
}

type RouteHandler

type RouteHandler func(DWorkContext)

type RouteParams

type RouteParams map[string]any

type Routes

type Routes map[string]*Route

func MakeRouter

func MakeRouter() Routes

func (*Routes) Dump

func (routes *Routes) Dump()

func (*Routes) Enable

func (routes *Routes) Enable()

Enable enables the Routes.

It sets up the HTTP handler for the Routes. When a request comes in, it parses the route and parameters from the URL path and the request method.

If the route is not found or the corresponding handler is nil, it returns a 404 Not Found response.

If the route is found, it creates a DWorkContext with the parsed parameters, the response writer and the request, and passes it to the route's handler function.

func (*Routes) EnableDebug

func (routes *Routes) EnableDebug()

EnableDebug enables the debug mode.

func (*Routes) RegisterRoute

func (routes *Routes) RegisterRoute(verb HTTPVerb, path string, handler RouteHandler) error

RegisterRoute registers a new route in the Routes struct.

The function takes in the HTTPVerb, path, and handler as parameters.

It returns an error if there's an issue parsing the verb or if there are conflicts with the path or parameters.

Jump to

Keyboard shortcuts

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