- Notifications
You must be signed in to change notification settings - Fork18
Using the data from your API, generate the HTML on the fly! Server-side rendering of the mustache templates
License
devopsfaith/api2html
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
API2HTML is a web server that rendersMustache templates and injects them your API data. This allows you to build websites by just declaring the API sources and writing the template view.
To create pages that feed from a backend you just need to add in the configuration file the URL patterns the server will listen to. Let's imagine we want to offer URLs like/products/13-inches-laptops where the second part is a variable that will be sent to the API:
..."pages":[{ "name": "products", "URLPattern": "/products/:category", "BackendURLPattern": "http://api.company.com/products/:category", "Template": "products_list", "CacheTTL": "3600s", "extra": { "promo":"Black Friday" }},...TheTemplate setting will look for the filetmpl/products_list.mustache and the response of the BackendURLPattern call will be injected in the variabledata. An example of how you could use it:
<h1>Products for sale</h1><p>Take advantage of the {{extra.promo}}!</p><table> {{#data}} <tr> <td>{{name}}</td> <td>{{price}}</td> </tr> {{/data}} {{^data}} <tr> <td colspan="2">There are no products in this category</td> </tr> {{/data}}</table>You probably guessed it already, but in this scenario the backend would be returning a response like this:
// http://api.company.com/products/13-inches-laptops{ [ { "name": "13-inch MacBook Air", "price": "$999.00" }, { "name": "Lenovo ThinkPad 13", "price": "$752.00" }, { "name": "Dell XPS13", "price": "$925.00" } ]}When you installapi2html for the first time you need to download the dependencies, automatically managed bydep. Install it with:
$ make prepareOnce all dependencies are installed just run:
$ makeOnce you have successfully compiled API2HTML in your platform the binaryapi2html will exist in the folder. Execute it as follows:
$ ./api2html -hTemplate Render As A ServiceUsage: api2html [command]Available Commands: generate Generate the final api2html templates. serve Run the api2html server.Use "api2html [command] --help" for more information about a command.$ ./api2html run -hRun the api2html server.Usage: api2html serve [flags]Aliases: serve, run, server, startExamples:api2html serve -d -c config.json -p 8080Flags: -c, --config string Path to the configuration filename (default "config.json") -d, --devel Enable the devel -p, --port int Listen port (default 8080)The generator allows you to create multiple mustache files using templating. That's right create templates with templates!
$ ./api2html generate -hGenerate the final api2html templates.Usage: api2html generate [flags]Aliases: generate, create, newExamples:api2html generate -d -c config.jsonFlags: -i, --iso string (comma-separated) iso code of the site to create (default "*") -p, --path string Base path for the generation (default ".") -r, --reg string regex filtering the sources to move to the output folder (default "ignore")$ curl -X PUT -F "file=@/path/to/tmpl.mustache" -H "Content-Type: multipart/form-data" \http://localhost:8080/template/<TEMPLATE_NAME>To build the project with Docker:
$ make dockerAnd run it as follows:
$ docker run -it --rm -p8080:8080 -v $PWD/config.json:/etc/api2html/config.json api2html -d -c /etc/api2html/config.jsonAbout
Using the data from your API, generate the HTML on the fly! Server-side rendering of the mustache templates
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.
