main :: IO ()
main = do
  -- Because this would mean our site wouldn't load, try loading our index file
  -- before we bother starting up the server at all. Force unwrap to raise an
  -- error if it cannot be found.
  indexTemplate <- loadHTML "index.html"
  let index = fromJust indexTemplate
  -- Initialize our database before running the Scotty app to avoid this
  -- being hit everytime we process an action.
  conn <- Simple.open databaseName
  let config = Config { _configConnection = conn }
  runReaderT initialize config
  -- Allow passing in the config throughout our various Scotty actions. Example
  -- pulled from:
  -- https://github.com/scotty-web/scotty/blob/master/examples/reader.hs
  scottyOptsT def (\m -> runReaderT (runConfigM m) config) $ do
    Scotty.middleware $ Static.staticPolicyWithOptions
      Static.defaultOptions $ Static.addBase "dist"
    -- Retrieve HTML of our index page.
    get "" $ do
      Scotty.html $ fromStrict $ substitute index (Script "index")
    -- Retrieve HTML of a specific post.
    get "/post/:slug" $ do
      slug <- Scotty.param "slug"
      Scotty.html $ fromStrict $ substitute index (Script slug)
    -- Retrieve a list of all posts currently on our blog. This is used within
    -- our homepage.
    get "/api/posts" $ do
      posts <- getPostList
      Scotty.json posts
    -- Retrieve details of a specific post.
    get "/api/post/:slug" $ do
      slug <- Scotty.param "slug"
      post <- getPost slug
      case post of
        Just r -> Scotty.json r
        Nothing -> Scotty.status status404
main :: IO ()
main = do
  -- Because this would mean our site wouldn't load, try loading our index file
  -- before we bother starting up the server at all. Force unwrap to raise an
  -- error if it cannot be found.
  indexTemplate <- loadHTML "index.html"
  let index = fromJust indexTemplate
  -- Initialize our database before running the Scotty app to avoid this
  -- being hit everytime we process an action.
  conn <- Simple.open databaseName
  let config = Config { _configConnection = conn }
  runReaderT initialize config
  -- Allow passing in the config throughout our various Scotty actions. Example
  -- pulled from:
  -- https://github.com/scotty-web/scotty/blob/master/examples/reader.hs
  scottyOptsT def (\m -> runReaderT (runConfigM m) config) $ do
    Scotty.middleware $ Static.staticPolicyWithOptions
      Static.defaultOptions $ Static.addBase "dist"
    -- Retrieve HTML of our index page.
    get "" $ do
      Scotty.html $ fromStrict $ substitute index (Script "index")
    -- Retrieve HTML of a specific post.
    get "/post/:slug" $ do
      slug <- Scotty.param "slug"
      Scotty.html $ fromStrict $ substitute index (Script slug)
    -- Retrieve a list of all posts currently on our blog. This is used within
    -- our homepage.
    get "/api/posts" $ do
      posts <- getPostList
      Scotty.json posts
    -- Retrieve details of a specific post.
    get "/api/post/:slug" $ do
      slug <- Scotty.param "slug"
      post <- getPost slug
      case post of
        Just r -> Scotty.json r
        Nothing -> Scotty.status status404
main = do
  -- Because this would mean our site wouldn't load, try loading our index file
  -- before we bother starting up the server at all. Force unwrap to raise an
  -- error if it cannot be found.
  indexTemplate <- loadHTML "index.html"
  let index = fromJust indexTemplate
  -- Initialize our database before running the Scotty app to avoid this
  -- being hit everytime we process an action.
  conn <- Simple.open databaseName
  let config = Config { _configConnection = conn }
  runReaderT initialize config
  -- Allow passing in the config throughout our various Scotty actions. Example
  -- pulled from:
  -- https://github.com/scotty-web/scotty/blob/master/examples/reader.hs
  scottyOptsT def (\m -> runReaderT (runConfigM m) config) $ do
    Scotty.middleware $ Static.staticPolicyWithOptions
      Static.defaultOptions $ Static.addBase "dist"
    -- Retrieve HTML of our index page.
    get "" $ do
      Scotty.html $ fromStrict $ substitute index (Script "index")
    -- Retrieve HTML of a specific post.
    get "/post/:slug" $ do
      slug <- Scotty.param "slug"
      Scotty.html $ fromStrict $ substitute index (Script slug)
    -- Retrieve a list of all posts currently on our blog. This is used within
    -- our homepage.
    get "/api/posts" $ do
      posts <- getPostList
      Scotty.json posts
    -- Retrieve details of a specific post.
    get "/api/post/:slug" $ do
      slug <- Scotty.param "slug"
      post <- getPost slug
      case post of
        Just r -> Scotty.json r
        Nothing -> Scotty.status status404