devserver

package
v0.0.0-...-66df17a Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package devserver implements `wpix serve`: a static file server for a browser game's development build that pushes asset changes to the open page.

Usage:

wpix serve [--addr :8080] [--assets <dir>] <dir>

It serves <dir> (the page and game.wasm) at /, and the --assets directory under /assets/, both with "Cache-Control: no-cache" so the browser revalidates instead of reusing a stale body after a save. GET /_weakpixel/changes is an event stream (server-sent events): the server polls the modtime and size of the asset tree every 250 ms, where a stat is cheap, and sends one `data: {"paths": [...]}` event per batch of changed files. Paths are asset keys: slash-separated, relative to the asset directory. A browser cannot list directories, so the server also answers /assets/assets.catalog.json from the asset directory (scanned on every request, so new files show after a page reload) unless the directory has its own. A game turns the stream into hot reload with httpfs.WatchChanges and game.Config.AssetChanges.

Exit codes: 0 ok, 1 the server failed, 2 usage error.

Index

Constants

View Source
const ChangesPath = "/_weakpixel/changes"

ChangesPath is the URL path of the event stream.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, opts Options, stdout io.Writer) error

Run serves opts.Dir until ctx is done and prints the address to stdout. A directory that does not exist is a usage error; a listener or server that fails is a plain error.

Types

type Options

type Options struct {
	Dir    string // the build directory, served at /
	Addr   string // address to listen on
	Assets string // asset directory, served under /assets/ and watched; may be empty
}

Options are the arguments of `wpix serve`.

type Server

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

Server serves a build directory and an asset directory and pushes the asset directory's changes to event-stream clients.

func New

func New(ctx context.Context, dir, assets string, interval time.Duration) *Server

New returns a Server and, when assets is not empty, starts polling it every interval until ctx ends. The tree is scanned once before New returns, so only changes after that are reported.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the HTTP handler: the build at /, the assets under /assets/ and the event stream at ChangesPath.

Jump to

Keyboard shortcuts

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