Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created December 21, 2022 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoyberg/b69e3b6b2663650c4e96b45903bf3943 to your computer and use it in GitHub Desktop.
Save snoyberg/b69e3b6b2663650c4e96b45903bf3943 to your computer and use it in GitHub Desktop.
Yes you really have to do this in two files these days :(
{-# LANGUAGE OverloadedStrings, QuasiQuotes, TemplateHaskell, TypeFamilies #-}
module App where
import ClassyPrelude hiding (Handler)
import Control.Exception.Safe (throw)
import Data.Aeson
import Deriving.Aeson
import GHC.Generics
import Prelude ()
import Yesod
data Book =
Book { isbn :: Text
, author :: Text
, bTitle :: Text
} deriving (Eq, Generic, Ord, Show)
instance FromJSON Book
instance ToJSON Book
data Movie =
Movie { mTitle :: Text
, director :: Text
, cast :: [Text]
} deriving (Eq, Generic, Ord, Show)
instance FromJSON Movie
instance ToJSON Movie
data CatalogItem = CatalogBook Book
| CatalogMovie Movie
deriving (Eq, Generic, Ord, Show)
instance FromJSON CatalogItem
instance ToJSON CatalogItem
data App = App
{ libraryCatalog :: TVar [CatalogItem]
}
mkYesodData "App" [parseRoutes|
/books#Text BooksR GET
/movies#Text MoviesR GET
/purchaseBook PurchaseBookR PUT
/purchaseMovie PurchaseMovieR PUT
/health HealthR GET
|]
#!/usr/bin/env stack --verbose script --resolver=lts20.4 --verbose
{-# LANGUAGE OverloadedStrings, QuasiQuotes, TemplateHaskell, TypeFamilies #-}
import ClassyPrelude hiding (Handler)
import Control.Exception.Safe (throw)
import Data.Aeson
import Deriving.Aeson
import GHC.Generics
import Prelude ()
import Yesod
import App
instance Yesod App
getBooksR :: Handler Html
getBooksR = undefined
getMoviesR :: Handler Html
getMoviesR = undefined
putPurchaseBookR :: Handler Html
putPurchaseBookR = undefined
putPurchaseMovieR :: Handler Html
putPurchaseMovieR = undefined
getHealthR :: Handler Html
getHealthR = do
putStrLn "Health!"
defaultLayout [whamlet|"Health!"|]
mkYesodDispatch "App" resourcesApp
main :: IO ()
main = do
putStrLn "in main"
let
initialCatalog = [ CatalogBook Book{isbn="1", bTitle="Dune", author="Frank Herbert"}
, CatalogMovie Movie{mTitle="Dune", director="Denis Villeneuve", cast=[]}
, CatalogBook Book{isbn="2", bTitle="How To Win Twilight Imperium", author="A Space Lion"}
]
icTVar <- atomically $ newTVar initialCatalog
warp 3000 App { libraryCatalog = icTVar }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment