vugu: github.com/vugu/vugu/devutil Index | Files

package devutil

import "github.com/vugu/vugu/devutil"

Package devutil has tooling to make Vugu development simpler.

Index

Package Files

compiler.go doc.go file-server.go handlers.go mux.go static.go tinygo-compiler.go

Variables

var DefaultAutoReloadIndex = DefaultIndex.Replace(
    "<!-- scripts -->",
    "<script src=\"http://localhost:8324/auto-reload.js\"></script>\n<!-- scripts -->")

DefaultAutoReloadIndex is like DefaultIndex but also includes a script tag to load auto-reload.js from the default URL.

var DefaultIndex = StaticContent("" /* 1402 byte string literal not displayed */)

DefaultIndex is the default index.html content for a development Vugu app. The exact text `<title>Vugu App</title>`, `<!-- styles -->` and `<!-- scripts -->` are meant to be replaced as needed if you quickly need to hack in CSS or JS references for a development Vugu application. If you need more control than that, just copy it into your application.

var DefaultTinygoDockerImage = "vugu/tinygo-dev:latest" // this is temporary until we can smooth things out with tinygo/tinygo:latest

DefaultTinygoDockerImage is used as the docker image for Tinygo unless overridden.

var NoFileExt = RequestMatcherFunc(func(r *http.Request) bool {
    return path.Ext(path.Clean("/"+r.URL.Path)) == ""
})

NoFileExt is a RequestMatcher that will return true for all paths which do not have a file extension.

type Compiler Uses

type Compiler interface {
    Execute() (outpath string, err error)
}

Compiler is implemented by WasmCompiler and TinygoCompiler.

type FileServer Uses

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

FileServer is similar to http.FileServer but has some options and behavior differences more useful for Vugu programs. The following rules apply when serving http responses:

If the path is a directory but does not end with a slash it is redirected to be with a slash.

If the path is a directory and ends with a slash then if it contains an index.html file that is served.

If the path is a directory and ends with a slash and has no index.html, if listings are enabled a listing will be returned.

If the path does not exist but exists when .html is appended to it then that file is served.

For anything else the handler for the not-found case is called, or if not set then a 404.html will be searched for and if that's not present http.NotFound is called.

Directory listings are disabled by default due to security concerns but can be enabled with SetListings.

func NewFileServer Uses

func NewFileServer() *FileServer

NewFileServer returns a FileServer instance. Before using you must set FileSystem to serve from by calling SetFileSystem or SetDir.

func (*FileServer) ServeHTTP Uses

func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler with the appropriate behavior.

func (*FileServer) SetDir Uses

func (fs *FileServer) SetDir(dir string) *FileServer

SetDir is short for SetFileSystem(http.Dir(dir))

func (*FileServer) SetFileSystem Uses

func (fs *FileServer) SetFileSystem(fsys http.FileSystem) *FileServer

SetFileSystem sets the FileSystem to use when serving files.

func (*FileServer) SetListings Uses

func (fs *FileServer) SetListings(v bool) *FileServer

SetListings enables or disables automatic directory listings when a directory is indicated in the URL path.

func (*FileServer) SetNotFoundHandler Uses

func (fs *FileServer) SetNotFoundHandler(h http.Handler) *FileServer

SetNotFoundHandler sets the handle used when no applicable file can be found.

type MainWasmHandler Uses

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

MainWasmHandler calls WasmCompiler.Build and responds with the resulting .wasm file.

func NewMainWasmHandler Uses

func NewMainWasmHandler(wc Compiler) *MainWasmHandler

NewMainWasmHandler returns an initialized MainWasmHandler.

func (*MainWasmHandler) ServeHTTP Uses

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

ServeHTTP implements http.Handler.

type Mux Uses

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

Mux is simple HTTP request multiplexer that has more generally useful behavior for Vugu development than http.ServeMux. Routes are considered in the order they are added.

func NewMux Uses

func NewMux() *Mux

NewMux returns a new Mux.

func (*Mux) Default Uses

func (m *Mux) Default(h http.Handler) *Mux

Default sets the defualt handler to be called if no other matches were found.

func (*Mux) Exact Uses

func (m *Mux) Exact(name string, h http.Handler) *Mux

Exact adds an exact route match. If path.Clean("/"+r.URL.Path)==name then h is called to handle the request.

func (*Mux) Match Uses

func (m *Mux) Match(rm RequestMatcher, h http.Handler) *Mux

Match adds a route that is used if the provided RequestMatcher returns true.

func (*Mux) ServeHTTP Uses

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type RequestMatcher Uses

type RequestMatcher interface {
    RequestMatch(r *http.Request) bool
}

RequestMatcher describes something that can say yes/no if a request matches. We use it here to mean "should this path be followed in order to answer this request".

type RequestMatcherFunc Uses

type RequestMatcherFunc func(r *http.Request) bool

RequestMatcherFunc implements RequestMatcher as a function.

func (RequestMatcherFunc) RequestMatch Uses

