core

package module
v0.0.0-...-086cf08 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2019 License: MIT Imports: 25 Imported by: 0

README

core

Documentation

Index

Constants

View Source
const (
	MIME_JSON = `application/json; charset=UTF-8`
	MIME_HTML = `text/html; charset=UTF-8`
)
View Source
const (
	Response_Ok                     = 200
	Response_Created                = 201
	Response_Bad_Request            = 400
	Response_Unauthorized           = 401
	Response_Forbidden              = 403
	Response_Not_Found              = 404
	Response_Unsupported_Media_Type = 415
	Response_Unprocessable_Entity   = 422
	Response_Too_Many_Requests      = 429
	Response_Internal_Server_Error  = 500
)
View Source
const (
	HeaderXRateLimit          = `X-Rate-Limit-Limit`
	HeaderXRateLimitRemaining = `X-Rate-Limit-Remaining`
)
View Source
const (
	ContentType_JSON = "application/json"
)

Variables

View Source
var APP = &App{Router: NewRouter()} // default application

Default application handler

View Source
var DefaultConfig = newConfigStruct()

Default global configuration

Functions

func Base64Encode

func Base64Encode(bv []byte) string

func Clean

func Clean(val string) string

func Run

func Run()

Run function will initiaate default config load and start listening for requests

func SHA1

func SHA1(input string) []byte

func ServeFile

func ServeFile(out Output, path string)

ServeFile this is just for development, file handling (CDN) better done by nginx or other TODO: there can be better alternative just to use http.FileServer

Types

type App

type App struct {
	Router
	Config *configStruct // TODO: for each there is application specific configuration
	// contains filtered or unexported fields
}

App structure represents single application on server server can route multiple applications, in different sub-directory or sub-domain

func New

func New(name string, subdomain bool) *App

New functions is constructor for app structure

func (*App) ServeHTTP

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

func (*App) Sub

func (app *App) Sub(name string, sub *App)

Sub is used to link application module to main application module

type Context

type Context interface {
	Input
	Output
}

Context interface combines input and output interfaces so that RouteFunc accepts single parameter - Context

type Input

type Input interface {
	App() *App

	RequestURI() string
	// Get used for getting GET passed parameters
	Get(key string) (val string)
	// Returns method of request (GET, POST, PUT, ...)
	Method() string
	// Returns header value
	HeaderValue(string) string

	ContentType() string
	// wrapper for http.Request.FormValue
	FormValue(string) string

	CookieValue(string) (string, bool)
	// Return user agent
	UserAgent() string
	// Returns remote IP address
	RemoteAddr() string

	// Returns true if request is marked as AJAX based
	Ajax() bool
	// Access to URL parameters
	Args(int) t.T
	// Access to posted data
	Data() t.Map
	// Provides access to session data
	Session() *session.Session

	// returns body content, JSON post with JSON as content-body
	Body() (result string)

	Request() *http.Request
	// contains filtered or unexported methods
}

Input is interface for routes input handler

type Module

type Module struct {
}

Module is par of app, for each app there is module instance

type Output

type Output interface {
	// writing function
	Write([]byte) (n int, err error)
	WriteString(string) (n int, err error)
	WriteJSON(interface{}) []byte

	// response config
	SetContentType(string)
	Response(int)

	// some heper functions
	SetCookieValue(string, string)
	Redirect(url ...string)
	AddHeader(string, interface{})
	Header() http.Header

	ResponseWriter() http.ResponseWriter
	Flush()
	// contains filtered or unexported methods
}

Output is interface for route output handler

type Route

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

Route handles single route

func (*Route) CSRF

func (route *Route) CSRF(emit, need bool) *Route

CSRF route option for setting CSRF validations

func (*Route) JSON

func (route *Route) JSON() *Route

JSON adds validation for request content so that only requests with content type json is handled

func (*Route) Match

func (route *Route) Match(nameA, nameB string) *Route

Match sets a rule that two input data fields should match

func (*Route) Need

func (route *Route) Need(fields ...string) *Route

Need functions adds validation for mandatory fields

func (*Route) NoCache

func (route *Route) NoCache() *Route

NoCache marks request handler output of route will not be cached in any way to client will be sent headers to not cache response

func (*Route) RateLimit

func (route *Route) RateLimit(rate, per float32) *Route

RateLimit sets routes maximum request rate per second from specific remote IP

func (*Route) RateLimitAuth

func (route *Route) RateLimitAuth(rate, per float32) *Route

RateLimitAuth sets routes maximum request rate per time for authorized users

func (*Route) ReqAuth

func (route *Route) ReqAuth(args ...string) *Route

ReqAuth marks route so that it can be accessed only by authorized session if session is not authorized request is redirected to route that is passed in argument

type RouteFunc

type RouteFunc func(Context)

RouteFunc is function type used in routes

type Router

type Router interface {
	Get(string, RouteFunc) *Route
	Post(string, RouteFunc) *Route
	Put(string, RouteFunc) *Route
	Delete(string, RouteFunc) *Route
	// main routing function
	Route(context Context) bool
	Handle(pattern string, handler http.Handler)
}

Router is interface for implement specific routing engines, for example we have module user which handles users, we can then port module across other projects that uses core

func NewRouter

func NewRouter() Router

NewRouter is constructor for creating router instance for default core router

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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