server

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 26 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 server builds the single HTTP handler that both the native window and the browser mode serve: the Connect services, and the interface for every other path.

Index

Constants

View Source
const (
	// SocketFile is the Unix socket the command line joins, in the data directory.
	SocketFile = "djinn.sock"
	// AddrFile holds the address of the running server, in the data directory: unix:///…/djinn.sock, or
	// http://127.0.0.1:PORT/?token=… whose token the command line sends as a bearer token.
	AddrFile = "server.addr"
)
View Source
const DocsPath = "/docs/"

DocsPath is where Handler serves the documentation site.

View Source
const DumpPath = "/debug/dump"

DumpPath is where Dump answers.

View Source
const ProfilePath = "/debug/pprof/"

ProfilePath is where Profiles serves.

View Source
const TilasmBridge = TilasmPrefix + "bridge.js"

TilasmBridge is the script that ends each HTML page of a tilasm Djinn serves (plan.Tilasms.Files): the same for every tilasm, it holds no secret, and a sandboxed frame loads it with neither the cookie nor a tilasm's key.

View Source
const TilasmPrefix = "/tilasm/"

TilasmPrefix is where the tilasms' files are served, each tilasm's under /tilasm/<id>/ (plan.Tilasms.Files).

Variables

This section is empty.

Functions

func Dump added in v0.1.1

func Dump(dir string) http.Handler

Dump answers a POST by writing the stacks of every goroutine to a new file of dir (logx.Dump) and its path as the body, for djinn debug dump: always mounted, it is reached only as the services are. A djinn whose calls hang still answers it, on a goroutine of its own.

func FrameKey

func FrameKey(token, id string) string

FrameKey is the key of a tilasm's files for the server of token: it opens that tilasm only, while that server runs.

func Guard

func Guard(next http.Handler, token, origin string) http.Handler

Guard lets through only the requests that carry token and, when the browser names one, come from origin (scheme://host:port, the address the server listens on).

The browser opens origin/?token=…: that first request sets a cookie and redirects to the same URL without the token. Later requests carry the cookie; a program sends "Authorization: Bearer <token>" instead.

func Handler

func Handler(ui, docs fs.FS, services map[string]http.Handler) http.Handler

Handler serves each Connect service at its path prefix, as returned by a generated New…Handler, the documentation site from docs at /docs/ when docs is not nil, and the interface from ui for every other path.

The services never compress a response: every client of this server is on the same machine, where gzip only costs time on both ends (see docs/transport.md). A service that writes nothing still answers 200, as on net/http. Each call to a service is logged (logx); a panic of a handler is logged with its stack and answered 500.

func ListenUnix

func ListenUnix(path string) (net.Listener, error)

ListenUnix listens on the Unix socket at path, readable and writable by the owner only. A socket left by a crashed server is replaced; a socket that answers means djinn is already running.

func NewToken

func NewToken() string

NewToken returns a random secret to put in the URL the browser opens.

func PeerPIDFromContext added in v0.1.1

func PeerPIDFromContext(ctx context.Context) int

PeerPIDFromContext returns the peer PID from ctx, or 0 if none.

func Profiles

func Profiles() http.Handler

Profiles serves Go's profiles of this process (net/http/pprof) under ProfilePath, for djinn up --pprof: mounted among the services, it is reached only as they are, through the Unix socket or the loopback server and its token. The CPU profile of 30 s: curl --unix-socket <data folder>/djinn.sock -o cpu.prof http://djinn/debug/pprof/profile?seconds=30, then go tool pprof cpu.prof.

func ReadAddr

func ReadAddr(home string) (string, error)

ReadAddr returns the address the running server wrote in home.

func Serve

func Serve(ctx context.Context, ln net.Listener, h http.Handler) error

Serve serves h on ln until ctx is done, then shuts down. Requests share ctx, so open streams end with it instead of holding the shutdown.

A Unix socket speaks HTTP/1.1, which the command line uses, and HTTP/2 without TLS (h2c, prior knowledge), which a bidirectional Connect stream needs. A TCP listener, which a browser reaches, speaks HTTP/1.1 only: no browser speaks h2c. See docs/transport.md.

func TilasmAddress

func TilasmAddress(origin, token, id string) string

TilasmAddress is the address of a tilasm's latest version on the http server at origin guarded by token, with its key: a browser or a program opens it as it is, while that server runs, and it opens nothing else.

func WholeWrites added in v0.1.1

func WholeWrites(h http.Handler) http.Handler

WholeWrites gathers what h writes until it flushes or returns, and hands it on in one Write: on wails://, each Write is a chunk WebKit passes to the page. connect-go writes a message of a stream in two writes (its 5-byte prefix, then its payload) and flushes after it; on macOS, WebKit sometimes held the payload back after handing over the prefix, and the page waited for it until a reload: a change of WishService.Watch never showed. One chunk per message leaves nothing half sent.

func WithPeerPID added in v0.1.1

func WithPeerPID(ctx context.Context, pid int) context.Context

WithPeerPID returns a context carrying the peer PID.

func WorkerContext added in v0.1.1

func WorkerContext(next http.Handler, attach func(ctx context.Context, peerPID int, token string) context.Context) http.Handler

WorkerContext wraps next so that any worker making a request is identified and its identity is placed in the request context.

func WriteAddr

func WriteAddr(home, addr string) (remove func(), err error)

WriteAddr writes addr to the address file of home, readable by the owner only. The returned function removes it, unless another server has written its own address since.

Types

type Transport

type Transport string

Transport is how the native window reaches the server.

const (
	// Wails serves the window through the Wails internal asset server (wails://): no port, no token, since only
	// the webview reaches it. The command line joins the server over a Unix socket.
	Wails Transport = "wails"
	// HTTP serves the window, a browser and the command line on the loopback, guarded by a token.
	HTTP Transport = "http"
)

func TransportFor

func TransportFor(goos string) Transport

TransportFor returns the transport of the native window on goos (a runtime.GOOS value). WebView2, on Windows, only hands a custom scheme response to the page once it is complete, so a stream would arrive in one burst: Windows goes through loopback HTTP. WebKit, on macOS and Linux, streams it.

Jump to

Keyboard shortcuts

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