func (f RequestMatcherFunc) RequestMatch(r *http.Request) bool

RequestMatch implements RequestMatcher.

type StaticContent Uses

type StaticContent string

StaticContent implements http.Handler and serves the HTML content in this string.

func (StaticContent) Replace Uses

func (sc StaticContent) Replace(old, new string) StaticContent

Replace performs a single string replacement on this StaticContent and returns the new value.

func (StaticContent) ServeHTTP Uses

func (sc StaticContent) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type StaticFilePath Uses

type StaticFilePath string

StaticFilePath implements http.Handler and serves the file at this path.

func (StaticFilePath) ServeHTTP Uses

func (sfp StaticFilePath) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type TinygoCompiler Uses

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

TinygoCompiler provides a convenient way to build a program via Tinygo into Wasm. This implementation by default uses Docker to download and run Tinygo, and provides methods to handle mapping local directories into the Tinygo docker filesystem and for making other dependencies available by calling `go get` on them. This approach might change once Tinygo has module support, but for now the idea is it makes it reasonably convenient to integration Tinygo into the workflow for Vugu app.

func MustNewTinygoCompiler Uses

func MustNewTinygoCompiler() *TinygoCompiler

MustNewTinygoCompiler is like NewTinygoCompiler but panics upon error.

func NewTinygoCompiler Uses

func NewTinygoCompiler() (*TinygoCompiler, error)

NewTinygoCompiler returns a new TinygoCompiler instance.

func (*TinygoCompiler) AddGoGet Uses

func (c *TinygoCompiler) AddGoGet(goGetCmdLine string) *TinygoCompiler

AddGoGet adds a go get command to the list of dependencies. Arguments are separated by whitespace.

func (*TinygoCompiler) AddGoGetArgs Uses

func (c *TinygoCompiler) AddGoGetArgs(goGetCmdParts []string) *TinygoCompiler

AddGoGetArgs is like AddGoGet but the args are explicitly separated in a string slice.

func (*TinygoCompiler) AddPkgReplace Uses

func (c *TinygoCompiler) AddPkgReplace(pkgName, dir string) *TinygoCompiler

AddPkgReplace adds a directory mapping for a package. It provides similar functionality to go.mod's replace statement, but is implemented with a docker volume mapping. The dir will be run through filepath.Abs before adding it and will panic if that fails.

func (*TinygoCompiler) Close Uses

func (c *TinygoCompiler) Close() error

Close performs any cleanup. For now it removes the temporary directory created by NewTinygoCompiler.

func (*TinygoCompiler) Execute Uses

func (c *TinygoCompiler) Execute() (outpath string, err error)

Execute runs the generate command (if any) and then invokes the Tinygo compiler and produces a wasm executable (or an error). The value of outpath is the absolute path to the output file on disk. It will be created with a temporary name and if no error is returned it is the caller's responsibility to delete the file when it is no longer needed. If an error occurs during any of the steps it will be returned with (possibly multi-line) descriptive output in it's error message, as produced by the underlying tool.

func (*TinygoCompiler) NoDocker Uses

func (c *TinygoCompiler) NoDocker() *TinygoCompiler

NoDocker is an alias for SetTinygoDockerImage("") and will result in the tinygo executable being run on the local system instead of via docker image.

func (*TinygoCompiler) SetAfterFunc Uses

func (c *TinygoCompiler) SetAfterFunc(f func(outpath string, err error) error) *TinygoCompiler

SetAfterFunc specifies a function to be executed after everthing else during Execute().

func (*TinygoCompiler) SetBeforeFunc Uses

func (c *TinygoCompiler) SetBeforeFunc(f func() error) *TinygoCompiler

SetBeforeFunc specifies a function to be executed before anything else during Execute().

func (*TinygoCompiler) SetBuildDir Uses

func (c *TinygoCompiler) SetBuildDir(dir string) *TinygoCompiler

SetBuildDir sets the directory of the main package, where `go build` will be run. Relative paths are okay and will be resolved with filepath.Abs.

func (*TinygoCompiler) SetDir Uses

func (c *TinygoCompiler) SetDir(dir string) *TinygoCompiler

SetDir sets both the build and generate directories.

func (*TinygoCompiler) SetGenerateCmdFunc Uses

func (c *TinygoCompiler) SetGenerateCmdFunc(cmdf func() *exec.Cmd) *TinygoCompiler

SetGenerateCmdFunc provides a function to create the exec.Cmd used when running `go generate`. It overrides any other generate-related setting.

func (*TinygoCompiler) SetGenerateDir Uses

func (c *TinygoCompiler) SetGenerateDir(dir string) *TinygoCompiler

SetGenerateDir sets the directory of where `go generate` will be run. Relative paths are okay and will be resolved with filepath.Abs.

func (*TinygoCompiler) SetLogWriter Uses

func (c *TinygoCompiler) SetLogWriter(w io.Writer) *TinygoCompiler

SetLogWriter sets the writer to use for logging output. Setting it to nil disables logging. The default from NewCompiler is os.Stderr

func (*TinygoCompiler) SetTinygoArgs Uses

func (c *TinygoCompiler) SetTinygoArgs(tinygoArgs ...string) *TinygoCompiler

SetTinygoArgs sets arguments to be passed to tinygo, e.g. -no-debug

func (*TinygoCompiler) SetTinygoDockerImage Uses

func (c *TinygoCompiler) SetTinygoDockerImage(img string) *TinygoCompiler

SetTinygoDockerImage will specify the docker image to use when invoking Tinygo. The default value is the value of when NewTinygoCompiler was called. If you specify an empty string then the "tinygo" command will be run directly on the local system.

func (*TinygoCompiler) WasmExecJS Uses

func (c *TinygoCompiler) WasmExecJS() (r io.Reader, err error)

WasmExecJS returns the contents of the wasm_exec.js file bundled with Tinygo.

type WasmCompiler Uses

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

WasmCompiler provides a convenient way to call `go generate` and `go build` and produce Wasm executables for your system.

func NewWasmCompiler Uses

func NewWasmCompiler() *WasmCompiler

NewWasmCompiler returns a WasmCompiler instance.

func (*WasmCompiler) Execute Uses

func (c *WasmCompiler) Execute() (outpath string, err error)

Execute runs the generate command (if any) and then invokes the Go compiler and produces a wasm executable (or an error). The value of outpath is the absolute path to the output file on disk. It will be created with a temporary name and if no error is returned it is the caller's responsibility to delete the file when it is no longer needed. If an error occurs during any of the steps it will be returned with (possibly multi-line) descriptive output in it's error message, as produced by the underlying tool.

func (*WasmCompiler) SetAfterFunc Uses

func (c *WasmCompiler) SetAfterFunc(f func(outpath string, err error) error) *WasmCompiler

SetAfterFunc specifies a function to be executed after everthing else during Execute().

func (*WasmCompiler) SetBeforeFunc Uses

func (c *WasmCompiler) SetBeforeFunc(f func() error) *WasmCompiler

SetBeforeFunc specifies a function to be executed before anything else during Execute().

func (*WasmCompiler) SetBuildCmdFunc Uses

func (c *WasmCompiler) SetBuildCmdFunc(cmdf func(outpath string) *exec.Cmd) *WasmCompiler

SetBuildCmdFunc provides a function to create the exec.Cmd used when running `go build`. It overrides any other build-related setting.

func (*WasmCompiler) SetBuildDir Uses

func (c *WasmCompiler) SetBuildDir(dir string) *WasmCompiler

SetBuildDir sets the directory of the main package, where `go build` will be run. Relative paths are okay and will be resolved with filepath.Abs.

func (*WasmCompiler) SetDir Uses

func (c *WasmCompiler) SetDir(dir string) *WasmCompiler

SetDir sets both the build and generate directories.

func (*WasmCompiler) SetGenerateCmdFunc Uses

func (c *WasmCompiler) SetGenerateCmdFunc(cmdf func() *exec.Cmd) *WasmCompiler

SetGenerateCmdFunc provides a function to create the exec.Cmd used when running `go generate`. It overrides any other generate-related setting.

func (*WasmCompiler) SetGenerateDir Uses

func (c *WasmCompiler) SetGenerateDir(dir string) *WasmCompiler

SetGenerateDir sets the directory of where `go generate` will be run. Relative paths are okay and will be resolved with filepath.Abs.

func (*WasmCompiler) SetLogWriter Uses

func (c *WasmCompiler) SetLogWriter(w io.Writer) *WasmCompiler

SetLogWriter sets the writer to use for logging output. Setting it to nil disables logging. The default from NewWasmCompiler is os.Stderr

func (*WasmCompiler) WasmExecJS Uses

func (c *WasmCompiler) WasmExecJS() (r io.Reader, err error)

WasmExecJS returns the contents of the wasm_exec.js file bundled with the Go compiler.

type WasmExecJSHandler Uses

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

WasmExecJSHandler calls WasmCompiler.WasmExecJS and responds with the resulting .js file. WasmCompiler.WasmExecJS will only be called the first time and subsequent times will return the same result from memory. (We're going to assume that you'll restart whatever process this is running in when upgrading your Go version.)

func NewWasmExecJSHandler Uses

func NewWasmExecJSHandler(wc WasmExecJSer) *WasmExecJSHandler

NewWasmExecJSHandler returns an initialized WasmExecJSHandler.

func (*WasmExecJSHandler) ServeHTTP Uses

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

ServeHTTP implements http.Handler.

type WasmExecJSer Uses

type WasmExecJSer interface {
    WasmExecJS() (contents io.Reader, err error)
}

WasmExecJSer is implemented by WasmCompiler and TinygoCompiler.

Package devutil imports 19 packages (graph). Updated 2020-06-23. Refresh now. Tools for package owners.