Serve Template Contents

This page is based on the following example:

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

Template 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"]
    TemplateHandler["πŸŸ₯</br>Template</br>Handler"]
  end

Downstream --> HTTPServer

style TemplateHandler 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.
└── template.html  ----- A example template for the TemplateHandler.

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: TemplateHandler

---
apiVersion: core/v1
kind: TemplateHandler
spec:
  mimeContents:
    - mimeType: text/plain
      statusCode: 500
      templateType: Text
      template: |
        Hello! AILERON Gateway!
    - mimeType: application/json
      statusCode: 500
      templateType: GoText
      template: |
        {
          "hello": "AILERON Gateway!"
        }
    - mimeType: text/html
      statusCode: 500
      templateType: GoHTML
      templateFile: ./template.html

The config tells:

  • Start a HTTPServer with port 8080.
  • Template handler is registered to the server (all paths match).
  • 3 templates are registered to the template handler.
    • A text template for Accept: text/plain
    • A go’s text template for Accept: application/json
    • A go’s html template for Accept: text/html

This graph shows the resource dependencies of the configuration.

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

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

style TemplateHandler 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.

Request with Accept: text/plain header to obtain templated response for the mime type.

$ curl -H "Accept: text/plain" http://localhost:8080/

Hello! AILERON Gateway!

Accept: application/json should also works to obtain json response.

Go’s text/template can be used for GoText template type.

$ curl -H "Accept: application/json" http://localhost:8080/

{
  "hello": "AILERON Gateway!"
}

Accept: text/html returns response with embedded information.

Go’s html/template can be used for GoHTML template type.

$ curl -H "Accept: text/html" http://localhost:8080/

<!doctype html>
<html>
<head>
  <title>AILERON Gateway</title>
</head>
<body>
  <h1>AILERON Gateway</h1>
  <p>
    proto : HTTP/1.1</br>
    host : localhost:8080</br>
    method : GET</br>
    path : /</br>
    remote : 127.0.0.1:46818</br>
    header : map[Accept:[text/html] User-Agent:[curl/7.68.0]]</br>
    User-Agent : [curl/7.68.0]</br>
    query : map[]</br>
  </p>
</body>
</html>

If templates are not defined requested by an Accept header, 406 Not Acceptable error will be returned.

curl -H "Accept: text/css" http://localhost:8080/

{"status":406,"statusText":"Not Acceptable"}

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