web

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2022 License: AGPL-3.0 Imports: 48 Imported by: 41

Documentation

Index

Constants

View Source
const CookieAuthName = "auth"

CookieAuthName is the name of the cookie that holds the Authentication Token

View Source
const CookieSessionName = "user_session_id"

CookieSessionName is the name of the cookie that holds the session ID

View Source
const CookieSignUpAuthName = "__signup_auth"

CookieSignUpAuthName is the name of the cookie that holds the temporary Authentication Token

Variables

View Source
var (
	PlainContentType     = "text/plain"
	HTMLContentType      = "text/html"
	JSONContentType      = "application/json"
	XMLContentType       = "application/xml"
	UTF8PlainContentType = PlainContentType + "; charset=utf-8"
	UTF8HTMLContentType  = HTMLContentType + "; charset=utf-8"
	UTF8XMLContentType   = XMLContentType + "; charset=utf-8"
	UTF8JSONContentType  = JSONContentType + "; charset=utf-8"
)

HTMLMimeType is the mimetype for HTML responses

View Source
var (

	//CspPolicyTemplate is the template used to generate the policy
	CspPolicyTemplate = fmt.Sprintf("%s; %s; %s; %s; %s; %s; %s; %s; %s; %s", cspBase, cspDefault, cspStyle, cspScript, cspImage, cspFont, cspObject, cspMedia, cspConnect, cspFrame)
)
View Source
var (
	//ErrContentTypeNotAllowed is used when POSTing a body that is not json
	ErrContentTypeNotAllowed = stdErrors.New("Only Content-Type application/json is allowed")
)

Functions

func AssetsURL added in v0.19.0

func AssetsURL(ctx context.Context, path string, a ...any) string

AssetsURL return the full URL to a tenant-specific static asset It should always return an absolute URL

func BaseURL added in v0.18.1

func BaseURL(ctx context.Context) string

BaseURL return the base URL from given context

func LogoURL added in v0.18.1

func LogoURL(ctx context.Context) string

LogoURL return the full URL to the tenant-specific logo URL

func OAuthBaseURL added in v0.18.1

func OAuthBaseURL(ctx context.Context) string

OAuthBaseURL returns the OAuth base URL used for host-wide OAuth authentication For Single Tenant HostMode, BaseURL is the current BaseURL For Multi Tenant HostMode, BaseURL is //login.{HOST_DOMAIN}

func ParseCookie added in v0.15.0

func ParseCookie(s string) *http.Cookie

ParseCookie return a list of cookie parsed from raw Set-Cookie

func TenantBaseURL added in v0.18.1

func TenantBaseURL(ctx context.Context, tenant *entity.Tenant) string

TenantBaseURL returns base URL for a given tenant

Types

type Cache added in v0.18.1

type Cache struct {
}

Cache provides a Blob backend to the autocert cache.

func NewAutoCertCache added in v0.18.1

func NewAutoCertCache() *Cache

NewAutoCertCache returns a new AutoCert cache using Blob Storage

func (*Cache) Delete added in v0.18.1

func (c *Cache) Delete(ctx context.Context, key string) error

Delete removes a certificate data from the cache under the specified key. If there's no such key in the cache, Delete returns nil.

func (*Cache) Get added in v0.18.1

func (c *Cache) Get(ctx context.Context, key string) ([]byte, error)

Get returns a certificate data for the specified key. If there's no such key, Get returns ErrCacheMiss.

func (*Cache) Put added in v0.18.1

func (c *Cache) Put(ctx context.Context, key string, data []byte) error

Put stores the data in the cache under the specified key.

type CertificateManager added in v0.7.0

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

CertificateManager is used to manage SSL certificates

func NewCertificateManager added in v0.7.0

func NewCertificateManager(ctx context.Context, certFile, keyFile string) (*CertificateManager, error)

NewCertificateManager creates a new CertificateManager

func (*CertificateManager) GetCertificate added in v0.7.0

func (m *CertificateManager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error)

GetCertificate decides which certificate to use It first tries to use loaded certificate for incoming request if it's compatible Otherwise fallsback to a automatically generated certificate by Let's Encrypt

