server

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultFirstGETTimeout bounds the wait for the browser's initial GET /.
	DefaultFirstGETTimeout = 60 * time.Second
	// DefaultBeaconTimeout bounds the wait for the load beacon after the page
	// has been delivered; on expiry the content is considered fully delivered
	// and the server shuts down normally.
	DefaultBeaconTimeout = 15 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type PageFunc added in v0.5.0

type PageFunc func() ([]byte, error)

PageFunc produces the bytes of the page for one request.

func Static added in v0.8.0

func Static(body []byte) PageFunc

Static turns already-built page bytes into a PageFunc, for a caller with nothing to rebuild.

type Server

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

Server hosts the standalone page and coordinates its own shutdown.

func New

func New(page []byte, firstGETTimeout, beaconTimeout time.Duration) *Server

New creates a Server serving page. firstGETTimeout bounds the wait for the browser's initial GET /; beaconTimeout bounds the wait for the load beacon after the page has been delivered. Non-positive values fall back to the

func NewEdit added in v0.5.0

func NewEdit(page PageFunc, token string, api http.Handler) *Server

NewEdit creates a keep-alive Server for --edit: the page and the api handler are both mounted under /e/<token>/, and nothing at all is served outside that prefix.

func NewKeepAlive added in v0.5.0

func NewKeepAlive(page PageFunc, events http.Handler) *Server

NewKeepAlive creates a Server that stays up until its Wait context is cancelled: neither timeout applies and the load beacon no longer ends the session. Used by --tunnel, where the page must remain reachable for as long as the session lives. The page is built per request, so a live session serves its current state; events, when not nil, is mounted at /events.

func (*Server) Addr added in v0.5.0

func (s *Server) Addr() string

Addr returns the listening address as host:port (e.g. 127.0.0.1:53187), or an empty string before Start. A tunnel needs the explicit IPv4 address, not a bare port: forwarding to "localhost" may resolve to ::1, where nothing

func (*Server) Close added in v0.5.0

func (s *Server) Close() error

Close drains the server outside its normal lifecycle, for a caller that gives up before Wait was ever reached — --tunnel does when ngrok fails to come up.

func (*Server) Path added in v0.5.0

func (s *Server) Path() string

Path returns the request path the page is served at: "/" normally, and "/e/<token>/" in edit mode. A tunnel forwards to Addr and knows nothing of the prefix, so the public URL is built by appending this.

func (*Server) Start

func (s *Server) Start() (string, error)

Start listens on 127.0.0.1 with an OS-assigned port and begins serving in the background. It returns the page URL (e.g. http://127.0.0.1:53187/).

func (*Server) URL added in v0.5.0

func (s *Server) URL() string

URL returns the page URL, or an empty string before Start. In edit mode it carries the token (http://127.0.0.1:53187/e/<token>/) — the bare root serves nothing there, so this is the only address worth printing or opening.

func (*Server) Wait

func (s *Server) Wait(ctx context.Context) error

Wait blocks until a shutdown condition is met and then drains the server:

  • no GET within firstGETTimeout: returns an error (the URL was already printed, so the user can open it manually);

Jump to

Keyboard shortcuts

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