simplehttp

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package simplehttp provides an http.Handler that makes it easy to serve Vugu applications. Useful for development and production.

The idea is that the common behaviors needed to serve a Vugu site are readily available in one place. If you require more functionality than simplehttp provides, nearly everything it does is available in the github.com/vugu/vugu package and you can construct what you need from its parts. That said, simplehttp should make it easy to start:

// dev flag enables most common development features
// including rebuild your .wasm upon page reload
dev := true
h := simplehttp.New(dir, dev)

After creation, some flags are available for tuning, e.g.:

h.EnableGenerate = true // upon page reload run "go generate ."
h.DisableBuildCache = true // do not try to cache build results during development, just rebuild every time
h.ParserGoPkgOpts.SkipRegisterComponentTypes = true // do not generate component registration init() stuff

Since it's just a regular http.Handler, starting a webserver is as simple as:

log.Fatal(http.ListenAndServe("127.0.0.1:5678", h))

Index

Constants

This section is empty.

Variables

View Source
var DefaultIsPageFunc = func(r *http.Request) bool {

	return path.Ext(path.Clean("/"+r.URL.Path)) == ""
}

DefaultIsPageFunc will return true for any request to a path with no file extension.

View Source
var DefaultPageTemplateSource = `` /* 1416-byte string literal not displayed */

DefaultPageTemplateSource a useful default HTML template for serving pages.

View Source
var DefaultStaticData = make(map[string]interface{}, 4)

DefaultStaticData is a map of static things added to the return value of DefaultTemplateDataFunc. Provides a quick and dirty way to do things like add CSS files to every page.

View Source
var DefaultTemplateDataFunc = func(r *http.Request) interface{} {
	ret := map[string]interface{}{
		"Request": r,
	}
	for k, v := range DefaultStaticData {
		ret[k] = v
	}
	return ret
}

DefaultTemplateDataFunc is the default behavior for making template data. It returns a map with "Request" set to r and all elements of DefaultStaticData added to it.

Functions

func FilteredFileServer

func FilteredFileServer(pattern *regexp.Regexp, fs http.FileSystem) http.Handler

FilteredFileServer is similar to the standard librarie's http.FileServer but the handler it returns will refuse to serve any files which don't match the specified regexp pattern after running through path.Clean(). The idea is to make it easy to serve only specific kinds of static files from a directory. If pattern does not match a 404 will be returned. Be sure to include a trailing "$" if you are checking for file extensions, so it only matches the end of the path, e.g. "[.](css|js)$"

Types

type PageHandler

type PageHandler struct {
	Template         *template.Template
	TemplateDataFunc func(r *http.Request) interface{}
}

PageHandler executes a Go template and responsds with the page.

func (*PageHandler) ServeHTTP

func (h *PageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type SimpleHandler

type SimpleHandler struct {
	Dir string // project directory

	EnableBuildAndServe          bool                 // enables the build-and-serve sequence for your wasm binary - useful for dev, should be off in production
	EnableGenerate               bool                 // if true calls `go generate` (requires EnableBuildAndServe)
	ParserGoPkgOpts              *gen.ParserGoPkgOpts // if set enables running ParserGoPkg with these options (requires EnableBuildAndServe)
	DisableBuildCache            bool                 // if true then rebuild every time instead of trying to cache (requires EnableBuildAndServe)
	DisableTimestampPreservation bool                 // if true don't try to keep timestamps the same for files that are byte for byte identical (requires EnableBuildAndServe)
	MainWasmPath                 string               // path to serve main wasm file from, in dev mod defaults to "/main.wasm" (requires EnableBuildAndServe)
	WasmExecJsPath               string               // path to serve wasm_exec.js from after finding in the local Go installation, in dev mode defaults to "/wasm_exec.js"

	IsPage      func(r *http.Request) bool // func that returns true if PageHandler should serve the request
	PageHandler http.Handler               // returns the HTML page

	StaticHandler http.Handler // returns static assets from Dir with appropriate filtering or appropriate error
	// contains filtered or unexported fields
}

SimpleHandler provides common web serving functionality useful for building Vugu sites.

func New

func New(dir string, dev bool) *SimpleHandler

New returns an SimpleHandler ready to serve using the specified directory. The dev flag indicates if development functionality is enabled. Settings on SimpleHandler may be tuned more specifically after creation, this function just returns sensible defaults for development or production according to if dev is true or false.

func (*SimpleHandler) ServeHTTP

func (h *SimpleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

Jump to

Keyboard shortcuts

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