func (*CertificateManager) StartHTTPServer added in v0.9.0

func (m *CertificateManager) StartHTTPServer()

StartHTTPServer creates a new HTTP server on port 80 that is used for the ACME HTTP Challenge

type Context

type Context struct {
	context.Context
	Response Response
	Request  Request
	// contains filtered or unexported fields
}

Context shared between http pipeline

func NewContext added in v0.18.1

func NewContext(engine *Engine, req *http.Request, rw http.ResponseWriter, params StringMap) *Context

NewContext creates a new web Context

func (*Context) AddCookie added in v0.6.0

func (c *Context) AddCookie(name, value string, expires time.Time) *http.Cookie

AddCookie adds a cookie

func (*Context) AddParam added in v0.7.0

func (c *Context) AddParam(name, value string)

AddParam add a single param to route parameters list

func (*Context) Attachment added in v0.12.0

func (c *Context) Attachment(fileName, contentType string, file []byte) error

Attachment returns an attached file

func (*Context) BadRequest

func (c *Context) BadRequest(dict Map) error

BadRequest returns 400 BadRequest with JSON result

func (*Context) BaseURL

func (c *Context) BaseURL() string

BaseURL returns base URL

func (*Context) Bind added in v0.17.0

func (c *Context) Bind(i any) error

Bind context values into given model

func (*Context) BindTo added in v0.4.0

func (c *Context) BindTo(i actions.Actionable) *validate.Result

BindTo context values into given model

func (*Context) Blob added in v0.7.0

func (c *Context) Blob(code int, contentType string, b []byte) error

Blob sends a blob response with status code and content type.

func (*Context) Commit added in v0.11.0

func (c *Context) Commit() error

Commit everything that is pending on current context

func (*Context) ContextID added in v0.12.0

func (c *Context) ContextID() string

ContextID returns the unique id for this context

func (*Context) Engine added in v0.10.0

func (c *Context) Engine() *Engine

Engine returns main HTTP engine

func (*Context) Enqueue added in v0.10.0

func (c *Context) Enqueue(task worker.Task)

Enqueue given task to be processed in background

func (*Context) Failure

func (c *Context) Failure(err error) error

Failure returns a 500 page

func (*Context) Forbidden added in v0.20.0

func (c *Context) Forbidden() error

Forbidden returns a 403 error response

func (*Context) GetMatchedRoutePath added in v0.19.1

func (c *Context) GetMatchedRoutePath() string

GetMatchedRoutePath returns the Matched Route name

func (*Context) Gone added in v0.12.0

func (c *Context) Gone() error

Gone returns a 410 error page

func (*Context) HandleValidation added in v0.4.0

func (c *Context) HandleValidation(result *validate.Result) error

HandleValidation handles given validation result property to return 400 or 500

func (*Context) Image added in v0.18.0

func (c *Context) Image(contentType string, b []byte) error

Image sends an image blob response with status code and content type.

func (*Context) IsAjax

func (c *Context) IsAjax() bool

IsAjax returns true if request is AJAX

func (*Context) IsAuthenticated

func (c *Context) IsAuthenticated() bool

IsAuthenticated returns true if user is authenticated

func (*Context) JSON added in v0.7.0

func (c *Context) JSON(code int, i any) error

JSON returns a JSON response with status code.

func (*Context) NoContent added in v0.7.0

func (c *Context) NoContent(code int) error

NoContent sends a response with no body and a status code.

func (*Context) NotFound

func (c *Context) NotFound() error

NotFound returns a 404 error page

func (*Context) Ok

func (c *Context) Ok(data any) error

Ok returns 200 OK with JSON result

func (*Context) Page

func (c *Context) Page(code int, props Props) error

Page returns a page with given variables

func (*Context) Param added in v0.7.0

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

Param returns parameter as string

func (*Context) ParamAsInt

func (c *Context) ParamAsInt(name string) (int, error)

ParamAsInt returns parameter as int

func (*Context) PermanentRedirect added in v0.17.0

func (c *Context) PermanentRedirect(url string) error

PermanentRedirect the request to a provided URL

