Server functions, session tokens and page rendering for a front project.
import net.ssr
This package is the server side of a front project: it answers the calls a page makes to its server func functions, hands out the session tokens those calls need, and sends finished HTML for a route. It sits on net.http and net.router; Mount adds its endpoints to a router and you run the router with http.Use as usual.
A call is POST /_salam/fn/Name with Content-Type: application/json and a body like {"args":{"id":3}}. The answer is {"ok":true,"value":...} or {"ok":false,"error":"..."} with status 200 for both. The other statuses are 400 for a body that is not a JSON object, has the wrong content type or is over the size cap; 403 for a failed origin or token check; 404 for a name nobody registered; and 500 when the function answered with neither Ok nor Fail.
Calls are protected twice. The request must be same-origin: when Sec-Fetch-Site is present it must be same-origin, and when Origin is present its host must equal the Host header. There is no CORS, so a page of another site cannot read an answer. And every call needs an X-Salam-Token header, a token signed with a secret, which the page embeds or a native app fetches from GET /_salam/session. Native apps send neither Origin nor Sec-Fetch-Site, so for them the token is the whole check.
Calling a server function
The program registers one function, mounts the endpoints on a router and calls them like a browser would: it fetches a token from /_salam/session, then posts {"args":...} with the token in X-Salam-Token. The last three calls show an unknown name (404), a missing token (403) and a foreign Origin (403).
Every GET or HEAD request that is not an endpoint goes through the same fallback. A file in the folder given to SetStaticDir is sent with an ETag, and a request with a matching If-None-Match gets 304. Otherwise the function given to SetRender is called: it reads the path with PagePath and answers with Respond, for example Respond(c, 200, html, "") for a page, Respond(c, 404, html, "") for an unknown route or Respond(c, 302, "", "/login") for a redirect. Without a renderer the answer is 404.
Things that catch people out:
•
A function must answer. Call Ok or Fail before it returns, on the same thread; otherwise the caller gets a generic 500.
•
Request is per thread. It is valid in the function that was called and in the functions it calls, not in a thread you start yourself.
•
The secret. Without SetSecret or SALAM_SERVER_SECRET a new secret is made at start, so tokens from before a restart or on another instance are refused. Set it when you run more than one server.
•
Behind a proxy keep the Host header. The origin check compares it with Origin.
•
Register everything before the server starts. And mount before http.Use, because the server keeps its own copy of the router.
The request being handled on this thread: inside a server function and while a page is rendered. Outside both, active is false and every accessor answers an empty string. The strings the accessors return belong to the request and are freed when it ends; copy one to keep it.
Registers a server function under a name. The handler is a function that takes the call handle; it reads the arguments with Args and answers with Ok or Fail. Registering a name again replaces the handler. Do this before the server starts.
Sets the secret that signs the session tokens. Without it the secret comes from the SALAM_SERVER_SECRET environment variable (16 bytes or more), or is generated when the server starts, which makes tokens stop working after a restart.
Sets the folder of built files, for example dist/public. A GET or HEAD request for a file in it is answered with an ETag, and If-None-Match gets 304. A directory answers with its index.html. Hidden files and paths that leave the folder are never served.
Sets the page renderer. It is a function that takes the call handle, reads the path with PagePath and answers with Respond. Generated server code registers it; the catch-all route calls it for every GET or HEAD request that is not a file.
Answers a call with an error text the function chose to return: {"ok":false,"error":...} with status 200. A function that answers with neither Ok nor Fail gets a generic 500.
Answers a call with an error status and the message in the usual {"ok":false,"error":...} body. Use it for a bad request (400) or a refusal; Fail answers with status 200.
Adds the endpoints to a router: POST /_salam/fn/:name for function calls, GET /_salam/session for tokens, and the catch-all that serves the static folder, calls the page renderer and answers 404. Call it after the routes of your own and before http.Use.