Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] signal on a semaphore when the proxy-server is initialized #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ cabal-dev
*.chi
*.chs.h
.virthualenv
.stack-work/
11 changes: 9 additions & 2 deletions Network/HTTP/Proxy/Server.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
-- | A library for programming custom proxy servers.
module Network.HTTP.Proxy.Server (proxyMain
,proxyMain_
,Settings (..)
,Cache (..)
,Default(..)) where
Expand All @@ -11,6 +12,7 @@ import Network.HTTP.Server.Logger
import Data.Default.Class
import Network.HostName
import Control.Monad.Reader
import Control.Concurrent.MVar (MVar,newEmptyMVar)

-- | The proxy monad: Reader (for settings) over IO
type Proxy s a = ReaderT (Settings s) IO a
Expand All @@ -19,7 +21,12 @@ type ProxyResponse s = Proxy s (Response s)

-- | Proxy entry-point. Spawns a new proxy server.
proxyMain :: forall s. HStream s => Settings s -> IO ()
proxyMain settings = (`runReaderT` settings) $
proxyMain settings = newEmptyMVar >>= proxyMain_ settings

-- | Proxy entry-point. Spawns a new proxy server.
-- When the proxy is initalized, signal a waiting thread by putting on the semaphore.
proxyMain_ :: forall s. HStream s => Settings s -> MVar () -> IO ()
proxyMain_ settings ready = (`runReaderT` settings) $
do mhname <- asks hostname
hname <- case mhname of
Nothing -> lift getHostName
Expand All @@ -30,7 +37,7 @@ proxyMain settings = (`runReaderT` settings) $
,srvHost = hname
,srvLog = log}
myLogInfo $ "Proxy server started on port " ++ (show port)
lift $ serverWith config (proxyHandler settings)
lift $ serverWith_ config (proxyHandler settings) ready

myLogInfo :: String -> Proxy s ()
myLogInfo s = asks logger >>= \l -> lift (logInfo l 0 s)
Expand Down
8 changes: 8 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
flags: {}
extra-package-dbs: []
packages:
- '.'
- '../http-server'
extra-deps:
- mime-0.4.0.2
resolver: lts-3.12