app

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package app contains the default web application server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStats added in v0.19.0

func GetStats() string

func ListenAndServeTLSWithTimeouts added in v0.9.0

func ListenAndServeTLSWithTimeouts(addr, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLSWithTimeouts starts a secure web server with timeouts. The default http server does not have timeouts by default, which leaves the server open to certain attacks that would start a connection, but then very slowly read or write. Timeout values are taken from global variables defined in config, which you can set at init time.

func ListenAndServeWithTimeouts added in v0.9.0

func ListenAndServeWithTimeouts(addr string, handler http.Handler) error

ListenAndServeWithTimeouts starts a web server with timeouts. The default http server does not have timeouts, which leaves the server open to certain attacks that would start a connection, but then very slowly read or write. Timeout values are taken from global variables defined in config, which you can set at init time. This non-secure version is appropriate if you are serving behind another server, like apache or nginx.

func RegisterStaticPath added in v0.0.9

func RegisterStaticPath(
	path string,
	directory string,
	useCacheBuster bool,
	hide []string,
)

RegisterStaticPath registers the given url path such that it points to the given directory in the host file system.

For example, passing "/test", "/my/test/dir" will serve documents out of /my/test/dir whenever a URL has /test in front of it. Only call this during application startup.

hide is a list of file endings for files that should not be served. If a file is searched for, but is not found, a NotFound error will be sent to the http server.

func Shutdown added in v0.19.0

func Shutdown(ctx context.Context) error

Shutdown performs a graceful shutdown of the server, returning any errors found.

func WebsocketMessengerHandler added in v0.14.0

func WebsocketMessengerHandler(w http.ResponseWriter, r *http.Request)

WebsocketMessengerHandler is a handler for Websockets.

Types

type Application

type Application struct {
	base.Base
	// contains filtered or unexported fields
}

Application is the application base, to be embedded in your application

func (*Application) AccessLogHandler added in v0.5.1

func (a *Application) AccessLogHandler(next http.Handler) http.Handler

AccessLogHandler simply logs requests.

func (*Application) BufferedOutputHandler added in v0.13.8

func (a *Application) BufferedOutputHandler(next http.Handler) http.Handler

BufferedOutputHandler manages the buffering of http output. It will save all output in a buffer, and make sure any and all Header sets can happen before writing the buffer out to the stream.

func (*Application) HSTSHandler added in v0.9.0

func (a *Application) HSTSHandler(next http.Handler) http.Handler

HSTSHandler sets the browser to HSTS mode using the given timeout. HSTS will force a browser to accept only HTTPS connections for everything coming from your domain, if the initial page was served over HTTPS. Many browsers already do this. What this additionally does is prevent the user from overriding this. Also, if your certificate is bad or expired, it will NOT allow the user the option of using your website anyways. This should be safe to send in development mode if your local server is not using HTTPS, since the header is ignored if a page is served over HTTP.

Once the HSTS policy has been sent to the browser, it will remember it for the amount of time specified, even if the header is not sent again. However, you can override it by sending another header, and clear it by setting the timeout to 0. Set the timeout to -1 to turn it off. You can also completely override this by implementing this function in your app.go file.

func (*Application) Init

func (a *Application) Init(self ApplicationI)

Init initializes the application base.

func (*Application) MakeAppServer

func (a *Application) MakeAppServer() http.Handler

MakeAppServer creates the handler chain that will handle http requests. There are a ton of ways to do this, 3rd party libraries to help with this, and middlewares you can use. This is a working example, and not a declaration of any "right" way to do this, since it can be very application specific. Generally you must make sure that PutAppContextHandler is called before ServePageHandler in the chain.

func (*Application) PutAppContextHandler added in v0.15.0

func (a *Application) PutAppContextHandler(next http.Handler) http.Handler

PutAppContextHandler is an HTTP handler that adds the application context to the current context.

func (*Application) PutContext

func (a *Application) PutContext(r *http.Request) *http.Request

PutContext puts application data into the context.

func (*Application) PutDbContextHandler added in v0.15.0

func (a *Application) PutDbContextHandler(next http.Handler) http.Handler

PutDbContextHandler is an HTTP handler that adds the database contexts to the current context.

func (*Application) ServeAppMux added in v0.18.0

func (a *Application) ServeAppMux(next http.Handler) http.Handler

ServeAppMux serves up the http2.AppMuxer, which handles REST calls, and dynamically created files.

To use your own AppMuxer, override this function in app.go and create your own.

func (*Application) ServeHTTP

func (a *Application) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves a Goradd form.

func (*Application) ServePageHandler added in v0.18.0

func (a *Application) ServePageHandler(next http.Handler) http.Handler

ServePageHandler processes requests for automated goradd pages.

func (*Application) ServePatternMux added in v0.18.0

func (a *Application) ServePatternMux(next http.Handler) http.Handler

ServePatternMux serves up the http2.PatternMuxer.

The pattern muxer routes patterns early in the handler stack. It is primarily for handlers that do not need the session manager or buffered output, things like static files.

The default version injects a standard http muxer. Override to use your own muxer.

func (*Application) ServeRequestHandler

func (a *Application) ServeRequestHandler() http.Handler

ServeRequestHandler is the last handler on the default call chain. It returns a simple not found error by default.

func (*Application) ServeWebsocketMessengerHandler added in v0.14.0

func (a *Application) ServeWebsocketMessengerHandler(next http.Handler) http.Handler

ServeWebsocketMessengerHandler is the authorization handler for watcher requests. It uses the pagestate as the client id, verifying the page state is valid

func (*Application) SessionHandler

func (a *Application) SessionHandler(next http.Handler) http.Handler

SessionHandler initializes the global session handler. This default version uses the injected global session handler. Feel free to replace it with the session handler of your choice.

func (*Application) SetupDatabaseWatcher added in v0.3.1

func (a *Application) SetupDatabaseWatcher()

SetupDatabaseWatcher injects the global database watcher and the database broadcaster which together detect database changes and then draws controls that are watching for those

func (*Application) SetupErrorHandling added in v0.18.0

func (a *Application) SetupErrorHandling()

SetupErrorHandling prepares the application for various types of error handling

func (*Application) SetupMessenger added in v0.3.1

func (a *Application) SetupMessenger()

SetupMessenger injects the global messenger that permits pub/sub communication between the server and client.

You can use this mechanism to set up your own messaging system for application use too.

func (*Application) SetupPagestateCaching added in v0.13.2

func (a *Application) SetupPagestateCaching()

SetupPagestateCaching sets up the service that saves pagestate information that reflects the state of a goradd form to our go code. The default sets up a one server-one process cache that does not scale, which works great for development, testing, and for moderate amounts of traffic. Override and replace the page cache with one that serializes the page state and saves it to a database to make it scalable.

func (*Application) SetupPaths added in v0.18.0

func (a *Application) SetupPaths()

SetupPaths sets up default path handlers for the application

func (*Application) SetupSessionManager

func (a *Application) SetupSessionManager()

SetupSessionManager sets up the global session manager. The session can be used to save data that is specific to a user and specific to the user's time on a browser. Sessions are often used to save login credentials so that you know the current user is logged in.

The default uses a 3rd party session manager, stores the session in memory, and tracks sessions using cookies. This setup is useful for development, testing, debugging, and for moderately used websites. However, this default does not scale, so if you are launching multiple copies of the app in production, you should override this with a scalable storage mechanism.

func (*Application) StatsHandler added in v0.19.0

func (a *Application) StatsHandler(next http.Handler) http.Handler

StatsHandler records performance statistics on the server.

type ApplicationI

type ApplicationI interface {
	Init()
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	PutContext(*http.Request) *http.Request
	SetupErrorHandling()
	SetupPagestateCaching()
	SetupSessionManager()
	SetupMessenger()
	SetupDatabaseWatcher()
	SetupPaths()
	SessionHandler(next http.Handler) http.Handler
	HSTSHandler(next http.Handler) http.Handler
	AccessLogHandler(next http.Handler) http.Handler
	PutDbContextHandler(next http.Handler) http.Handler
	ServeAppMux(next http.Handler) http.Handler
	ServePatternMux(next http.Handler) http.Handler
}

ApplicationI allows you to override default functionality in the ApplicationBase. The main routine offers a way of creating mock applications, and alternate versions of the application from the default

Jump to

Keyboard shortcuts

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