Intial barebones API.

This commit is contained in:
Dhananjay Balan 2023-02-03 23:40:28 +01:00
parent 7717d9cb24
commit 4e9ed112c4
4 changed files with 77 additions and 2 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use nix

View File

@ -1,8 +1,61 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
module Main where module Main where
import Data.Text (Text(..))
import Data.Aeson
import qualified MyLib (someFunc) import qualified MyLib (someFunc)
import Database.SQLite.Simple
import Database.SQLite.Simple.FromRow (FromRow)
import Database.SQLite.Simple.FromField (fromField)
import Database.SQLite.Simple.QQ
import Network.Wai
import Network.Wai.Handler.Warp
import Deriving.Aeson
import Data.Proxy
import Servant
import Control.Monad.IO.Class
data Quote = Quote { qQuote :: Text
, qAuthor :: Text
, qBook :: Text
} deriving (Show, Eq, Ord, Generic)
deriving (FromJSON,ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "q", CamelToSnake]] Quote
instance FromRow Quote where
fromRow = Quote <$> field <*> field <*> field
main :: IO () main :: IO ()
main = do main = do
putStrLn "Hello, Haskell!" putStrLn "Hello, Haskell!"
MyLib.someFunc let dbfile = "quotes.db"
initDb dbfile
runApp dbfile
type API = Get '[JSON] [Quote]
api :: Proxy API
api = Proxy
initDb :: FilePath -> IO ()
initDb dbFile = withConnection dbFile $ \conn ->
execute_ conn
[sql|CREATE TABLE IF NOT EXISTS quotes (quote text non null, author text, book text)|]
server :: FilePath -> Server API
server dbf = listQuotes dbf
listQuotes :: FilePath -> Handler [Quote]
listQuotes db = liftIO $ withConnection db $ \conn -> query_ conn [sql|SELECT * FROM quotes;|]
runApp :: FilePath -> IO ()
runApp dbfile = run 8081 (serve api $ server dbfile)

View File

@ -69,7 +69,6 @@ library
-- Other library packages from which modules are imported. -- Other library packages from which modules are imported.
build-depends: base ^>=4.16.3.0 build-depends: base ^>=4.16.3.0
-- Directories containing source files. -- Directories containing source files.
hs-source-dirs: lib hs-source-dirs: lib
@ -92,6 +91,13 @@ executable quotes-api
-- Other library packages from which modules are imported. -- Other library packages from which modules are imported.
build-depends: build-depends:
base ^>=4.16.3.0, base ^>=4.16.3.0,
sqlite-simple ^>=0.4.18.0,
text ^>=1.2.5.0,
servant-server ^>=0.19.1,
wai,
warp,
aeson,
deriving-aeson,
quotes-api quotes-api
-- Directories containing source files. -- Directories containing source files.

15
shell.nix Normal file
View File

@ -0,0 +1,15 @@
let
pkgs = import <unstable> { }; # pin the channel to ensure reproducibility!
in
pkgs.haskellPackages.developPackage {
root = ./.;
modifier = drv:
pkgs.haskell.lib.addBuildTools drv (with pkgs.haskellPackages;
[ cabal-install
ghcid
ghc
haskell-language-server
zlib
]);
}