Skip to content

Commit

Permalink
Apply ormolu formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JBetz committed Jun 8, 2024
1 parent 5f2f488 commit 25abbbd
Show file tree
Hide file tree
Showing 21 changed files with 1,271 additions and 1,153 deletions.
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/.retool_types/**": true,
"**/*tsconfig.json": true,
".cache": true,
"retool.config.json": true
},
"nixEnvSelector.nixFile": "${workspaceFolder}/shell.nix"
}
92 changes: 46 additions & 46 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,62 @@ import qualified Data.ByteString.Lazy.Char8 as LC8
import Data.IORef
import Data.Maybe
import Ensemble.Config
import Ensemble.Handler
import Ensemble.Env
import Ensemble.Handler
import Network.HTTP.Types (status400)
import Network.Wai (responseLBS)
import qualified Network.Wai.Handler.WebSockets as WaiWs
import qualified Network.Wai.Handler.Warp as Warp
import qualified Network.Wai.Handler.WebSockets as WaiWs
import qualified Network.WebSockets as WS
import Options.Generic
import System.IO

main :: IO ()
main = do
hSetBuffering stdout NoBuffering
config <- getRecord "Ensemble Audio Engine"
env <- createEnv config
runWebSocketInterface env (fromMaybe 3000 $ port config)

where
hSetBuffering stdout NoBuffering
config <- getRecord "Ensemble Audio Engine"
env <- createEnv config
runWebSocketInterface env (fromMaybe 3000 $ port config)
where
runWebSocketInterface env port' = do
let warpSettings = Warp.setPort port' Warp.defaultSettings
let websocketApp pendingConnection = do
connection <- WS.acceptRequest pendingConnection
sendThread <- forkIO $ forever $ do
outgoingMessage <- readChan $ env_messageChannel env
WS.sendTextData connection (A.encode outgoingMessage)
isOpen <- newIORef True
whileM $ do
incomingMessage <- fmap Just (WS.receiveData connection) `catch` \case
WS.CloseRequest _ _ -> do
killThread sendThread
writeIORef isOpen False
pure Nothing
WS.ConnectionClosed -> do
killThread sendThread
writeIORef isOpen False
pure Nothing
WS.ParseException message -> do
putStrLn $ "PARSE EXCEPTION: " <> message
pure Nothing
WS.UnicodeException message -> do
putStrLn $ "UNICODE EXCEPTION: " <> message
pure Nothing
whenJust incomingMessage $ handleIncomingMessage env
readIORef isOpen
let backupApp _ respond = respond $ responseLBS status400 [] "Not a WebSocket request"
Warp.runSettings warpSettings $ WaiWs.websocketsOr WS.defaultConnectionOptions websocketApp backupApp
let warpSettings = Warp.setPort port' Warp.defaultSettings
let websocketApp pendingConnection = do
connection <- WS.acceptRequest pendingConnection
sendThread <- forkIO $ forever $ do
outgoingMessage <- readChan $ env_messageChannel env
WS.sendTextData connection (A.encode outgoingMessage)
isOpen <- newIORef True
whileM $ do
incomingMessage <-
fmap Just (WS.receiveData connection) `catch` \case
WS.CloseRequest _ _ -> do
killThread sendThread
writeIORef isOpen False
pure Nothing
WS.ConnectionClosed -> do
killThread sendThread
writeIORef isOpen False
pure Nothing
WS.ParseException message -> do
putStrLn $ "PARSE EXCEPTION: " <> message
pure Nothing
WS.UnicodeException message -> do
putStrLn $ "UNICODE EXCEPTION: " <> message
pure Nothing
whenJust incomingMessage $ handleIncomingMessage env
readIORef isOpen
let backupApp _ respond = respond $ responseLBS status400 [] "Not a WebSocket request"
Warp.runSettings warpSettings $ WaiWs.websocketsOr WS.defaultConnectionOptions websocketApp backupApp

handleIncomingMessage env message =
case A.eitherDecodeStrict message of
Right incomingMessage -> do
outgoingMessage <- receiveMessage env incomingMessage
writeChan (env_messageChannel env) outgoingMessage
Left parseError ->
hPutStrLn stderr $ "Parse error: " <> parseError
handleIncomingMessage env message =
case A.eitherDecodeStrict message of
Right incomingMessage -> do
outgoingMessage <- receiveMessage env incomingMessage
writeChan (env_messageChannel env) outgoingMessage
Left parseError ->
hPutStrLn stderr $ "Parse error: " <> parseError

handleOutgoingMessages env =
void $ forkIO $ forever $ do
outgoingMessage <- readChan $ env_messageChannel env
putStrLn $ LC8.unpack (A.encode outgoingMessage)
handleOutgoingMessages env =
void $ forkIO $ forever $ do
outgoingMessage <- readChan $ env_messageChannel env
putStrLn $ LC8.unpack (A.encode outgoingMessage)
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ in
pkgs.haskellPackages.ghc
pkgs.haskellPackages.hoogle
pkgs.haskellPackages.hlint
pkgs.haskellPackages.ormolu
pkgs.cabal2nix
];
shellHook = ''
Expand Down
9 changes: 5 additions & 4 deletions src/Ensemble.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Ensemble
( module Ensemble.API
, module Ensemble.Env
) where
module Ensemble
( module Ensemble.API,
module Ensemble.Env,
)
where

import Ensemble.API
import Ensemble.Env
Loading

0 comments on commit 25abbbd

Please sign in to comment.