Skip to content

Commit cb6dd6a

Browse files
committed
Bundle HTTP.app related configuration in HTTP.AppConfig
1 parent fa54ac3 commit cb6dd6a

6 files changed

Lines changed: 30 additions & 18 deletions

File tree

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
packages: .
22

33
program-options
4-
ghc-options: -Werror
4+
ghc-options: -Werror -fhide-source-paths
55

66
tests: True

src/HTTP.hs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE NoImplicitPrelude #-}
33
module HTTP (
4-
withServer
5-
, socketName
6-
, newSocket
7-
, socketAddr
4+
AppConfig(..)
5+
, withServer
86

97
#ifdef TEST
108
, app
@@ -25,8 +23,6 @@ import Network.Socket
2523

2624
import Util
2725
import qualified Trigger
28-
import Config (Config)
29-
import qualified Config
3026
import qualified Config.DeepSeek as Config
3127
import qualified DeepSeek
3228
import GHC.Diagnostic
@@ -35,14 +31,21 @@ import HTTP.Util
3531
import Sensei.API (QuickFixRequest(..), DeepFixRequest(..))
3632
import qualified Sensei.API as API
3733

34+
data AppConfig = AppConfig {
35+
putStrLn :: String -> IO ()
36+
, deepSeek :: Maybe Config.DeepSeek
37+
, dir :: FilePath
38+
, getLastResult :: IO (Trigger.Result, String, [Diagnostic])
39+
}
40+
3841
socketAddr :: FilePath -> SockAddr
3942
socketAddr = SockAddrUnix . socketName
4043

4144
withSocket :: (Socket -> IO a) -> IO a
4245
withSocket = bracket newSocket close
4346

44-
withServer :: (String -> IO ()) -> Config -> FilePath -> IO (Trigger.Result, String, [Diagnostic]) -> IO a -> IO a
45-
withServer putStrLn config dir = withApplication dir . app putStrLn config dir
47+
withServer :: AppConfig -> IO a -> IO a
48+
withServer config = withApplication config.dir $ app config
4649

4750
withApplication :: FilePath -> Application -> IO a -> IO a
4851
withApplication dir application action = do
@@ -65,8 +68,8 @@ withThread asyncAction action = do
6568
takeMVar mvar
6669
return r
6770

68-
app :: (String -> IO ()) -> Config -> FilePath -> IO (Trigger.Result, String, [Diagnostic]) -> Application
69-
app putStrLn config dir getLastResult request respond = case pathInfo request of
71+
app :: AppConfig -> Application
72+
app config@AppConfig { putStrLn, dir, getLastResult } request respond = case pathInfo request of
7073

7174
[] -> requireMethod "GET" $ do
7275
getLastResult >>= textPlain

src/Run.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ run args = do
7171
cleanupAction.add $ Prelude.putStrLn message
7272
cleanupAction.run
7373

74-
HTTP.withServer putStrLn config dir (readMVar lastOutput) $ do
74+
HTTP.withServer (HTTP.AppConfig putStrLn config.deepSeek dir (readMVar lastOutput)) $ do
7575
watchFiles dir queue $ do
7676
mode <- newIORef Lenient
7777
Input.watch stdin (dispatch mode queue) (emitEvent queue Done)
@@ -182,7 +182,7 @@ runWeb args = do
182182
Session.withSession defaultSessionConfig args $ \ session -> do
183183
_ <- trigger session defaultHooks
184184
lock <- newMVar ()
185-
HTTP.withServer Prelude.putStrLn config "" (withMVar lock $ \() -> trigger session defaultHooks) $ do
185+
HTTP.withServer (HTTP.AppConfig Prelude.putStrLn config.deepSeek "" (withMVar lock $ \ () -> trigger session defaultHooks)) $ do
186186
waitForever
187187

188188
defaultSessionConfig :: Session.Config

test/ClientSpec.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ module ClientSpec (spec) where
22

33
import Helper
44

5-
import Config
6-
import HTTP (socketName)
75
import qualified HTTP
6+
import HTTP.Util (socketName)
87
import Client
98
import qualified Trigger
109

@@ -17,7 +16,7 @@ withFailure = withServer Trigger.Failure (withColor Red "failure")
1716
withServer :: Trigger.Result -> String -> (FilePath -> IO a) -> IO a
1817
withServer result text action = do
1918
withTempDirectory $ \ dir -> do
20-
HTTP.withServer (\ _ -> pass) defaultConfig dir (return (result, text, [])) $ do
19+
HTTP.withServer (HTTP.AppConfig (\ _ -> pass) Nothing dir (return (result, text, []))) $ do
2120
action dir
2221

2322
spec :: Spec

test/HTTPSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ spec = do
3535

3636
withApp :: (Trigger.Result, String, [Diagnostic]) -> SpecWith (FilePath, Application) -> Spec
3737
withApp lastResult = around \ item -> withTempDirectory \ dir -> do
38-
item (dir, HTTP.app putStrLn defaultConfig dir $ return lastResult)
38+
item (dir, HTTP.app (HTTP.AppConfig putStrLn Nothing dir (return lastResult)))
3939

4040
withAppWithFailure :: FilePath -> SpecWith (FilePath, Application) -> Spec
4141
withAppWithFailure name = around \ item -> withTempDirectory \ dir -> do
@@ -58,7 +58,7 @@ spec = do
5858
deepSeek = config.deepSeek <|> Just (DeepSeek $ BearerToken "")
5959

6060
app :: Application
61-
app = HTTP.app putStrLn defaultConfig { deepSeek } dir (return $ (Trigger.Failure, "", [err]))
61+
app = HTTP.app (HTTP.AppConfig putStrLn deepSeek dir (return $ (Trigger.Failure, "", [err])))
6262

6363
item (dir, app)
6464
where

test/Helper.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import Language.Haskell.GhciWrapper
5151

5252
import GHC.Diagnostic
5353

54+
import qualified HTTP
55+
5456
timeout :: IO a -> IO (Maybe a)
5557
timeout action = lookupEnv "CI" >>= \ case
5658
Nothing -> System.Timeout.timeout 5_000_000 action
@@ -67,6 +69,14 @@ ghciConfig = Config {
6769
, configEcho = silent
6870
}
6971

72+
_appConfig :: HTTP.AppConfig
73+
_appConfig = HTTP.AppConfig {
74+
putStrLn = \ _ -> pass
75+
, deepSeek = Nothing
76+
, dir = ""
77+
, getLastResult = undefined
78+
}
79+
7080
withTempDirectory :: (FilePath -> IO a) -> IO a
7181
withTempDirectory = withSystemTempDirectory "hspec"
7282

0 commit comments

Comments
 (0)