func (*Context) QueryParam added in v0.7.0

func (c *Context) QueryParam(key string) string

QueryParam returns querystring parameter for given key

func (*Context) QueryParamAsArray added in v0.10.0

func (c *Context) QueryParamAsArray(key string) []string

QueryParamAsArray returns querystring parameter for given key as an array

func (*Context) QueryParamAsInt added in v0.18.0

func (c *Context) QueryParamAsInt(key string) (int, error)

QueryParamAsInt returns querystring parameter for given key

func (*Context) Redirect added in v0.7.0

func (c *Context) Redirect(url string) error

Redirect the request to a provided URL

func (*Context) RemoveCookie added in v0.6.0

func (c *Context) RemoveCookie(name string)

RemoveCookie removes a cookie

func (*Context) Rollback added in v0.11.0

func (c *Context) Rollback()

Rollback everything that is pending on current context

func (*Context) SessionID added in v0.17.0

func (c *Context) SessionID() string

SessionID returns the current session ID

func (*Context) Set added in v0.7.0

func (c *Context) Set(key any, val any)

Set saves data in the context.

func (*Context) SetCanonicalURL added in v0.18.0

func (c *Context) SetCanonicalURL(rawurl string)

SetCanonicalURL sets the canonical link on the HTTP Response Headers

func (*Context) SetSessionID added in v0.17.0

func (c *Context) SetSessionID(id string)

SetSessionID sets the session ID on current context

func (*Context) SetTenant

func (c *Context) SetTenant(tenant *entity.Tenant)

SetTenant update HTTP context with current tenant

func (*Context) SetUser

func (c *Context) SetUser(user *entity.User)

SetUser update HTTP context with current user

func (*Context) String added in v0.7.0

func (c *Context) String(code int, text string) error

String returns a text response with status code.

func (*Context) Tenant

func (c *Context) Tenant() *entity.Tenant

Tenant returns current tenant

func (*Context) Unauthorized added in v0.4.0

func (c *Context) Unauthorized() error

Unauthorized returns a 401 error response

func (*Context) User

func (c *Context) User() *entity.User

User returns authenticated user

func (*Context) XML added in v0.16.0

func (c *Context) XML(code int, text string) error

XML returns a XML response with status code.

type DefaultBinder added in v0.7.0

type DefaultBinder struct {
}

DefaultBinder is the default HTTP binder

func NewDefaultBinder added in v0.7.0

func NewDefaultBinder() *DefaultBinder

NewDefaultBinder creates a new default binder

func (*DefaultBinder) Bind added in v0.7.0

func (b *DefaultBinder) Bind(target any, c *Context) error

Bind request data to object i

type Engine

type Engine struct {
	context.Context
	// contains filtered or unexported fields
}

Engine is our web engine wrapper

func New

func New() *Engine

New creates a new Engine

func (*Engine) Cache added in v0.18.0

func (e *Engine) Cache() *cache.Cache

Cache returns current cache

func (*Engine) Delete added in v0.15.0

func (e *Engine) Delete(path string, handler HandlerFunc)

Delete handles HTTP DELETE requests

func (*Engine) Get added in v0.15.0

func (e *Engine) Get(path string, handler HandlerFunc)

Get handles HTTP GET requests

func (*Engine) Group

func (e *Engine) Group() *Group

Group creates a new route group

func (*Engine) NotFound added in v0.16.0

func (e *Engine) NotFound(handler HandlerFunc)

NotFound register how to handle routes that are not found

func (*Engine) Post added in v0.15.0

func (e *Engine) Post(path string, handler HandlerFunc)

Post handles HTTP POST requests

func (*Engine) Put added in v0.15.0

func (e *Engine) Put(path string, handler HandlerFunc)

Put handles HTTP PUT requests

func (*Engine) Start

func (e *Engine) Start(address string)

Start the server.

func (*Engine) Stop added in v0.13.0

func (e *Engine) Stop() error

Stop the server.

func (*Engine) Use

func (e *Engine) Use(middleware MiddlewareFunc)

Use adds a middleware to the root engine

func (*Engine) Worker added in v0.10.0

func (e *Engine) Worker() worker.Worker

