<commit_before>{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP #-}
module Network.Wai.Handler.Launch (run) where

import Network.Wai
import Network.HTTP.Types
import qualified Network.Wai.Handler.Warp as Warp
import Data.IORef
import Control.Concurrent
import Foreign
import Foreign.C.String
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as S
import Data.Enumerator (($$), enumList)
import Blaze.ByteString.Builder (fromByteString)
#if !WINDOWS
import System.Cmd (rawSystem)
#endif

ping :: IORef Bool -> Middleware
ping  var app req
    | pathInfo req == ["_ping"] = do
        liftIO $ writeIORef var True
        return $ responseLBS status200 [] ""
    | otherwise = do
        res <- app req
        let renum = responseEnumerator res
        return $ ResponseEnumerator $ \f -> do
            renum $ \status headers -> do
                case lookup "content-type" headers of
                    Just ct
                        | "text/html" `S.isPrefixOf` ct -> do
                            enumList 1 [fromByteString "<script>setInterval(function(){var x;if(window.XMLHttpRequest){x=new XMLHttpRequest();}else{x=new ActiveXObject(\"Microsoft.XMLHTTP\");}x.open(\"GET\",\"/_ping\",false);x.send();},1000)</script>"] $$ f status headers
                    _ -> f status headers

#if WINDOWS
foreign import ccall "launch"
    launch :: IO ()
#else
launch :: IO ()
launch = rawSystem "xdg-open" ["http://localhost:4587/"] >> return ()
#endif

run :: Application -> IO ()
run app = do
    x <- newIORef True
    forkIO $ Warp.runSettings Warp.defaultSettings
        { Warp.settingsPort = 4587
        , Warp.settingsOnException = const $ return ()
        } $ ping x app
    launch
    loop x

loop :: IORef Bool -> IO ()
loop x = do
    let seconds = 2
    threadDelay $ 1000000 * seconds
    b <- readIORef x
    if b
        then writeIORef x False >> loop x
        else return ()<commit_msg>Longer ping interval<commit_after>{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP #-}
module Network.Wai.Handler.Launch (run) where

import Network.Wai
import Network.HTTP.Types
import qualified Network.Wai.Handler.Warp as Warp
import Data.IORef
import Control.Concurrent
import Foreign
import Foreign.C.String
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as S
import Data.Enumerator (($$), enumList)
import Blaze.ByteString.Builder (fromByteString)
#if !WINDOWS
import System.Cmd (rawSystem)
#endif

ping :: IORef Bool -> Middleware
ping  var app req
    | pathInfo req == ["_ping"] = do
        liftIO $ writeIORef var True
        return $ responseLBS status200 [] ""
    | otherwise = do
        res <- app req
        let renum = responseEnumerator res
        return $ ResponseEnumerator $ \f -> do
            renum $ \status headers -> do
                case lookup "content-type" headers of
                    Just ct
                        | "text/html" `S.isPrefixOf` ct -> do
                            enumList 1 [fromByteString "<script>setInterval(function(){var x;if(window.XMLHttpRequest){x=new XMLHttpRequest();}else{x=new ActiveXObject(\"Microsoft.XMLHTTP\");}x.open(\"GET\",\"/_ping\",false);x.send();},60000)</script>"] $$ f status headers
                    _ -> f status headers

#if WINDOWS
foreign import ccall "launch"
    launch :: IO ()
#else
launch :: IO ()
launch = rawSystem "xdg-open" ["http://localhost:4587/"] >> return ()
#endif

run :: Application -> IO ()
run app = do
    x <- newIORef True
    forkIO $ Warp.runSettings Warp.defaultSettings
        { Warp.settingsPort = 4587
        , Warp.settingsOnException = const $ return ()
        } $ ping x app
    launch
    loop x

loop :: IORef Bool -> IO ()
loop x = do
    let seconds = 120
    threadDelay $ 1000000 * seconds
    b <- readIORef x
    if b
        then writeIORef x False >> loop x
        else return ()
{'fraction_non_alphanumeric': 0.11761736496718829, 'fraction_numerical': 0.01287228672387683, 'mean_word_length': 2.6258005489478498, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '334479', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1172, 'n_words': 350}