engine

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: BSD-3-Clause Imports: 79 Imported by: 0

Documentation

Overview

Package engine provides the server configuration struct and several functions for serving files over HTTP

Index

Constants

View Source
const (
	// Version number. Stable API within major version numbers.
	Version = 2.0
)

Variables

View Source
var (
	ErrVersion  = errors.New("only showing version information")
	ErrDatabase = errors.New("could not find a usable database backend")
)

ErrVersion is returned when the initialization quits because all that is done is showing version information

Functions

func AtShutdown

func AtShutdown(shutdownFunction func())

AtShutdown adds a function to the list of functions that will be ran at shutdown

func ValidGCSS

func ValidGCSS(gcssdata []byte, errorReturn chan error)

ValidGCSS checks if the given data is valid GCSS. The error value is returned on the channel.

Types

type Config

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

Config is the main structure for the Algernon server. It contains all the state and settings. The order of the fields has been decided by the "fieldalignment" utility.

func New

func New(versionString, description string) (*Config, error)

New creates a new server configuration based using the default values

func (*Config) AmberPage

func (ac *Config) AmberPage(w http.ResponseWriter, req *http.Request, filename string, amberdata []byte, funcs template.FuncMap)

AmberPage the given source bytes (in Amber) converted to HTML, to a writer. The filename is only used in error messages, if any.

func (*Config) ClientCanGzip

func (ac *Config) ClientCanGzip(req *http.Request) bool

ClientCanGzip checks if the client supports gzip compressed responses

func (*Config) Close

func (ac *Config) Close()

Close removes the temporary directory

func (*Config) CombinedLogFormat

func (ac *Config) CombinedLogFormat(req *http.Request, statusCode int, byteSize int64) string

CombinedLogFormat returns a line with the data that is available at the start of a request handler. The log line is in CLF, similar to the Common log format, but with two extra fields. See also: https://httpd.apache.org/docs/1.3/logs.html#combined

func (*Config) CommonLogFormat

func (ac *Config) CommonLogFormat(req *http.Request, statusCode int, byteSize int64) string

CommonLogFormat returns a line with the data that is available at the start of a request handler. The log line is in NCSA format, the same log format used by Apache. Fields where data is not available are indicated by a "-". See also: https://en.wikipedia.org/wiki/Common_Log_Format

func (*Config) DataToClient

func (ac *Config) DataToClient(w http.ResponseWriter, req *http.Request, filename string, data []byte)

DataToClient is a helper function for sending file data (that might be cached) to a HTTP client

func (*Config) DatabaseBackend

func (ac *Config) DatabaseBackend() (pinterface.IPermissions, error)

DatabaseBackend tries to retrieve a database backend, using one of the available permission middleware packages. It assign a name to dbName (used for the status output) and returns a IPermissions struct.

func (*Config) DirPage

func (ac *Config) DirPage(w http.ResponseWriter, req *http.Request, rootdir, dirname, theme string)

DirPage serves a directory, using index.* files, if present. The directory must exist. rootdir is the base directory (can be ".") dirname is the specific directory that is to be served (should never be ".")

func (*Config) DirectoryListing

func (ac *Config) DirectoryListing(w http.ResponseWriter, req *http.Request, rootdir, dirname, theme string)

DirectoryListing serves the given directory as a web page with links the the contents

func (*Config) FilePage

func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, _ string)

FilePage tries to serve a single file. The file must exist. Must be given a full filename.

func (*Config) GCSSPage

func (ac *Config) GCSSPage(w http.ResponseWriter, req *http.Request, filename string, gcssdata []byte)

GCSSPage writes the given source bytes (in GCSS) converted to CSS, to a writer. The filename is only used in the error message, if any.

func (*Config) GenerateShutdownFunction

func (ac *Config) GenerateShutdownFunction(gracefulServer *graceful.Server) func()

GenerateShutdownFunction generates a function that will run the postponed shutdown functions. Note that gracefulServer can be nil. It's only used for finding out if the server was interrupted (ctrl-c or killed, SIGINT/SIGTERM)

func (*Config) HyperAppPage

func (ac *Config) HyperAppPage(w http.ResponseWriter, req *http.Request, filename string, jsxdata []byte)

HyperAppPage writes the given source bytes (in JSX for HyperApp) converted to JS, to a writer. The filename is only used in the error message, if any.

func (*Config) Info

func (ac *Config) Info() string

Info returns a string with various info about the current configuration

func (*Config) InsertAutoRefresh

func (ac *Config) InsertAutoRefresh(req *http.Request, htmldata []byte) []byte

InsertAutoRefresh inserts JavaScript code to the page that makes the page refresh itself when the source files changes. The JavaScript depends on the event server being available. If JavaScript can not be inserted, return the original data. Assumes that the given htmldata is actually HTML (looks for body/head/html tags when inserting a script tag)

func (*Config) JSXPage

func (ac *Config) JSXPage(w http.ResponseWriter, req *http.Request, filename string, jsxdata []byte)

JSXPage writes the given source bytes (in JSX) converted to JS, to a writer. The filename is only used in the error message, if any.

