Skip to content

Commit

Permalink
Add config to web service actions
Browse files Browse the repository at this point in the history
  • Loading branch information
memowe committed Feb 19, 2024
1 parent 9de8070 commit 4ad8bc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/LiBro/WebService.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module LiBro.WebService where

import LiBro.Config
import Data.Aeson
import Data.Proxy
import Servant
Expand All @@ -11,19 +12,18 @@ instance ToJSON PersonIDs
type LiBroAPI = "hello" :> Get '[JSON] PersonIDs
:<|> "yay" :> Get '[PlainText] String

libroServer :: Server LiBroAPI
libroServer = handleHello
:<|> handleYay
libroServer :: Config -> Server LiBroAPI
libroServer cfg = handleHello cfg
:<|> handleYay
where
handleHello :: Handler PersonIDs
handleHello = return $ PersonIDs [17, 42]
handleHello :: Config -> Handler PersonIDs
handleHello cfg = return $ PersonIDs [17, 42]

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4 on ubuntu-latest

Defined but not used: ‘cfg’

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4 on ubuntu-latest

This binding for ‘cfg’ shadows the existing binding

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4 on ubuntu-latest

Defined but not used: ‘cfg’

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4 on ubuntu-latest

This binding for ‘cfg’ shadows the existing binding

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6 on ubuntu-latest

Defined but not used: ‘cfg’

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6 on ubuntu-latest

This binding for ‘cfg’ shadows the existing binding

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6 on ubuntu-latest

Defined but not used: ‘cfg’

Check warning on line 20 in lib/LiBro/WebService.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6 on ubuntu-latest

This binding for ‘cfg’ shadows the existing binding

handleYay :: Handler String
handleYay = return "Yay!"

libroApi :: Proxy LiBroAPI
libroApi = Proxy

libro :: Application
libro = serve libroApi libroServer

libro :: Config -> Application
libro = serve libroApi . libroServer
6 changes: 3 additions & 3 deletions server/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import LiBro.WebService
import Network.Wai.Handler.Warp

configuredMain :: Config -> IO ()
configuredMain config = do
let port = Conf.port $ Conf.server config
configuredMain cfg = do
let port = Conf.port $ Conf.server cfg

Check warning on line 9 in server/Main.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4 on ubuntu-latest

This binding for ‘port’ shadows the existing binding

Check warning on line 9 in server/Main.hs

View workflow job for this annotation

GitHub Actions / GHC 9.6 on ubuntu-latest

This binding for ‘port’ shadows the existing binding
putStrLn $ "Serving LiBro backend on port " ++ show port ++ "."
run port libro
run port (libro cfg)

main :: IO ()
main = readConfig >>= maybe complain configuredMain
Expand Down
6 changes: 5 additions & 1 deletion test/LiBro/WebServiceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Test.Hspec.Wai.QuickCheck

import LiBro.Config
import LiBro.WebService
import Data.Default
import Data.ByteString

spec :: Spec
spec = describe "RESTful JSON web service" $ do
helloLibro

helloLibro :: Spec
helloLibro = describe "Dummy: hello libro!" $ with (return libro) $ do
helloLibro = describe "Dummy: hello libro!" $ with (return cfgLibro) $ do

describe "Yay endpoint" $ do
it "Respond with 200 greeting" $ do
Expand All @@ -30,3 +32,5 @@ helloLibro = describe "Dummy: hello libro!" $ with (return libro) $ do
property $ \endpoint ->
show endpoint /= "hello" ==>
get (pack endpoint) `shouldRespondWith` 404

where cfgLibro = libro $ Config def def

0 comments on commit 4ad8bc5

Please sign in to comment.