설정: 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에 있는 앱들의 출처를 공백으로 구분해 적은 것이에요. 그래서 사용자를 앱으로 돌려보내는 양식을 일일이 두 번씩 적지 않아도 허용돼요.
역방향 프록시 뒤에서
Caddy나 nginx 같은 프록시 뒤에서는 모든 요청이 프록시에서 오기 때문에, 요청 한도와 로그인 시도 횟수가 모든 방문자를 하나로 묶어 버려요. 프록시가 헤더에 비밀 값을 담아 보내도록 하고, 그 값을 담은 환경 변수의 이름을 지정하세요:
{ "http": { "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" } } }그 비밀 값을 담은 요청은 X-Forwarded-For의 마지막 주소, 즉 프록시가 추가한 주소로 집계되고, 그 밖의 요청은 실제 출발지 주소로 집계돼요. 그래서 다른 경로로 서버에 도달한 요청이 다른 사람인 척할 수 없어요. throttle는 IPv6 방문자를 /64 단위로 집계해요. 한 기기가 그 안의 어떤 주소든 쓸 수 있기 때문이에요.