func (*Config) ListenAndServeQUIC

func (ac *Config) ListenAndServeQUIC(mux http.Handler, mut *sync.Mutex, justServeRegularHTTP chan bool, servingHTTPS *bool)

ListenAndServeQUIC attempts to serve the given http.Handler over QUIC/HTTP3, then reports back any errors when done serving.

func (*Config) LoadBasicSystemFunctions

func (ac *Config) LoadBasicSystemFunctions(L *lua.LState)

LoadBasicSystemFunctions loads functions related to logging, markdown and the current server directory into the given Lua state

func (*Config) LoadBasicWeb

func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.LState, filename string, flushFunc func(), httpStatus *FutureStatus)

LoadBasicWeb loads functions related to handling requests, outputting data to the browser, setting headers, pretty printing and dealing with the directory where files are being served, into the given Lua state.

func (*Config) LoadCacheFunctions

func (ac *Config) LoadCacheFunctions(L *lua.LState)

LoadCacheFunctions loads functions related to caching into the given Lua state

func (*Config) LoadCommonFunctions

func (ac *Config) LoadCommonFunctions(w http.ResponseWriter, req *http.Request, filename string, L *lua.LState, flushFunc func(), httpStatus *FutureStatus)

LoadCommonFunctions adds most of the available Lua functions in algernon to the given Lua state struct

func (*Config) LoadJFile

func (ac *Config) LoadJFile(L *lua.LState, scriptdir string)

LoadJFile makes functions related to building a library of Lua code available

func (*Config) LoadLuaFunctionsForREPL

func (ac *Config) LoadLuaFunctionsForREPL(L *lua.LState, o *textoutput.TextOutput)

LoadLuaFunctionsForREPL exports the various Lua functions that might be needed in the REPL

func (*Config) LoadLuaHandlerFunctions

func (ac *Config) LoadLuaHandlerFunctions(L *lua.LState, filename string, mux *http.ServeMux, addDomain bool, httpStatus *FutureStatus, theme string)

LoadLuaHandlerFunctions makes functions related to handling HTTP requests available to Lua scripts

func (*Config) LoadPluginFunctions

func (ac *Config) LoadPluginFunctions(L *lua.LState, o *textoutput.TextOutput)

LoadPluginFunctions takes a Lua state and a TextOutput (the TextOutput struct should be nil if not in a REPL)

func (*Config) LoadRenderFunctions

func (ac *Config) LoadRenderFunctions(w http.ResponseWriter, _ *http.Request, L *lua.LState)

LoadRenderFunctions adds functions related to rendering text to the given Lua state struct

func (*Config) LoadServeFile

func (ac *Config) LoadServeFile(w http.ResponseWriter, req *http.Request, L *lua.LState, filename string)

LoadServeFile exposes functions for serving other files to Lua

func (*Config) LoadServerConfigFunctions

func (ac *Config) LoadServerConfigFunctions(L *lua.LState, filename string) error

LoadServerConfigFunctions makes functions related to server configuration and permissions available to the given Lua struct.

func (*Config) LogAccess

func (ac *Config) LogAccess(req *http.Request, statusCode int, byteSize int64)

LogAccess creates one entry in the access log, given a http.Request, a HTTP status code and the amount of bytes that have been transferred.

func (*Config) Lua2funcMap

func (ac *Config) Lua2funcMap(w http.ResponseWriter, req *http.Request, filename, luafilename, ext string, errChan chan error, funcMapChan chan template.FuncMap)

Lua2funcMap runs in a Lua file and returns the functions as a template.FuncMap (or an error)

func (*Config) LuaFunctionMap

func (ac *Config) LuaFunctionMap(w http.ResponseWriter, req *http.Request, luadata []byte, filename string) (template.FuncMap, error)

LuaFunctionMap returns the functions available in the given Lua code as functions in a map that can be used by templates.

Note that the lua functions must only accept and return strings and that only the first returned value will be accessible. The Lua functions may take an optional number of arguments.

func (*Config) MarkdownPage

func (ac *Config) MarkdownPage(w http.ResponseWriter, req *http.Request, data []byte, filename string)

MarkdownPage write the given source bytes as markdown wrapped in HTML to a writer, with a title

func (*Config) MustServe

func (ac *Config) MustServe(mux *http.ServeMux) error

MustServe sets up a server with handlers

func (*Config) NewGracefulServer

func (ac *Config) NewGracefulServer(mux *http.ServeMux, http2support bool, addr string) *graceful.Server

NewGracefulServer creates a new graceful server configuration

func (*Config) OpenURL

func (ac *Config) OpenURL(host, cPort string, httpsPrefix bool)

OpenURL tries to open an URL with the system browser

func (*Config) PongoHandler

func (ac *Config) PongoHandler(w http.ResponseWriter, req *http.Request, filename, ext string)

PongoHandler renders and serves a Pongo2 template

func (*Config) PongoPage

func (ac *Config) PongoPage(w http.ResponseWriter, req *http.Request, filename string, pongodata []byte, funcs template.FuncMap)

