设置:kscript.json
kscript.json 的 http 部分包含路由及其中间件的设置。以下每项均为可选:
{
"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
}
}每个设置也都列在kscript.json 参考中。
每个响应的标头
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} 是登录提供方的 clients.json 中应用来源(origin)的列表,以空格分隔,这样无需重复列出每个应用,就能允许将用户送回应用的表单提交。
位于反向代理之后
在 Caddy 或 nginx 等代理后面时,每个请求都来自代理本身,因此速率限制和登录尝试次数会把所有访问者混为一谈。让代理在请求头中发送一个密钥,并指定保存该密钥的环境变量名称:
{ "http": { "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" } } }携带该密钥的请求会按其 X-Forwarded-For 中的最后一个地址计数,即代理添加的那个地址;其他请求则按其来源地址计数,这样通过其他途径到达服务器的请求就无法冒充他人。throttle 会按 /64 段统计 IPv6 访问者,因为同一台机器可以使用该段中的任意地址。