Documentation
¶
Overview ¶
Package singleserve provides the local HTTP, browser authentication, browser-presence, and graceful-shutdown boundary for single-binary browser applications while leaving routing, assets, and business logic to callers.
A Server wraps an application-owned net/http.Handler, binds only to loopback, and produces a short-lived browser bootstrap URL on an isolated per-launch origin. Trusted owner code can rotate that URL with Launch.NewBootstrapURL. Browser authentication becomes an HttpOnly session before application content runs; programmatic callers use Launch.Client. Applications choose an explicit or browser-bound lifetime and may supply a shutdown guard.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"github.com/rztaylor/singleserve"
)
func main() {
server, err := singleserve.New(singleserve.Options{
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = fmt.Fprint(w, "hello") }),
Lifetime: singleserve.ExplicitLifetime(),
Opener: singleserve.BrowserOpenerFunc(func(context.Context, string) error { return nil }),
})
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = server.Start(ctx)
fmt.Println(err)
}
Output: context canceled
Index ¶
- Constants
- Variables
- func DenyShutdown(code, message string) error
- type BrowserOpener
- type BrowserOpenerFunc
- type Launch
- func (l *Launch) Address() string
- func (l *Launch) BaseURL() string
- func (l *Launch) Client() *http.Client
- func (l *Launch) NewBootstrapURL() (string, error)
- func (l *Launch) OpenBrowser(ctx context.Context) error
- func (l *Launch) Shutdown(ctx context.Context) error
- func (l *Launch) URL() string
- func (l *Launch) Wait() (Result, error)
- type LifetimeMode
- type LifetimePolicy
- type Options
- type Result
- type Server
- type ShutdownDeniedError
- type ShutdownGuard
- type ShutdownGuardFunc
- type ShutdownReason
- type ShutdownRequest
- type TabSnapshot
- type TabState
Examples ¶
Constants ¶
const ( // TokenHeader carries the private programmatic credential applied by // Launch.Client. Browser clients do not send it. TokenHeader = "X-Singleserve-Token" // TabHeader identifies one browser tab instance. TabHeader = "X-Singleserve-Tab" // ControlPath is reserved for Singleserve lifecycle endpoints. ControlPath = "/_singleserve/" )
Variables ¶
var ErrAlreadyStarted = errors.New("singleserve: server already started")
ErrAlreadyStarted reports an attempt to start a single-use Server twice.
var ErrBrowserAlreadyOpened = errors.New("singleserve: browser already opened")
ErrBrowserAlreadyOpened reports an attempt to open one Launch twice.
var ( // ErrTooManyTabs reports that the bounded tab registry has no reclaimable entries. ErrTooManyTabs = errors.New("singleserve: too many connected tabs") )
Functions ¶
func DenyShutdown ¶
DenyShutdown creates a typed shutdown veto. Invalid codes or empty messages are treated as internal guard failures when evaluated.
Types ¶
type BrowserOpener ¶
BrowserOpener opens a URL without waiting for the browser to exit.
type BrowserOpenerFunc ¶
BrowserOpenerFunc adapts a function to BrowserOpener.
type Launch ¶
type Launch struct {
// contains filtered or unexported fields
}
Launch is the active view of a started Server.
func (*Launch) Client ¶ added in v0.2.0
Client returns an HTTP client authenticated only for this launch origin. The client bypasses environment proxies, dials the bound listener directly, and never exposes its credential.
func (*Launch) NewBootstrapURL ¶ added in v0.2.0
NewBootstrapURL invalidates any earlier unconsumed bootstrap capability and returns a fresh one-time browser URL that expires after two minutes. It does not change the launch lifetime or invalidate an established browser session. Treat the complete URL as a secret.
func (*Launch) OpenBrowser ¶
OpenBrowser opens URL with the configured platform opener.
func (*Launch) Shutdown ¶
Shutdown forces owner-controlled graceful shutdown and waits for completion or for ctx to be canceled. It bypasses the application guard.
type LifetimeMode ¶
type LifetimeMode uint8
LifetimeMode controls whether browser absence can stop the server.
const ( // LifetimeExplicit stops only on parent cancellation, owner shutdown, or an // authenticated browser shutdown request. LifetimeExplicit LifetimeMode = iota // LifetimeBrowserBound also stops when first contact never arrives or every // contacted browser tab disconnects. LifetimeBrowserBound )
type LifetimePolicy ¶
type LifetimePolicy struct {
Mode LifetimeMode
FirstContactTimeout time.Duration
HeartbeatTimeout time.Duration
DisconnectGrace time.Duration
CheckInterval time.Duration
}
LifetimePolicy configures browser-presence shutdown behavior.
func BrowserBoundLifetime ¶
func BrowserBoundLifetime() LifetimePolicy
BrowserBoundLifetime returns the recommended browser-owned policy.
func ExplicitLifetime ¶
func ExplicitLifetime() LifetimePolicy
ExplicitLifetime returns the safe zero-value lifetime policy.
type Options ¶
type Options struct {
Handler http.Handler
Address string
Lifetime LifetimePolicy
Guard ShutdownGuard
Opener BrowserOpener
}
Options configures one single-use local server.
type Result ¶
type Result struct {
Reason ShutdownReason
StartedAt time.Time
StoppedAt time.Time
}
Result describes the completed server lifetime.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server owns one authenticated loopback listener and its lifecycle state. A Server is single-use.
func (*Server) Tabs ¶
func (s *Server) Tabs() []TabSnapshot
Tabs returns a time-ordered copy of the current tab registry.
type ShutdownDeniedError ¶
ShutdownDeniedError is a safe, expected application veto.
func (*ShutdownDeniedError) Error ¶
func (e *ShutdownDeniedError) Error() string
type ShutdownGuard ¶
type ShutdownGuard interface {
CheckShutdown(context.Context, ShutdownRequest) error
}
ShutdownGuard can veto browser-initiated or browser-bound shutdown.
type ShutdownGuardFunc ¶
type ShutdownGuardFunc func(context.Context, ShutdownRequest) error
ShutdownGuardFunc adapts a function to ShutdownGuard.
func (ShutdownGuardFunc) CheckShutdown ¶
func (f ShutdownGuardFunc) CheckShutdown(ctx context.Context, request ShutdownRequest) error
CheckShutdown calls f.
type ShutdownReason ¶
type ShutdownReason string
ShutdownReason records why graceful shutdown began.
const ( ShutdownBrowserRequest ShutdownReason = "browser_request" ShutdownBrowserDisconnected ShutdownReason = "browser_disconnected" ShutdownFirstContactTimeout ShutdownReason = "first_contact_timeout" ShutdownProgrammatic ShutdownReason = "programmatic" ShutdownContextCanceled ShutdownReason = "context_canceled" ShutdownServerError ShutdownReason = "server_error" )
type ShutdownRequest ¶
type ShutdownRequest struct {
Reason ShutdownReason
Tabs []TabSnapshot
}
ShutdownRequest is the server-built input to an application shutdown guard.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
minimal
command
Command minimal is an executable specification of Singleserve's public API.
|
Command minimal is an executable specification of Singleserve's public API. |
|
internal
|
|
|
testbrowserjar
Package testbrowserjar provides browser-like host-only cookie behavior for repository HTTP tests.
|
Package testbrowserjar provides browser-like host-only cookie behavior for repository HTTP tests. |
|
testtransport
Package testtransport provides browser-like loopback name resolution for repository HTTP tests.
|
Package testtransport provides browser-like loopback name resolution for repository HTTP tests. |