newtrouter
newtrouter YAML_CONFIG_FILE
newtrouter is a web service designed to work along side JSON API like that form with Postgres + PostgREST, and a template engine like Newt’s Handlebars template engine. newtrouter accepts a request, if it finds a matching route description it runs the request through a data pipeline of web services returning the results of the last one executed to the web browser or requester. It’s just a data router that manages a pipeline of services for each defined request pattern.
In additional content routing newtrouter can also service out static resources. This is handy during development but less useful if you are using a front end web server such as a production setting.
newtrouter’s configuration uses a declarative model expressed in YAML. It can also allow environment variables read at start up to be part of the data for mapping JSON data source requests. This is particularly helpful for supplying access credentials. You do not express secrets in the newtrouter YAML configuration file. This follows the best practice used when working with container services and Lambda like systems.
newtrouter is configured from a YAML file. The YAML
should not include secrets. Instead you can pass these in via the
environment variables identified the
.appliction.environment
property. Here’s a summary of the
Newt YAML syntax that newtrouter uses.
Top level properties for newtrouter YAML.
The application properties are optional.
Routes hosts a list of request descriptions and their data pipelines. This property is only used by Newt router and Newt code generator.
id
description
request [METHOD ][PATH]
GET /items/{item_id}
would make item_id
available in building your service paths in the pipeline. The pattern
takes up a whole path segment so /blog/{year}-{month}-{day}
would not work but /blog/{year}/{month}/{day}
would capture
the individual elements. The Newt router sits closely on top of the Go
1.22 HTTP package route handling. For the details on how Go 1.22 and
above request handlers and patterns form see See https://tip.golang.org/doc/go1.22#enhanced_routing_patterns
and https://pkg.go.dev/net/http#hdr-Patterns for
explanations.
pipeline
debug
newtrouter
service will log
verbose results to standard out for this specific pipeline
A pipeline is a list of web services containing a type, URL, method and content types
service [METHOD ][URL]
.application.environment
. All the elements extracted from
the elements derived from the request path are passed through strings.
These are then used to construct a simple key-value object of variable
names and objects which are then passed through the Handlebars template
representing the target service URL.
description
timeout
Running newtrouter with a YAML configuration file called “blog.yaml”
newtrouter blog.yaml
An example of a YAML file describing blog like application based on Postgres+PostgREST.
applications:
router:
port: 8010
htdocs: htdocs
template_engine:
port: 8011
postgres:
namespace: blog
port: 5432
dsn: postgres://{PGUSER}:{PGPASSWORD}@localhost:5432/blog.yaml
postgrest:
app_path: postgrest
conf_path: postgrest.conf
port: 3000
enviroment:
- PGUSER
- PGPASSWORD
models:
- id: post
description: A blog post or article
elements:
- type: text
id: post_id
attributes:
name: post_id
placeholdertext: e.g. /<YYYY>/<MM>/<DD>/<SLUG>
title: (required) Enter the path for the blog entry with a unique slug
required: true
- type: text
id: title
attributes:
name: title
title: (optional) Enter a title for your post
placeholdertext: ex. My Blog Post for Today
- type: text
id: byline
attributes:
name: byline
title: (optional) Include a byline for your post
placeholdertext: ex. By Jane Jones, 1912-12-12
- type: textarea
id: content
attributes:
name: content
title: (required) Write your post here
placeholdertext: ex. Something exciting happened today...
required: true
- type: date
id: pubDate
attributes:
name: pubDate
required: "true"
routes:
- id: post_create
request: GET /post_create
description: Handle retrieving the webform for post create
pipeline:
- service: POST http://localhost:8011/post_create
description: Display a post for create
- id: post_create
request: POST /post_create
description: Handle form submission for post create
pipeline:
- service: POST http://localhost:3000/rpc/post_create
description: Access PostgREST API for post create
- service: POST http://localhost:8011/post_create_response
description: This is an result template for post create
- id: post_update
request: GET /post_update/{oid}
description: Handle retrieving the webform for post update
pipeline:
- service: GET http://localhost:3000/rpc/post_read/{oid}
description: Retrieve post from PostgREST API before update
- service: POST http://localhost:8011/post_update
description: Display a post for update
- id: post_update
request: POST /post_update
description: Handle form submission for post update
pipeline:
- service: PUT http://localhost:3000/rpc/post_update/{oid}
description: Access PostgREST API for post update
- service: POST http://localhost:8011/post_update_response
description: This is an result template for post update
- id: post_delete
request: GET /post_delete/{oid}
description: Handle retrieving the webform for post delete
pipeline:
- service: GET http://localhost:3000/rpc/post_read/{oid}
description: Retrieve post from PostgREST API before delete
- service: POST http://localhost:8011/post_delete
description: Display a post for delete
- id: post_delete
request: POST /post_delete
description: Handle form submission for post delete
pipeline:
- service: DELETE http://localhost:3000/rpc/post_delete/{oid}
description: Access PostgREST API for post delete
- service: POST http://localhost:8011/post_delete_response
description: This is an result template for post delete
- id: post_read
request: POST /post_read
description: Retrieve object(s) for post read
pipeline:
- service: GET http://localhost:3000/rpc/post_read/{oid}
description: Access PostgREST API for post read
- service: POST http://localhost:8011/post_read
description: This template handles post read
- id: post_list
request: POST /post_list
description: Retrieve object(s) for post list
pipeline:
- service: GET http://localhost:3000/rpc/post_list
description: Access PostgREST API for post list
- service: POST http://localhost:8011/post_list
description: This template handles post list
templates:
- id: post_create
request: /post_create
template: post_create_form.tmpl
description: Display a post for create
- id: post_create
request: /post_create_response
template: post_create_response.tmpl
description: This is an result template for post create
- id: post_update
request: /post_update
template: post_update_form.tmpl
description: Display a post for update
- id: post_update
request: /post_update_response
template: post_update_response.tmpl
description: This is an result template for post update
- id: post_delete
request: /post_delete
template: post_delete_form.tmpl
description: Display a post for delete
- id: post_delete
request: /post_delete_response
template: post_delete_response.tmpl
description: This is an result template for post delete
- id: post_read
request: /post_read
template: post_read.tmpl
description: This template handles post read
- id: post_list
request: /post_list
template: post_list.tmpl
description: This template handles post list