summaryrefslogtreecommitdiff
path: root/src/Shop/Server.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Shop/Server.hs')
-rw-r--r--src/Shop/Server.hs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/Shop/Server.hs b/src/Shop/Server.hs
new file mode 100644
index 0000000..21d0d65
--- /dev/null
+++ b/src/Shop/Server.hs
@@ -0,0 +1,49 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE TypeOperators #-}
+module Shop.Server where
+
+import Shop.API
+import Shop.Config
+import Shop.Types
+import Shop.Handler
+import qualified Control.Category as Category
+import Control.Monad.Logger (runStdoutLoggingT)
+import Protolude
+import Servant.API
+import Servant.Server hiding (Handler)
+import Network.Socket
+import Network.Wai.Handler.Warp (defaultSettings,
+ runSettings,
+ runSettingsSocket,
+ setHost,
+ setPort)
+
+
+server :: ServerT ShopAPI App
+server = charge
+
+app :: Config -> Application
+app cfg = serve shopAPI (readerServer cfg)
+
+readerServer :: Config -> Server ShopAPI
+readerServer cfg = appToInternalHandler cfg `enter` server
+
+serveApp :: (Config -> Application) -> IO ()
+serveApp f = do
+ cfg <- getConfig
+ let logger = setLogger (getConfigEnv cfg)
+ let settings = setHost (getConfigHost cfg) $
+ setPort (getConfigPort cfg) defaultSettings
+ case getConfigSocket cfg of
+ Nothing -> runSettings settings (logger $ f cfg)
+ Just s -> do
+ sock <- socket AF_UNIX Stream 0
+ bind sock $ SockAddrUnix "shop-server.sock"
+ listen sock maxListenQueue
+ runSettingsSocket settings sock (logger $ f cfg)
+ close sock
+
+main :: IO ()
+main = serveApp app