diff --git a/examples/blog/README.md b/examples/blog/README.md new file mode 100644 index 0000000..77567e3 --- /dev/null +++ b/examples/blog/README.md @@ -0,0 +1,20 @@ +# Example blog + +The following code shows a very basic example of API2HTML serving pages. In order to run it: + + cd examples/blog + api2html serve -d -c config.json + +This will start the server and you will be able to navigate the following pages: + + - [Home](http://localhost:8080/) + - [Post](http://localhost:8080/posts/1) + - [robots.txt](http://localhost:8080/robots.txt) + - [sitemap.xml](http://localhost:8080/sitemap.xml) + - [hello.txt](http://localhost:8080/hello.txt): An example of `text/plain` content + - [404 page](http://localhost:8080/idontexist) + - 500 page: You need to break the API response to see it + +See the `config.json` to understand how this works and the `tmpl` folder to see how `{{data}}` is injected. + +The template system is [Mustache](https://mustache.github.io) diff --git a/examples/blog/config.json b/examples/blog/config.json new file mode 100644 index 0000000..1894321 --- /dev/null +++ b/examples/blog/config.json @@ -0,0 +1,33 @@ +{ + "robots": true, + "sitemap": true, + "static_txt_content": [ + "hello.txt" + ], + "pages":[ + { + "name": "post", + "URLPattern": "/posts/:post", + "BackendURLPattern": "https://jsonplaceholder.typicode.com/posts/:post", + "Template": "post", + "Layout": "main", + "CacheTTL": "3600s" + }, + { + "name": "home", + "URLPattern": "/", + "BackendURLPattern": "https://jsonplaceholder.typicode.com/posts", + "Template": "home", + "Layout": "main", + "CacheTTL": "3600s", + "IsArray": true, + "extra": {"is_home":true } + } + ], + "templates": {"home":"tmpl/home.mustache","post":"tmpl/post.mustache"}, + "layouts": {"main":"tmpl/main_layout.mustache"}, + "extra":{ + "lang": "en-US", + "copyright": "© 2018 My Company" + } +} diff --git a/examples/blog/static/404 b/examples/blog/static/404 new file mode 100644 index 0000000..5deb41f --- /dev/null +++ b/examples/blog/static/404 @@ -0,0 +1,12 @@ + + +
+ + +The page you are looking for is not hosted in this site
+You might want to customize this file by editing static/404
The response from the API was weird an unable to process it.
+You might want to customize this file by editing static/500
This site is powered by an API and all the source code is either configuration or templating. Enjoy!
+You are seeing the tmpl/home.mustache
template
The response from the API is: + + https://jsonplaceholder.typicode.com/posts/{{data.id}} + +
++{ + "userId": {{data.userId}}, + "id": {{data.id}}, + "title": "{{data.title}}", + "body": "{{data.body}}" +} ++ +