2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-12-17 19:55:10 +03:00
|
|
|
TODO This has a bunch of stub logic that was intended for an
|
|
|
|
architecture with a single Urbit daemon running multiple
|
|
|
|
ships. Do it or strip it out.
|
|
|
|
-}
|
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.King.API (King(..), kingAPI, readPortsFile) where
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
import RIO.Directory
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
import Network.Socket (Socket)
|
|
|
|
import Prelude (read)
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Arvo (Belt)
|
|
|
|
import Urbit.King.App (HasConfigDir(..))
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
import qualified Network.HTTP.Types as H
|
|
|
|
import qualified Network.Wai as W
|
|
|
|
import qualified Network.Wai.Handler.Warp as W
|
|
|
|
import qualified Network.Wai.Handler.WebSockets as WS
|
|
|
|
import qualified Network.WebSockets as WS
|
2020-01-24 08:28:38 +03:00
|
|
|
import qualified Urbit.Vere.NounServ as NounServ
|
|
|
|
import qualified Urbit.Vere.Term.API as Term
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
2019-12-17 21:06:20 +03:00
|
|
|
type TermConn = NounServ.Conn Belt [Term.Ev]
|
|
|
|
|
|
|
|
type TermConnAPI = TVar (Maybe (TermConn -> STM ()))
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-12-17 17:31:50 +03:00
|
|
|
Daemon state.
|
|
|
|
-}
|
|
|
|
data King = King
|
2019-12-17 21:06:20 +03:00
|
|
|
{ kServer :: Async ()
|
|
|
|
, kTermConn :: TermConnAPI
|
2019-12-17 17:31:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-12-17 17:31:50 +03:00
|
|
|
Get the filepath of the urbit config directory and the ports file.
|
|
|
|
-}
|
|
|
|
portsFilePath :: HasConfigDir e => RIO e (FilePath, FilePath)
|
|
|
|
portsFilePath = do
|
|
|
|
dir <- view configDirL
|
|
|
|
fil <- pure (dir </> ".king.ports")
|
|
|
|
pure (dir, fil)
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-12-17 17:31:50 +03:00
|
|
|
Write the ports file.
|
|
|
|
-}
|
|
|
|
portsFile :: HasConfigDir e => Word -> RAcquire e (FilePath, FilePath)
|
|
|
|
portsFile por =
|
|
|
|
mkRAcquire mkFile (removeFile . snd)
|
|
|
|
where
|
|
|
|
mkFile = do
|
|
|
|
(dir, fil) <- portsFilePath
|
|
|
|
createDirectoryIfMissing True dir
|
|
|
|
writeFile fil (encodeUtf8 $ tshow por)
|
|
|
|
pure (dir, fil)
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-12-17 17:31:50 +03:00
|
|
|
Get the HTTP port for the running Urbit daemon.
|
|
|
|
-}
|
|
|
|
readPortsFile :: HasConfigDir e => RIO e (Maybe Word)
|
|
|
|
readPortsFile = do
|
|
|
|
(_, fil) <- portsFilePath
|
|
|
|
bs <- readFile fil
|
2019-12-17 19:55:10 +03:00
|
|
|
evaluate (readMay $ unpack $ decodeUtf8 bs)
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
kingServer :: HasLogFunc e => (Int, Socket) -> RAcquire e King
|
|
|
|
kingServer is =
|
|
|
|
mkRAcquire (startKing is) (cancel . kServer)
|
|
|
|
where
|
|
|
|
startKing :: HasLogFunc e => (Int, Socket) -> RIO e King
|
|
|
|
startKing (port, sock) = do
|
2019-12-17 21:06:20 +03:00
|
|
|
api <- newTVarIO Nothing
|
2019-12-17 17:31:50 +03:00
|
|
|
let opts = W.defaultSettings & W.setPort port
|
2019-12-17 21:06:20 +03:00
|
|
|
env <- ask
|
|
|
|
tid <- async $ io $ W.runSettingsSocket opts sock $ app env api
|
|
|
|
pure (King tid api)
|
2019-12-17 17:31:50 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-12-17 17:31:50 +03:00
|
|
|
Start the HTTP server and write to the ports file.
|
|
|
|
-}
|
|
|
|
kingAPI :: (HasConfigDir e, HasLogFunc e)
|
|
|
|
=> RAcquire e King
|
|
|
|
kingAPI = do
|
|
|
|
(port, sock) <- io $ W.openFreePort
|
|
|
|
(dir, fil) <- portsFile (fromIntegral port)
|
2019-12-17 19:55:10 +03:00
|
|
|
-- lockFile dir
|
2019-12-17 17:31:50 +03:00
|
|
|
kingServer (port, sock)
|
|
|
|
|
2019-12-17 21:06:20 +03:00
|
|
|
serveTerminal :: HasLogFunc e => e -> TermConnAPI -> Word -> W.Application
|
|
|
|
serveTerminal env api word =
|
|
|
|
WS.websocketsOr WS.defaultConnectionOptions wsApp fallback
|
2019-12-17 17:31:50 +03:00
|
|
|
where
|
|
|
|
fallback req respond =
|
|
|
|
respond $ W.responseLBS H.status500 []
|
|
|
|
$ "This endpoint uses websockets"
|
|
|
|
|
2019-12-17 21:06:20 +03:00
|
|
|
wsApp pen =
|
|
|
|
atomically (readTVar api) >>= \case
|
|
|
|
Nothing -> WS.rejectRequest pen "Ship not running"
|
|
|
|
Just sp -> do
|
|
|
|
wsc <- io $ WS.acceptRequest pen
|
|
|
|
inp <- io $ newTBMChanIO 5
|
|
|
|
out <- io $ newTBMChanIO 5
|
|
|
|
atomically $ sp $ NounServ.mkConn inp out
|
2019-12-19 16:16:52 +03:00
|
|
|
let doit = runRIO env
|
|
|
|
$ NounServ.wsConn "NOUNSERV (wsServ) " inp out wsc
|
|
|
|
|
|
|
|
-- If `wai` kills this thread for any reason, the TBMChans
|
2020-01-23 05:58:22 +03:00
|
|
|
-- need to be closed. If they are not closed, the
|
2019-12-19 16:16:52 +03:00
|
|
|
-- terminal will not know that they disconnected.
|
|
|
|
finally doit $ atomically $ do
|
|
|
|
closeTBMChan inp
|
|
|
|
closeTBMChan out
|
2019-12-17 17:31:50 +03:00
|
|
|
|
|
|
|
data BadShip = BadShip Text
|
|
|
|
deriving (Show, Exception)
|
|
|
|
|
2019-12-17 21:06:20 +03:00
|
|
|
app :: HasLogFunc e => e -> TermConnAPI -> W.Application
|
|
|
|
app env api req respond =
|
2019-12-17 17:31:50 +03:00
|
|
|
case W.pathInfo req of
|
2019-12-17 21:06:20 +03:00
|
|
|
["terminal", session] -> do
|
2019-12-17 17:31:50 +03:00
|
|
|
session :: Word <- evaluate $ read $ unpack session
|
2019-12-17 21:06:20 +03:00
|
|
|
serveTerminal env api session req respond
|
2019-12-17 17:31:50 +03:00
|
|
|
["status"] ->
|
2019-12-17 21:06:20 +03:00
|
|
|
respond $ W.responseLBS H.status200 [] "{}"
|
2019-12-17 17:31:50 +03:00
|
|
|
_ ->
|
|
|
|
respond $ W.responseLBS H.status404 [] "No implemented"
|