In Bologna, we have a community about Functional Programming, and lately, I was looking for a quick and easy way to host all the data of the talks.
In this post, we will see how we can use GitHub pages to host the JSON files, and kscript to validate the data before “deploy”.
GitHub Pages is a quick way to host data for free so why don’t we use it also to store some static JSON and use it as API in an app?
To use it we just need a Github repo and enable the feature as explained in this guide
Once done we can push the JSON file and it will be hosted for us for free.
file in a git repo: https://github.com/fp-in-bo/data/blob/master/events/all.json
same file hosted by GitHub Pages: https://fp-in-bo.github.io/data/events/all.json
Kscript will bring enhanced scripting support for Kotlin on *nix-based systems, so we can use it fo validate our data before deploying it.
After a quick installation we can start to write our validation script.
To write our first kscript here’s what we need to do:
create empty file
make a script automatically install kscript and its dependencies on the first run if necessary
kscript --add-bootstrap-header script.kts
open the script with some IDE / text editor (I’ll use IntelliJ)
kscript --idea script.kts
write our logic
Kscript allows us to use dependencies, so we’re going to use Jackson in this example. To add the dependency in our script we just need to declare it directly inside the script like following:
In this example we are going to read a file from arguments, then parse it using Jackson, here is the full script:
Another feature of kscript is to create standalone binaries, let’s do it because we’re gonna use it in our continuous integration:
kscript –package script.kts
Our desired flow to publish a new talk to the repo is:
- open a new branch
- add a talk into the JSON
- open a PR
- waiting for checks in Continuous Integration
- merge the PR
This way once the PR will be merged to master, the new talk will be available in our API. In this repository, I’m using Travis for CI, and all we need is to invoke our script passing the right file
That’s all. I find this approach and the mix of these two tools pragmatic and I suggest this approach if you need to host some data that will not change very frequently. You can find here the repo.
Thanks @FChiusolo for the fish.