Trimness: Keep Your Templates Trim
In this post, we look at this open-source application and learn how to send a render request by using HTTP endpoints. Read on for more!
Join the DZone community and get the full member experience.
Join For FreeTrimness is:
- an open-source project licensed under the Apache License 2.0
- a GitHub repository available at https://github.com/trimou/trimness
- a practical and extensible tool to build a lightweight service to render templates
Trimness is NOT:
- a silver bullet framework/library.
- an application server nor a deployable application.
It is good to use if you want to:
- build a simple reporting application.
- build a rendering component in a microservice architecture.
It is built on several open-source projects:
- Trimou (mustache/handlebars-like templating engine).
- Weld (CDI component model, extensibility).
- Vert.x (web server, routing, event bus).
In a nutshell, the business of Trimness is to render templates using a clear API. So far there are two ways to send a "render request" to Trimness:
- HTTP endpoints
- Vert.x event bus
Get Started With HTTP Endpoints
The best way to start with Trimness is to play with the simple example. Clone the repository, build the project, and run the example shaded jar:
$ git clone git@github.com:trimou/trimness.git
$ cd trimness
$ mvn clean install
$ java -jar examples/simple/target/trimness-example-simple-shaded.jar
First, let's make sure Trimness is up and running (using HTTP GET):
$ curl localhost:8080/monitor/health
You should get something like:
{"checks":[{"id":"org.trimou.trimness.TrimouEngine","result":"success"}],"result":"success"}
Now, let's use curl
to perform a very simple hello world render request (using HTTP POST):
$ curl -X POST -H "Content-Type: application/json" -d '{ "templateContent" : "Hello {{model.name}}!", "model" : { "name" : "Foo"} }' http://localhost:8080/render
The reply should be Hello Foo!
. Also, see the project README for HTTP endpoints details.
There is also a simple test HTML form located at localhost:8080/test
. Try this if you prefer UI over command line. Just insert commits-list.html
in the template id field and hit the render button. You should get a link to a resulting HTML page with a list of the last commits from the Trimness repository.
Note that this example shows one of the extension points - ModelProvider
- in action. GithubModelProvider makes use of GitHub API to fetch the data for the template.
Future and Plans
Trimness defines several extension points which allow you to enrich the built-in functionality. Since the component model is built on CDI, adding a new feature usually involves adding a new bean class which follows an extension point contract (e.g. ModelProvider
) on the classpath. The extension points API is almost finished. Now we'd like to add some optional modules that will provide additional functionality. Feel free to stop by and file a new issue/feature request in https://github.com/trimou/trimness/issues.
Opinions expressed by DZone contributors are their own.
Comments