Previous part, we added a simple Krakend config. You can see it from here.
What are KrakenD Templates?
A template system gives you full flexibility to work with the configuration file. It comes in handy to: Split a large krakend. json file into several pieces. Inject variables, like environment settings, into the configuration.
create a config folder and add some files
- settings
service.json
We keep our application url in service.json
- templates
We have sample.json which includes category api endpoint. If I add my all endpoints here, It would be bigger and not readable. I divide my endpoints for each apis to templates. So I have more files but it is more readable now.
CategoryApiEndpoint.tmpl
.tmpl is special file extension for KrakenD. CategoryApiEndpoint.tmpl includes just our category api endpoints.
Note: I use docker on windows. So if I want to access my local network, I need to use host.docker.internal instead of localhost
ProductApiEndpoint.tmpl
Endpoint.tmpl
This file has the sum of all templates. We just call api templates we just created. We also say which service will be used by which template.
sample.json
Now, we don’t need to add anymore all endpoints here. We just include our Endpoint template.
update Dockerfile
FROM devopsfaith/krakendCOPY . /etc/krakend/RUN FC_ENABLE=1 \
FC_SETTINGS="config/settings" \
FC_TEMPLATES="config/templates" \
krakend check -t -d -c "config/sample.json"ENTRYPOINT FC_ENABLE=1 \
FC_SETTINGS="/etc/krakend/config/settings"\
FC_TEMPLATES="/etc/krakend/config/templates" \
krakend run -c "/etc/krakend/config/sample.json"
Now run following these commands.
docker build -t simplekrakend .
docker run -d -p 8080:8080 simplekrakend
When we call POST localhost:8080/category, they route our categoryapi (http://localhost:5000/category/insert) behind
Next Post, we will talk about Containerization