Adventures in Haskell (Servant)
October 3, 2020
It is the end of my second week at RC, and I’ve decided to have a go at building a small project using Servant, the Haskell webserver library. The idea is to construct a PDF server — let’s call it pdfServer.io — for files written in MiniLaTeX. The flow of data goes like this:
- A user requests a PDF of a MiniLaTeX document.
- minilatex.lamdera.app sends a POST request to pdfServer.io with the source text of the document.
- pdfServer.io runs
xelatexon the data received and stores the resulting PDF file.
- pdfServer.io replies to the POST request with a URL,
ab1234is the id of the generated PDF file.
- When the URL is received by the MiniLaTeX app, it presents it as a clickable link.
- The user clicks on the link, making a GET request to pdfLatex.io.
- pdfLatex.io replies with the PDF file, which either loads in the browser or is downloaded to disk, depending on the user’s browser and settings.
The documentation for Servant is especially good. Here are the basic references, together with some examples
- Github Repo
- Minimal Example — 48 lines of code!
- Simple Example with Data Persistence – 94 lines of code
- Core Documentation
I am still studying the code and the docs, and am trying out little examples. Hope to get the project going shortly. But as a taste of how awesome Servant is, try out the minimal example and also the example with data persistence (via sqlite). Then take a look at the tutorial, which explains how one can do so much with so few lines of code.