PongoPage write the given source bytes (ina Pongo2) converted to HTML, to a writer. The filename is only used in error messages, if any.

func (*Config) PrettyError

func (ac *Config) PrettyError(w http.ResponseWriter, req *http.Request, filename string, filebytes []byte, errormessage, lang string)

PrettyError serves an informative error page to the user Takes a ResponseWriter, title (can be empty), filename, filebytes, errormessage and programming/scripting/template language (i.e. "lua". Can be empty).

func (*Config) REPL

func (ac *Config) REPL(ready, done chan bool) error

REPL provides a "Read Eval Print" loop for interacting with Lua. A variety of functions are exposed to the Lua state.

func (*Config) ReadAndLogErrors

func (ac *Config) ReadAndLogErrors(w http.ResponseWriter, filename, ext string) (*datablock.DataBlock, error)

ReadAndLogErrors tries to read a file, and logs an error if it could not be read

func (*Config) RegisterHandlers

func (ac *Config) RegisterHandlers(mux *http.ServeMux, handlePath, servedir string, addDomain bool)

RegisterHandlers configures the given mutex and request limiter to handle HTTP requests

func (*Config) RunConfiguration

func (ac *Config) RunConfiguration(filename string, mux *http.ServeMux, withHandlerFunctions bool) error

RunConfiguration runs a Lua file as a configuration script. Also has access to the userstate and permissions. Returns an error if there was a problem with running the lua script, otherwise nil. perm can be nil, but then several Lua functions will not be exposed.

The idea is not to change the Lua struct or the luapool, but to set the configuration variables with the given Lua configuration script.

luaHandler is a flag that lets Lua functions like "handle" and "servedir" be available or not.

func (*Config) RunLua

func (ac *Config) RunLua(w http.ResponseWriter, req *http.Request, filename string, flushFunc func(), fust *FutureStatus) error

RunLua uses a Lua file as the HTTP handler. Also has access to the userstate and permissions. Returns an error if there was a problem with running the lua script, otherwise nil.

func (*Config) SCSSPage

func (ac *Config) SCSSPage(w http.ResponseWriter, req *http.Request, filename string, scssdata []byte)

SCSSPage writes the given source bytes (in SCSS) converted to CSS, to a writer. The filename is only used in the error message, if any.

func (*Config) Serve

func (ac *Config) Serve(mux *http.ServeMux, done, ready chan bool) error

Serve HTTP, HTTP/2 and/or HTTPS. Returns an error if unable to serve, or nil when done serving.

func (*Config) ServeStaticFile

func (ac *Config) ServeStaticFile(filename, colonPort string) error

ServeStaticFile is a convenience function for serving only a single file. It can be used as a quick and easy way to view a README.md file. Will also serve local images if the resulting HTML contains them.

func (*Config) ServerHeaders

func (ac *Config) ServerHeaders(w http.ResponseWriter)

ServerHeaders sets the HTTP headers that are set before anything else

func (*Config) SetFileStatCache

func (ac *Config) SetFileStatCache(fs *datablock.FileStat)

SetFileStatCache can be used to set a different FileStat cache than the default one

type DirConfig

type DirConfig struct {
	Main struct {
		Title string
		Theme string
	}
}

DirConfig keeps a directory listing configuration

type FutureStatus

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

FutureStatus is useful when redirecting in combination with writing to a buffer before writing to a client. May contain more fields in the future.

type ReverseProxy added in v1.15.0

type ReverseProxy struct {
	PathPrefix string
	Endpoint   url.URL
}

ReverseProxy holds which path prefix (like "/api") should be sent where (like "http://localhost:8080")

func (*ReverseProxy) DoProxyPass added in v1.15.0

func (rp *ReverseProxy) DoProxyPass(req http.Request) (*http.Response, error)

DoProxyPass tries to proxy the given http.Request to where the ReverseProxy points

type ReverseProxyConfig added in v1.15.0

type ReverseProxyConfig struct {
	ReverseProxies []ReverseProxy
	// contains filtered or unexported fields
}

ReverseProxyConfig holds several "path prefix --> URL" ReverseProxy structs, together with structures that speeds up the prefix matching.

func NewReverseProxyConfig added in v1.15.0

func NewReverseProxyConfig() *ReverseProxyConfig

NewReverseProxyConfig creates a new and empty ReverseProxyConfig struct

func (*ReverseProxyConfig) Add added in v1.15.0

func (rc *ReverseProxyConfig) Add(rp *ReverseProxy)

Add can add a ReverseProxy and will also (re-)initialize the internal proxy matcher

func (*ReverseProxyConfig) FindMatchingReverseProxy added in v1.15.0

func (rc *ReverseProxyConfig) FindMatchingReverseProxy(path string) *ReverseProxy

FindMatchingReverseProxy checks if the given URL path should be proxied

func (*ReverseProxyConfig) Init added in v1.15.0

func (rc *ReverseProxyConfig) Init()

Init prepares the proxyMatcher and prefix2rproxy fields according to the ReverseProxy structs

Jump to

Keyboard shortcuts

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