Worker returns current worker reference

type Group

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

Group is our router group wrapper

func (*Group) Delete added in v0.8.0

func (g *Group) Delete(path string, handler HandlerFunc)

Delete handles HTTP DELETE requests

func (*Group) Get

func (g *Group) Get(path string, handler HandlerFunc)

Get handles HTTP GET requests

func (*Group) Group

func (g *Group) Group() *Group

Group creates a new route group

func (*Group) Post

func (g *Group) Post(path string, handler HandlerFunc)

Post handles HTTP POST requests

func (*Group) Put added in v0.15.0

func (g *Group) Put(path string, handler HandlerFunc)

Put handles HTTP PUT requests

func (*Group) Static

func (g *Group) Static(prefix, root string)

Static return files from given folder

func (*Group) Use

func (g *Group) Use(middleware MiddlewareFunc)

Use adds a middleware to current route stack

type HandlerFunc

type HandlerFunc func(*Context) error

HandlerFunc represents an HTTP handler

type Map added in v0.4.0

type Map map[string]any

Map defines a generic map of type `map[string]any`

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc represents an HTTP middleware

type Props added in v0.13.0

type Props struct {
	Title       string
	Description string
	Page        string
	Data        Map
}

Props defines the data required to render rages

type ReactRenderer added in v0.18.1

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

func NewReactRenderer added in v0.18.1

func NewReactRenderer(scriptPath string) (*ReactRenderer, error)

func (*ReactRenderer) Render added in v0.18.1

func (r *ReactRenderer) Render(u *url.URL, props Map) (string, error)

type Renderer added in v0.7.0

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

Renderer is the default HTML Render

func NewRenderer added in v0.7.0

func NewRenderer() *Renderer

NewRenderer creates a new Renderer

func (*Renderer) Render added in v0.7.0

func (r *Renderer) Render(w io.Writer, statusCode int, props Props, ctx *Context)

Render a template based on parameters

type Request added in v0.14.0

type Request struct {
	Method        string
	ContentLength int64
	Body          string
	IsSecure      bool
	StartTime     time.Time
	URL           *url.URL
	// contains filtered or unexported fields
}

Request wraps the http request object

func WrapRequest added in v0.14.0

func WrapRequest(request *http.Request) Request

WrapRequest returns Fider wrapper of HTTP Request

func (*Request) AddCookie added in v0.14.0

func (r *Request) AddCookie(cookie *http.Cookie)

AddCookie adds a cookie

func (*Request) BaseURL added in v0.18.1

func (r *Request) BaseURL() string

BaseURL returns base URL

func (*Request) Cookie added in v0.14.0

func (r *Request) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie provided in the request.

func (*Request) GetHeader added in v0.14.0

func (r *Request) GetHeader(key string) string

GetHeader returns the value of HTTP header from given key

func (*Request) IsAPI added in v0.15.0

func (r *Request) IsAPI() bool

IsAPI returns true if its a request for an API resource

func (*Request) IsCrawler added in v0.18.0

func (r *Request) IsCrawler() bool

IsCrawler returns true if the request is coming from a crawler

func (*Request) IsCustomDomain added in v0.20.0

func (r *Request) IsCustomDomain() bool

IsCustomDomain returns true if the request was made using a custom domain (CNAME)

func (*Request) SetHeader added in v0.14.0

func (r *Request) SetHeader(key, value string)

SetHeader updates the value of HTTP header of given key

type Response added in v0.19.0

type Response struct {
	Writer     http.ResponseWriter
	StatusCode int
}

Response is a wrapper of http.ResponseWriter with access to the StatusCode

func (Response) Flush added in v0.19.0

func (r Response) Flush()

func (*Response) Header added in v0.19.0

func (r *Response) Header() http.Header

func (Response) Hijack added in v0.19.0

func (r Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*Response) Write added in v0.19.0

func (r *Response) Write(b []byte) (int, error)

func (*Response) WriteHeader added in v0.19.0

func (r *Response) WriteHeader(code int)

type StringMap added in v0.7.0

type StringMap map[string]string

StringMap defines a map of type `map[string]string`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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