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
- A text template for
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"}
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.