# Building a Web app with FP - Part I - Servant

**URL:** <https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960>\
**Category:** Links\
**Created:** [February 26, 2021, 12:31pm UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960 "2021-02-26T12:31:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matsumonkie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/matsumonkie/32/922_2.png) [@matsumonkie](https://discourse.haskell.org/u/matsumonkie)\
**Post date:** [February 26, 2021, 12:31pm UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/1 "2021-02-26T12:31:34Z")

</div>

Howdy! I’m writing a series on building from scratch a web app with FP.

[https://matsumonkie.fr/post/1/](https://matsumonkie.fr/post/1/)

There will be articles about packaging with Nix, running on NixOS, configuring with Dhall, testing with Ghcid, creating a web page with Elm, and maybe more fun stuff.

To make the read of code easier, I’ve made [kamo](https://github.com/matsumonkie/kamo), which in a nutshell helps writing technical articles with diffs.

If you’ve read this first article, I’m curious about what you think about kamo. Did you find it useful to understand the code? Anything you didn’t like about it?

---

<div class="post-metadata">

**Author:** ![gilmi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/gilmi/32/45_2.png) [@gilmi](https://discourse.haskell.org/u/gilmi)\
**Post date:** [February 27, 2021, 6:09am UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/2 "2021-02-27T06:09:35Z")

</div>

You probably want to change the title and move the link into the body of your message, right now it’s not clickable.

I liked the article and am interested in reading the next posts!

I also liked the diff idea and tried to use it in the past without much success. But currently the side by side view isn’t working very well because the windows are too small.

I’d recommend either adopting a formatting style that keep lines short (probably good anyway especially for articles) to the point where one doesn’t need to scroll, or (/and) display the changes inline instead of side by side.

Another thing, I think the article kinda assumes that the reader already knows servant and advanced type system features - it assume the user understands that it’s possible to create types which represent APIs, what the servant combinators look like, etc without explaining those stuff which are fairly non trivial.

If the point is to write an article for people who have some sort of an understanding of servant that’s fine, but for people who don’t understand it yet, explaining what’s written and how things work will make it more accessible.

---

<div class="post-metadata">

**Author:** ![matsumonkie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/matsumonkie/32/922_2.png) [@matsumonkie](https://discourse.haskell.org/u/matsumonkie)\
**Post date:** [February 27, 2021, 8:16am UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/3 "2021-02-27T08:16:23Z")

</div>

I changed the title and it’s much better indeed 🙂

Yes, I try to not dive too much into the details otherwise it’s going to look like more like a book rather than a blog post (there is a lot to cover, type level, monad & monad transformer…). But maybe I should give a bit more details.

> [@gilmi](#):
>
> I’d recommend either adopting a formatting style that keep lines short (probably good anyway especially for articles) to the point where one doesn’t need to scroll, or (/and) display the changes inline instead of side by side.

That’s curious… Would you mind sharing your OS and browser (or perhaps are you browsing on your mobile?)? I made it so the whole diff would be displayed without having to scroll. But I guess my css skills failed me again 😅

Anyways, thanks a lot for the feedback!

---

<div class="post-metadata">

**Author:** ![gilmi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/gilmi/32/45_2.png) [@gilmi](https://discourse.haskell.org/u/gilmi)\
**Post date:** [February 27, 2021, 11:49am UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/4 "2021-02-27T11:49:31Z")

</div>

> [@matsumonkie](#):
>
> Yes, I try to not dive too much into the details otherwise it’s going to look like more like a book rather than a blog post (there is a lot to cover, type level, monad & monad transformer…). But maybe I should give a bit more details.

Of course, I’m not saying you should cover Haskell from first principles, monads and monad transformers are not novel in Haskell world and there are many articles and books explaining them (you even linked one), but servant and type level programming (for servant) might not be as common.

Think about who you are writing these articles for and what you think they understand - maybe your target audience is familiar with enough type level programming and read about servant in the past even if they never wrote servant code before and they will not have any problem following along (I’m in this group), but if you are targeting people who don’t know what servant is or are not really familiar with type level programming, maybe some information will be required, maybe even links to the right articles.

> [@matsumonkie](#):
>
> That’s curious… Would you mind sharing your OS and browser (or perhaps are you browsing on your mobile?)? I made it so the whole diff would be displayed without having to scroll. But I guess my css skills failed me again

I’ve read it on mobile! (firefox). indeed on my 1440p desktop monitor fullscreen only a bit of scrolling was needed, if I cut it in half (as I normally do) we have about 700px for two screens side by side and that’s not much.

---

<div class="post-metadata">

**Author:** ![matsumonkie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/matsumonkie/32/922_2.png) [@matsumonkie](https://discourse.haskell.org/u/matsumonkie)\
**Post date:** [March 1, 2021, 9:54am UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/5 "2021-03-01T09:54:33Z")

</div>

I agree, re-reading it now and it feels like I sometimes target beginners, sometimes intermediate users. Thanks for the advice, I’ll try to focus on a single audience 🙂

edit: I have updated the article with your advice in mind 👍

---

<div class="post-metadata">

**Author:** ![sekun](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/sekun/32/1332_2.png) [@sekun](https://discourse.haskell.org/u/sekun)\
**Post date:** [March 25, 2021, 12:18pm UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/6 "2021-03-25T12:18:07Z")

</div>

Had some time to read this. Seems good so far, and I like how you made it obvious which lines were changed. Really useful.

### Package issue

```haskell
sekun@pop-os:~/Projects/hweather$ stack install

Error: While constructing the build plan, the following exceptions were encountered:

In the dependencies for hweather-0.1.0.0:
    servant-flatten needed, but the stack configuration has no specified version (latest matching version is 0.2)
needed since hweather is a build target.

Some different approaches to resolving this:

  * Recommended action: try adding the following to your extra-deps in /home/sekun/Projects/hweather/stack.yaml:

- servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234

```

~~I followed its recommendation but I’m not sure if I’m supposed to cause I don’t know what stack trickery is required since I’ve never used it before. It _seems_ to be fine.~~

It works!

## curl

So with this one I was kinda confused with for a bit since I wasn’t sure exactly what the `curl` request would look like for `PUT /api/weather/:location`. It doesn’t mention how the user could try this out, only for the `GET` request.

To be more specific, I was confused with how `ReqBody` translates to the actual JSON body. Servant’s docs had a `User` object which I assumed that it would just be a JSON object but was confused with what the key of the JSON object would be in this case. Turns out, the entire thing was just a string! Well, figures hahah.

```haskell
:<|> ReqBody '[JSON] String :> Put '[JSON] String

```

So turns out it was just this:

```haskell
> curl -H "Content-Type: application/json" -X PUT -d '"sunny"' localhost:8888/api/weather/Paris
# "sunny"

```

---

<div class="post-metadata">

**Author:** ![matsumonkie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/matsumonkie/32/922_2.png) [@matsumonkie](https://discourse.haskell.org/u/matsumonkie)\
**Post date:** [March 25, 2021, 8:24pm UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/7 "2021-03-25T20:24:46Z")

</div>

Oh yes, you’re right, I’ll edit the post to add the curl command for the PUT request as well 🙂  
And Yes, also correct for the stack thing. I actually had the same warning but because it depends on which stack resolver you’re using, I didn’t explain it 🙂

I’ve made a repo with all the branches (i.e: branch part-I == article part-I) [here](https://github.com/matsumonkie/weather-api) if you some details are missing from the article (like the stack thing)!

---

<div class="post-metadata">

**Author:** ![emilypi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/emilypi/32/5307_2.png) [@emilypi](https://discourse.haskell.org/u/emilypi)\
**Post date:** [March 25, 2021, 9:50pm UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/8 "2021-03-25T21:50:44Z")

</div>

😌😌😌😌😌😌😌 monke 😌😌😌😌😌😌😌

---

<div class="post-metadata">

**Author:** ![sekun](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/sekun/32/1332_2.png) [@sekun](https://discourse.haskell.org/u/sekun)\
**Post date:** [March 26, 2021, 6:14am UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/9 "2021-03-26T06:14:42Z")

</div>

Great post though! Thanks too for referencing resources for the harder topics. I’m excited to read the next one when I get some more free time. 😃

---

<div class="post-metadata">

**Author:** ![matsumonkie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/matsumonkie/32/922_2.png) [@matsumonkie](https://discourse.haskell.org/u/matsumonkie)\
**Post date:** [March 26, 2021, 7:15am UTC](https://discourse.haskell.org/t/building-a-web-app-with-fp-part-i-servant/1960/10 "2021-03-26T07:15:23Z")

</div>

It means a lot, thanks you sekun 🙂
