Serve Static Contents

This page is based on the following example:

https://github.com/aileron-gateway/aileron-gateway/tree/main/examples/static/

Static Handler

Overview

This example runs a template server that returns response generated from templates.

block-beta
  columns 4
  Downstream:1
  space:1
  block:aileron:2
    HTTPServer["πŸŸͺ</br>HTTP</br>Server"]
    StaticHandler["πŸŸ₯</br>Static</br>Handler"]
  end

Downstream --> HTTPServer

style StaticHandler stroke:#ff6961,stroke-width:2px

Legend:

  • πŸŸ₯ #ff6961 Handler resources.
  • 🟩 #77dd77 Middleware resources (Server-side middleware).
  • 🟦 #89CFF0 Tripperware resources (Client-side middleware).
  • πŸŸͺ #9370DB Other resources.

In this example, following directory structure and files are supposed. If you need a pre-built binary, download from GitHub Releases.

template/           ----- Working directory.
β”œβ”€β”€ aileron         ----- AILERON Gateway binary (aileron.exe on windows).
β”œβ”€β”€ config.yaml     ----- AILERON Gateway config file.
└── root/           ----- Root directory that contains served contents.
    β”œβ”€β”€ hello.html  ----- Example content.
    β”œβ”€β”€ hello.json  ----- Example content.
    └── hello.xml   ----- Example content.

Config

Configuration yaml to run a server with template handler would becomes as follows.

# config.yaml

apiVersion: core/v1
kind: Entrypoint
spec:
  runners:
    - apiVersion: core/v1
      kind: HTTPServer

---
apiVersion: core/v1
kind: HTTPServer
spec:
  addr: ":8080"
  virtualHosts:
    - handlers:
        - handler:
            apiVersion: core/v1
            kind: StaticFileHandler

---
apiVersion: core/v1
kind: StaticFileHandler
spec:
  rootDir: "./root/"
  enableListing: true
  header: # Additional response headers.
    Cache-Control: no-cache

The config tells:

  • Start a HTTPServer with port 8080.
  • Static handler is registered to the server (all paths match).
    • Use ./root/ directory as root path.

This graph shows the resource dependencies of the configuration.

graph TD
  Entrypoint["πŸŸͺ **Entrypoint**</br>default/default"]
  HTTPServer["πŸŸͺ **HTTPServer**</br>default/default"]
  StaticHandler["πŸŸ₯</br>**StaticHandler**</br>default/default"]

Entrypoint --"Runner"--> HTTPServer
HTTPServer --"HTTP Handler"--> StaticHandler

style StaticHandler stroke:#ff6961,stroke-width:2px

Run

Run the AILERON Gateway with command:

./aileron -f ./config.yaml

Check

After running a server, send HTTP requests to it. Content type of response bodies are sniffed by Go’s DetectContentType.

$ curl http://localhost:8080/hello.json

{ "hello": "AILERON Gateway" }
$ curl http://localhost:8080/hello.xml

<hello>AILERON Gateway</hello>

Last modified June 2, 2025: update docs (df954a4)