From bc75dbe4a72210352c2b1c0983b35658d307dca5 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 29 Sep 2020 22:27:04 +0200 Subject: Much.API: make socket configurable --- src/Much/API.hs | 13 +++++++------ src/Much/API/Config.hs | 11 +++++++++++ src/Much/Core.hs | 3 ++- src/Much/State.hs | 4 +++- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 src/Much/API/Config.hs (limited to 'src') diff --git a/src/Much/API.hs b/src/Much/API.hs index 5810c92..0c1bf8d 100644 --- a/src/Much/API.hs +++ b/src/Much/API.hs @@ -1,15 +1,17 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeOperators #-} -module Much.API where +module Much.API (module Much.API) where import Control.Concurrent import Control.Exception (catch, finally, throwIO) import Control.Monad.IO.Class import Data.Function ((&)) import Data.Proxy (Proxy) +import Much.API.Config as Much.API import Much.Event import Much.State import Much.TreeView @@ -34,12 +36,11 @@ type API = api :: Proxy API api = Proxy -main :: (Event -> IO ()) -> IO () -main putEvent = do +main :: Config -> (Event -> IO ()) -> IO () +main Config{socketPath} putEvent = do sock <- socket AF_UNIX Stream defaultProtocol - let sockFile = "/home/tv/tmp/much/warp.sock" -- PID? - removeIfExists sockFile - bind sock $ SockAddrUnix sockFile + removeIfExists socketPath + bind sock $ SockAddrUnix socketPath listen sock maxListenQueue let settings = defaultSettings & setPort 0 diff --git a/src/Much/API/Config.hs b/src/Much/API/Config.hs new file mode 100644 index 0000000..2e3b1cc --- /dev/null +++ b/src/Much/API/Config.hs @@ -0,0 +1,11 @@ +module Much.API.Config where + +data Config = Config + { socketPath :: FilePath + } + +emptyConfig :: Config +emptyConfig = + Config + { socketPath = "/tmp/much.api.sock" + } diff --git a/src/Much/Core.hs b/src/Much/Core.hs index 769fd03..b0f9a51 100644 --- a/src/Much/Core.hs +++ b/src/Much/Core.hs @@ -70,6 +70,7 @@ emptyState = State , unreadSearch = SGR [38,5,250] } , tagSymbols = M.empty + , apiConfig = Much.API.emptyConfig } @@ -164,7 +165,7 @@ runState q0 = do threadIds <- mapM forkIO [ forever $ scan stdin >>= putEvent . EScan - , Much.API.main putEvent + , Much.API.main (apiConfig q1) putEvent ] winchHandler putEvent diff --git a/src/Much/State.hs b/src/Much/State.hs index 43756a7..b7b01e6 100644 --- a/src/Much/State.hs +++ b/src/Much/State.hs @@ -9,9 +9,10 @@ import GHC.Generics import Much.TreeView (TreeView) import Scanner import System.Posix.Signals -import qualified Data.Text as T import qualified Data.Map as M +import qualified Data.Text as T import qualified Data.Tree.Zipper as Z +import qualified Much.API.Config data State = State { cursor :: Z.TreePos Z.Full TreeView @@ -29,6 +30,7 @@ data State = State , mousemap :: Scan -> State -> IO State , tagSymbols :: M.Map T.Text T.Text , colorConfig :: ColorConfig (Blessings String -> Blessings String) + , apiConfig :: Much.API.Config.Config } instance Show (State -> IO ()) where -- cgit v1.2.3