chc

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: MIT Imports: 8 Imported by: 0

README

What is CHC?

* CHC is a library for building out HTTP Routes and Controllers completely made in GO.

What is a Route?

* A Route is a collection of HTTP methods and URLs that can be used to respond to requests.

What is a Controller?

* A Controller is how a Route is mapped to a specific function.

see examples here

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CHC

type CHC struct {
	Routes  []*Route
	Logging bool
}

func NewCHC

func NewCHC() *CHC

Create a new CHC object

func (*CHC) AddRoute

func (chc *CHC) AddRoute(route *Route)

Add a route to CHC

func (*CHC) AddRoutes

func (chc *CHC) AddRoutes(routes []*Route)

Add multiple routes to CHC

func (*CHC) GetEnv

func (chc *CHC) GetEnv(key string) string

Get Environment Variable from the given key

func (*CHC) Listen

func (chc *CHC) Listen(host string, port int)

Start the CHC server on the given address and port

func (*CHC) LoadEnv

func (chc *CHC) LoadEnv(filePath string)

Load Environment Variables from the given file

func (*CHC) NewJsonArray added in v1.0.3

func (chc *CHC) NewJsonArray() []map[any]any

Make a new json array

func (*CHC) NewJsonObject added in v1.0.3

func (chc *CHC) NewJsonObject() map[any]any

Make a new json object

func (*CHC) NewRoute

func (chc *CHC) NewRoute() *Route

Create a new route object

func (*CHC) RequestLogging

func (chc *CHC) RequestLogging(logging bool)

Turn Request Logging on or off (if using NewCHC function default is on, otherwise default is off)

type Request

type Request struct {
	Method   string
	URL      string
	Protocol string
	Body     string
	Params   map[any]any
	Headers  map[any]any
	Cookies  map[any]any
	Conn     net.Conn
}

func (*Request) FormDataBody

func (request *Request) FormDataBody() (map[any]any, error)

Parse the request body as Form Data and return the result as a map

func (*Request) JsonArrayBody

func (request *Request) JsonArrayBody() ([]map[any]any, error)

Parse the request body as a JSON array and return the result as a map

func (*Request) JsonBody

func (request *Request) JsonBody() (map[any]any, error)

Parse the request body as a JSON object and return the result as a map

type Response

type Response struct {
	StatusCode int
	Body       string
	Headers    map[string]string
	Cookies    map[string]string
	Conn       net.Conn
}

func (*Response) GetImageBytes

func (response *Response) GetImageBytes(filepath string) ([]byte, error)

Get the bytes from and image file

func (*Response) LoadHtmlFile

func (response *Response) LoadHtmlFile(filePath string) error

Load a HTML file from a path and set the content type to text/html

func (*Response) LoadImageFile

func (response *Response) LoadImageFile(filePath string) error

Set the response to an image from a path

func (*Response) SetCookie

func (response *Response) SetCookie(key string, value string) *Response

Set a cookie for the response

func (*Response) SetHeader

func (response *Response) SetHeader(key string, value string) *Response

Set a header for the response

func (*Response) SetJsonArrayBody

func (response *Response) SetJsonArrayBody(array []map[string]interface{}) error

Set the response body to a json array

func (*Response) SetJsonObjectBody

func (response *Response) SetJsonObjectBody(object map[string]interface{}) error

Set the response body to a json object

func (*Response) SetRedirect

func (response *Response) SetRedirect(url string) *Response

Set the response redirect to a url

func (*Response) SetStatusCode

func (response *Response) SetStatusCode(statusCode int) *Response

Set the status code for the response

func (*Response) SetStringBody

func (response *Response) SetStringBody(body string) *Response

Set the response body to a string

type Route

type Route struct {
	Path        string
	Methods     []string
	Headers     map[any]any
	File        string
	Controller  func(request *Request, response *Response) (response_ *Response)
	Middlewares []func(response *Response) (response_ *Response, next bool)
}

func (*Route) AddMiddleware

func (route *Route) AddMiddleware(middleware func(response *Response) (response_ *Response, next bool))

Add a middleware to a route

func (*Route) AddMiddlewares

func (route *Route) AddMiddlewares(middlewares []func(response *Response) (response_ *Response, next bool))

Add multiple middlewares to a route

Jump to

Keyboard shortcuts

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