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
- func Dump(dir string) http.Handler
- func FrameKey(token, id string) string
- func Guard(next http.Handler, token, origin string) http.Handler
- func Handler(ui, docs fs.FS, services map[string]http.Handler) http.Handler
- func ListenUnix(path string) (net.Listener, error)
- func NewToken() string
- func PeerPIDFromContext(ctx context.Context) int
- func Profiles() http.Handler
- func ReadAddr(home string) (string, error)
- func Serve(ctx context.Context, ln net.Listener, h http.Handler) error
- func TilasmAddress(origin, token, id string) string
- func WholeWrites(h http.Handler) http.Handler
- func WithPeerPID(ctx context.Context, pid int) context.Context
- func WorkerContext(next http.Handler, ...) http.Handler
- func WriteAddr(home, addr string) (remove func(), err error)
- type Transport
Constants ¶
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" )
const DocsPath = "/docs/"
DocsPath is where Handler serves the documentation site.
const DumpPath = "/debug/dump"
DumpPath is where Dump answers.
const ProfilePath = "/debug/pprof/"
ProfilePath is where Profiles serves.
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
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 ¶
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 ¶
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 ¶
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).
func ListenUnix ¶
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
PeerPIDFromContext returns the peer PID from ctx, or 0 if none.
func Profiles ¶
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 Serve ¶
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 ¶
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
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
WithPeerPID returns a context carrying the peer PID.
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 ¶
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.