KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

ルートと API

設定:kscript.json

kscript.jsonのhttpセクションには、ルートとそのミドルウェアの設定が含まれます。いずれも省略可能です:

JSON
{
  "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:

JSON
{
  "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などのプロキシーの背後では、すべてのリクエストがプロキシーから送られるため、レート制限やサインイン試行回数の集計がすべての訪問者を一括りにしてしまいます。プロキシーにヘッダーで秘密の値を送らせ、その値を保持する環境変数の名前を指定してください:

JSON
{ "http": { "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" } } }

その秘密の値を含むリクエストは、プロキシーが追加したX-Forwarded-For内の最後のアドレスで集計されます。それ以外のリクエストは、接続元のアドレスで集計されます。これにより、別の経路でサーバーに届いたものが他人になりすますことはできません。throttleは、1台のマシンがその中の任意のアドレスを使用できるため、IPv6の訪問者を/64単位で集計します。