-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.hs
64 lines (52 loc) · 1.77 KB
/
Application.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
module Application where
import Tunneler.Common
import Web.Simple
import Web.Frank
import System.Process
import System.Exit
import Control.Monad.IO.Class
import Data.Aeson
import Control.Exception
import Network.HTTP.Simple
import System.Environment
import ResultStatus
import Tunnel
import Authentication
app :: (Application -> IO ()) -> IO ()
app runner = do
settings <- newAppSettings
let username = envUsername settings
let password = envPassword settings
runner $ controllerApp settings $
authenticated "tunneler" username password $ do
get "/" $
respond $ okJson $ encode okStatus
post "/actions/stop" $
runCmd "killall ngrok" okStatus errorStatus
get "/actions/status" $ do
runningStatus <- liftIO getRunningTunnelStatus
runCmd "pgrep ngrok > /dev/null"
runningStatus
(ResultStatus "STOPPED" Nothing)
post "/actions/start" $ do
let command = "/usr/local/bin/ngrok start --all -log=stdout > /dev/null"
_ <- liftIO $ spawnCommand command
respond $ okJson $ encode (ResultStatus "STARTING" Nothing)
getRunningTunnelStatus :: IO ResultStatus
getRunningTunnelStatus = do
result <- liftIO (try getTunnelInfo
:: IO (Either HttpException (Maybe Tunnel)))
return $ ResultStatus "RUNNING" (tunnelResult result)
where
tunnelResult (Left _) = Nothing
tunnelResult (Right maybeTunnel) = maybeTunnel
runCmd :: String -> ResultStatus -> ResultStatus -> ControllerT s IO b
runCmd command success err = do
result <- liftIO $ system command
case result of
ExitSuccess ->
respond $ okJson $ encode success
ExitFailure _ ->
respond $ okJson $ encode err