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.
- Use
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>
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.