Skip to content

Handler 后端示例

覆盖全部 backend.handler 类型的可运行示例。源码:examples/handler/

字段说明与脚本 API 见 路由指南 — Handler 后端

全部 handler 类型

yaml
# Runnable samples for all backend.handler types.
#
# Validate (from repo root):
#   ingress validate -c examples/handler/ingress.yaml
#
# Run (cwd must be examples/handler so root_dir resolves):
#   cd examples/handler && ingress run -c ingress.yaml
#
# Try:
#   curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/text
#   curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/files/
#   curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/templates/
#   curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/script/js
#   curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/script/go

version: v1
port: 8080

rules:
  # Host-level service; path backends override with handler.
  - host: handler.example.work
    backend:
      service:
        name: fallback-upstream.internal
        port: 8080
    paths:
      - path: /static/text
        backend:
          type: handler
          handler:
            type: static_response
            status_code: 200
            headers:
              Content-Type: text/plain; charset=utf-8
            body: |
              Hello from static_response handler.

      - path: /static/json
        backend:
          handler:
            type: static_response
            headers:
              Content-Type: application/json
            body: |
              {"handler":"static_response","ok":true}

      - path: /static/files
        backend:
          handler:
            type: file_server
            root_dir: ./static
            index_file: index.html

      - path: /static/templates
        backend:
          handler:
            type: templates
            root_dir: ./templates

      - path: /static/script/js
        backend:
          handler:
            type: script
            engine: javascript
            status_code: 200
            headers:
              Content-Type: application/json
            script: |
              ctx.status = 200
              ctx.type = "application/json"
              ctx.body = JSON.stringify({
                engine: "javascript",
                method: ctx.method,
                path: ctx.path,
              })
              ctx.setHeader("X-Handler-Engine", "javascript")

      - path: /static/script/go
        backend:
          handler:
            type: script
            engine: go
            script: |
              ctx.SetHeader("Content-Type", "application/json")
              ctx.SetHeader("X-Handler-Engine", "go")
              ctx.String(200, `{"engine":"go","method":"`+ctx.Method+`","path":"`+ctx.Path+`"}`)

  # Rule-level handler (no paths): health-style plain text.
  - host: status.example.work
    backend:
      type: handler
      handler:
        type: static_response
        headers:
          Content-Type: text/plain; charset=utf-8
        body: "ok\n"

file_server / templates 使用的静态文件与模板目录:

  • examples/handler/static/ — 普通文件(index.htmlhello.txt
  • examples/handler/templates/ — Go 模板,可用 {{.Path}}{{.Method}}

校验与运行

bash
ingress validate -c examples/handler/ingress.yaml
cd examples/handler && ingress run -c ingress.yaml

handler.root_dir 相对 进程工作目录 解析,因此建议在 examples/handler/ 下启动(或自行调整路径)。

快速验证

bash
curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/text
curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/files/hello.txt
curl -H "Host: handler.example.work" http://127.0.0.1:8080/static/script/js
curl -H "Host: status.example.work" http://127.0.0.1:8080/

相关示例

Released under the MIT License.