summaryrefslogtreecommitdiff
path: root/apex/Ibb.hs
blob: 21c1043a6cdf777859992e4ac01b7094b4b1efc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}

module Ibb where

import Biz.Ibb (Person(..), Book(..), allPeople)
import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Lazy (ByteString)
import Data.Maybe (fromMaybe)
import Data.Text.Lazy (Text)
import Data.Text.Lazy.Encoding (encodeUtf8)
import System.Environment (lookupEnv)
import System.Random (newStdGen)
import System.Random.Shuffle (shuffle')
import Text.Blaze (Markup)
import Text.Blaze.Html (Html)
import Text.Blaze.Html.Renderer.Text (renderHtml)
import Text.Hamlet (shamlet)
import Text.Lucius (lucius, renderCss)
import Web.Scotty (ActionM, ScottyM, scotty, get, html, raw, setHeader)

render :: Html -> ActionM ()
render = html . renderHtml

css :: ByteString -> ActionM ()
css src = setHeader "content-type" "text/css" >> raw src

main :: IO ()
main = do
    port <- read <$> fromMaybe "3000" <$> lookupEnv "PORT" :: IO Int
    scotty port routes

routes :: ScottyM ()
routes = do
    get "/" $ do
        r <- liftIO newStdGen
        let peopleList = shuffle' allPeople (length allPeople) r
        render (homepage peopleList)
    get "/custom.css" $ css stylesheet

displayPerson :: Person -> Markup
displayPerson person = [shamlet|
<div .card>
  <img .card-img .img-fluid src=#{_pic person}>
  <div .card-body>
    <h4 .card-title>
      #{_name person}
    <h6>
      <a target=_blank href="https://twitter.com/#{_twitter person}" class="fab fa-twitter">
      <a target=_blank href=#{_website person} class="fas fa-globe">
    <p .card-text>
      #{_blurb person}
      <ul>
        $forall book <- (_books person)
          <li>
            <a target=_blank .text-dark href="https://www.amazon.com/dp/#{_amznref book}">
              #{_title book}
|]

title, subtitle :: Text
title = "Influenced By Books"
subtitle = "Influential people and the books that made them."

homepage :: [Person] -> Markup
homepage peopleList = [shamlet|
<!doctype html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
        rel="stylesheet"
        integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
        crossorigin="anonymous">
  <link rel="stylesheet"
        href="https://use.fontawesome.com/releases/v5.7.1/css/all.css"
        integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr"
        crossorigin="anonymous">
  <link href="/custom.css" rel="stylesheet">
  <title>#{title} | #{subtitle}
  <script>
    (function(f, a, t, h, o, m){
        a[h]=a[h]||function(){
            (a[h].q=a[h].q||[]).push(arguments)
        };
        o=f.createElement('script'),
        m=f.getElementsByTagName('script')[0];
        o.async=1; o.src=t; o.id='fathom-script';
        m.parentNode.insertBefore(o,m)
    })(document, window, '//stats.simatime.com/tracker.js', 'fathom');
    fathom('set', 'siteId', 'IJATN');
    fathom('trackPageview');
<body>
  <div .container.mt-5>
    <div .jumbotron>
      <h1 .display-4>
        #{title}
      <p .lead>
        #{subtitle}

      <p .lead>
        <a href="http://eepurl.com/ghBFjv">
          Get new book recommendations from the world's influencers in your email.

    <div .card-columns>
      $forall person <- peopleList
        #{displayPerson person}
|]

stylesheet :: ByteString
stylesheet = encodeUtf8 . renderCss $ [lucius|
.jumbotron
{ background: #fff
; text-align: center
}
h1.display-4
{ font-family: 'Times New Roman', times, serif
}
|] undefined