chc

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2022 License: MIT Imports: 8 Imported by: 2

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.

Documentation Coming Soon/In Progress

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) NewRoute

func (chc *CHC) NewRoute(path string, methods []string, type_ string, headers map[string]string, file string, controller func(request *Request) (response *Response)) *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[string]string
	Headers  map[string]string
	Cookies  map[string]string
	Conn     net.Conn
}

func (*Request) FormDataBody

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

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

func (*Request) JsonArrayBody

func (request *Request) JsonArrayBody() ([]map[string]interface{}, error)

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

func (*Request) JsonBody

func (request *Request) JsonBody() (map[string]interface{}, error)

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

func (*Request) NewResponse added in v1.1.2

func (request *Request) NewResponse() *Response

Create a new response object

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)

Set a cookie for the response

func (*Response) SetHeader

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

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)

Set the response redirect to a url

func (*Response) SetStatusCode

func (response *Response) SetStatusCode(statusCode int)

Set the status code for the response

func (*Response) SetStringBody

func (response *Response) SetStringBody(body string)

Set the response body to a string

type Route

type Route struct {
	Path       string
	Methods    []string
	Type       string
	Headers    map[string]string
	File       string
	Controller func(request *Request) (response *Response)
	Middleware []func(request *Request) (response *Response)
}

func (*Route) AddMiddleware

func (route *Route) AddMiddleware(middleware func(request *Request) (response *Response))

Add a middleware to a route

func (*Route) AddMiddlewares

func (route *Route) AddMiddlewares(middlewares []func(request *Request) (response *Response))

Add multiple middleware to a route

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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