diff --git a/lib/LiBro/WebService.hs b/lib/LiBro/WebService.hs new file mode 100644 index 0000000..fe44060 --- /dev/null +++ b/lib/LiBro/WebService.hs @@ -0,0 +1,29 @@ +module LiBro.WebService where + +import Data.Aeson +import Data.Proxy +import Servant +import GHC.Generics + +newtype PersonIDs = PersonIDs {personIDs :: [Int]} deriving Generic +instance ToJSON PersonIDs + +type LiBroAPI = "hello" :> Get '[JSON] PersonIDs + :<|> "yay" :> Get '[PlainText] String + +libroServer :: Server LiBroAPI +libroServer = handleHello + :<|> handleYay + where + handleHello :: Handler PersonIDs + handleHello = return $ PersonIDs [17, 42] + + handleYay :: Handler String + handleYay = return "Yay!" + +libroApi :: Proxy LiBroAPI +libroApi = Proxy + +libro :: Application +libro = serve libroApi libroServer + diff --git a/lib/LiBro/WebService/API.hs b/lib/LiBro/WebService/API.hs deleted file mode 100644 index fdc9231..0000000 --- a/lib/LiBro/WebService/API.hs +++ /dev/null @@ -1,9 +0,0 @@ -module LiBro.WebService.API where - -import Data.Proxy -import Servant.API - -type LiBroAPI = "hello" :> Get '[PlainText] String - -libroApi :: Proxy LiBroAPI -libroApi = Proxy diff --git a/lib/LiBro/WebService/Server.hs b/lib/LiBro/WebService/Server.hs deleted file mode 100644 index cb73d7e..0000000 --- a/lib/LiBro/WebService/Server.hs +++ /dev/null @@ -1,10 +0,0 @@ -module LiBro.WebService.Server where - -import LiBro.WebService.API -import Servant - -server :: Server LiBroAPI -server = return "Hello LiBro!" - -libro :: Application -libro = serve libroApi server diff --git a/lib/LiBro/WebService/State.hs b/lib/LiBro/WebService/State.hs new file mode 100644 index 0000000..6beec0f --- /dev/null +++ b/lib/LiBro/WebService/State.hs @@ -0,0 +1,25 @@ +module LiBro.WebService.State where + +import LiBro.Config +import LiBro.Data +import LiBro.Control +import Control.Concurrent + +data LiBroState = LiBroState + { config :: Config + , mvBlocking :: MVar Blocking + , mvData :: MVar LiBroData + } + +lsConfig :: LiBroState -> IO Config +lsConfig = return . config + +lsData :: LiBroState -> IO LiBroData +lsData = readMVar . mvData + +lsInit :: Config -> IO LiBroState +lsInit config = do + mvb <- newEmptyMVar + mvd <- newEmptyMVar + initData config mvb mvd + return $ LiBroState config mvb mvd diff --git a/libro-backend.cabal b/libro-backend.cabal index fda5d1d..cca1fd1 100644 --- a/libro-backend.cabal +++ b/libro-backend.cabal @@ -41,8 +41,8 @@ library , LiBro.Data.Storage , LiBro.Data.SafeText , LiBro.Control - , LiBro.WebService.API - , LiBro.WebService.Server + , LiBro.WebService + , LiBro.WebService.State , LiBro.Util build-depends: aeson , attoparsec @@ -108,3 +108,4 @@ test-suite libro-backend-test , text , transformers , vector + , wai diff --git a/server/Main.hs b/server/Main.hs index 93bc118..2b6a3a9 100644 --- a/server/Main.hs +++ b/server/Main.hs @@ -1,7 +1,7 @@ module Main where import LiBro.Config as Conf -import LiBro.WebService.Server +import LiBro.WebService import Network.Wai.Handler.Warp configuredMain :: Config -> IO ()