web

package
v0.0.0-...-acda5ee Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: GPL-3.0 Imports: 47 Imported by: 0

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

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

func BaseURL(ctx context.Context) string

BaseURL return the base URL from given context

func LogoURL

func LogoURL(ctx context.Context) string

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

func OAuthBaseURL

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

func ParseCookie(s string) *http.Cookie

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

func TenantBaseURL

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

TenantBaseURL returns base URL for a given tenant

Types

type Cache

type Cache struct {
}

Cache provides a Blob backend to the autocert cache.

func NewAutoCertCache

func NewAutoCertCache() *Cache

NewAutoCertCache returns a new AutoCert cache using Blob Storage

func (*Cache) Delete

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

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

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

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

CertificateManager is used to manage SSL certificates

func NewCertificateManager

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

NewCertificateManager creates a new CertificateManager

func (*CertificateManager) GetCertificate

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

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

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

NewContext creates a new web Context

func (*Context) AddCookie

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

AddCookie adds a cookie

func (*Context) AddParam

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

AddParam add a single param to route parameters list

func (*Context) Attachment

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

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

Bind context values into given model

func (*Context) BindTo

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

BindTo context values into given model

func (*Context) Blob

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

func (c *Context) Commit() error

Commit everything that is pending on current context

func (*Context) ContextID

func (c *Context) ContextID() string

ContextID returns the unique id for this context

func (*Context) Engine

func (c *Context) Engine() *Engine

Engine returns main HTTP engine

func (*Context) Enqueue

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

func (c *Context) Forbidden() error

Forbidden returns a 403 error response

func (*Context) GetMatchedRoutePath

func (c *Context) GetMatchedRoutePath() string

GetMatchedRoutePath returns the Matched Route name

func (*Context) Gone

func (c *Context) Gone() error

Gone returns a 410 error page

func (*Context) HandleValidation

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

HandleValidation handles given validation result property to return 400 or 500

func (*Context) Image

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

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

JSON returns a JSON response with status code.

func (*Context) NoContent

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

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

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

PermanentRedirect the request to a provided URL

func (*Context) QueryParam

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

QueryParam returns querystring parameter for given key

func (*Context) QueryParamAsArray

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

QueryParamAsArray returns querystring parameter for given key as an array

func (*Context) QueryParamAsInt

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

QueryParamAsInt returns querystring parameter for given key

func (*Context) Redirect

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

Redirect the request to a provided URL

func (*Context) RemoveCookie

func (c *Context) RemoveCookie(name string)

RemoveCookie removes a cookie

func (*Context) Rollback

func (c *Context) Rollback()

Rollback everything that is pending on current context

func (*Context) SessionID

func (c *Context) SessionID() string

SessionID returns the current session ID

func (*Context) Set

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

Set saves data in the context.

func (*Context) SetCanonicalURL

func (c *Context) SetCanonicalURL(rawurl string)

SetCanonicalURL sets the canonical link on the HTTP Response Headers

func (*Context) SetSessionID

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

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

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

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

XML returns a XML response with status code.

type DefaultBinder

type DefaultBinder struct {
}

DefaultBinder is the default HTTP binder

func NewDefaultBinder

func NewDefaultBinder() *DefaultBinder

NewDefaultBinder creates a new default binder

func (*DefaultBinder) Bind

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

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

Cache returns current cache

func (*Engine) Delete

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

Delete handles HTTP DELETE requests

func (*Engine) Get

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

func (e *Engine) NotFound(handler HandlerFunc)

NotFound register how to handle routes that are not found

func (*Engine) Post

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

Post handles HTTP POST requests

func (*Engine) Put

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

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

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

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

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

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

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

Props defines the data required to render rages

type ReactRenderer

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

func NewReactRenderer

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

func (*ReactRenderer) Render

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

type Renderer

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

Renderer is the default HTML Render

func NewRenderer

func NewRenderer() *Renderer

NewRenderer creates a new Renderer

func (*Renderer) Render

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

Render a template based on parameters

type Request

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

func WrapRequest(request *http.Request) Request

WrapRequest returns Templates wrapper of HTTP Request

func (*Request) AddCookie

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

AddCookie adds a cookie

func (*Request) BaseURL

func (r *Request) BaseURL() string

BaseURL returns base URL

func (*Request) Cookie

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

Cookie returns the named cookie provided in the request.

func (*Request) GetHeader

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

GetHeader returns the value of HTTP header from given key

func (*Request) IsAPI

func (r *Request) IsAPI() bool

IsAPI returns true if its a request for an API resource

func (*Request) IsCrawler

func (r *Request) IsCrawler() bool

IsCrawler returns true if the request is coming from a crawler

func (*Request) IsCustomDomain

func (r *Request) IsCustomDomain() bool

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

func (*Request) SetHeader

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

SetHeader updates the value of HTTP header of given key

type Response

type Response struct {
	Writer     http.ResponseWriter
	StatusCode int
}

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

func (Response) Flush

func (r Response) Flush()

func (*Response) Header

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

func (Response) Hijack

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

func (*Response) Write

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

func (*Response) WriteHeader

func (r *Response) WriteHeader(code int)

type StringMap

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