Settings: kscript.json
The http section of kscript.json holds the settings for routes and their middleware. Every one is optional:
{
"http": {
"routes": ["routes.kscript", { "file": "api/v1.kscript", "prefix": "/api/v1", "api": true }], // default: routes/web.*, routes/api.*, routes.* and api.*
"middleware": ["log"], // for every route
"groups": { "api": ["cors", "throttle"] }, // names for lists of middleware
"views": "views", // where @view() looks first; never served
"api_prefix": "/api", // the prefix of api.* files
"write_files": true, // false: controllers and their live actions can only read files
"cors": { "origins": ["*"], "methods": ["GET", "POST"], "headers": ["Content-Type", "Authorization"], "credentials": false },
"auth": { "token_env": "API_TOKEN", "login": "/login", "lifetime": "2h" }, // and the KScript.Auth settings in auth.md
"throttle": { "limit": 60, "seconds": 60, "store": "memory" }, // store: memory, file or database (shared by every server)
"upload_limit": "8MB", // the most a request's uploaded files may be, in all
"headers": { "X-Frame-Options": "DENY" }, // sent with every response that doesn't set its own
"proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" }, // believe a reverse proxy's X-Forwarded-For
"openapi": { "routes": "auto", "title": "People API", "version": "1.2.0", "file": "openapi.json" } // or false
}
}Every setting is also in the kscript.json reference.
Headers on every response
headers adds response headers to everything kscript serve answers: pages, files, routes, passkeys and the sign-in provider. A response that sets a header itself keeps its own. It is the place for a site's security headers:
{
"http": {
"headers": {
"Content-Security-Policy": "default-src 'self'; script-src 'self'; frame-ancestors 'none'; form-action 'self' {client_origins}",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "same-origin"
}
}
}{client_origins} is the origins of the apps in the sign-in provider's clients.json, separated by spaces, so a form that sends someone back to an app is allowed without listing each one twice.
Behind a reverse proxy
Behind a proxy such as Caddy or nginx, every request comes from the proxy, so rate limits and sign-in attempt counts would lump every visitor together. Have the proxy send a secret in a header, and name the environment variable that holds it:
{ "http": { "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" } } }A request carrying that secret is counted under the last address in its X-Forwarded-For, the one the proxy added; any other request under the address it came from, so nothing that reaches the server another way can pretend to be someone else. throttle counts an IPv6 visitor by their /64, since one machine can use any address in it.