1 -- |The entry point of Lucu httpd.
2 module Network.HTTP.Lucu.Httpd
8 import Control.Concurrent
9 import Network hiding (accept)
10 import Network.HTTP.Lucu.Config
11 import Network.HTTP.Lucu.Interaction
12 import Network.HTTP.Lucu.RequestReader
13 import Network.HTTP.Lucu.Resource.Tree
14 import Network.HTTP.Lucu.ResponseWriter
15 import Network.HTTP.Lucu.SocketLike
16 import System.Posix.Signals
18 -- |This is the entry point of Lucu httpd. It listens to a socket and
19 -- waits for clients. Computation of 'runHttpd' never stops by itself
20 -- so the only way to stop it is to raise an exception in the thread
23 -- Note that 'runHttpd' automatically makes SIGPIPE be ignored by
24 -- computing @'System.Posix.Signals.installHandler'
25 -- 'System.Posix.Signals.sigPIPE' 'System.Posix.Signals.Ignore'
26 -- 'Prelude.Nothing'@. This can hardly cause a problem but it may do.
30 -- > module Main where
31 -- > import Network.HTTP.Lucu
34 -- > main = let config = defaultConfig
35 -- > resources = mkResTree [ ([], helloWorld) ]
37 -- > runHttpd config resourcees []
39 -- > helloWorld :: ResourceDef
40 -- > helloWorld = ResourceDef {
41 -- > resUsesNativeThread = False
42 -- > , resIsGreedy = False
44 -- > = Just $ do setContentType $ read "text/plain"
45 -- > output "Hello, world!"
46 -- > , resHead = Nothing
47 -- > , resPost = Nothing
48 -- > , resPut = Nothing
49 -- > , resDelete = Nothing
51 runHttpd :: Config -> ResTree -> [FallbackHandler] -> IO ()
54 do _ <- installHandler sigPIPE Ignore Nothing
56 case cnfSSLConfig cnf of
60 -> do so <- listenOn (sslServerPort scnf)
61 _loopTID <- forkIO $ httpLoop (sslContext scnf, so)
64 httpLoop =<< listenOn (cnfServerPort cnf)
66 httpLoop :: SocketLike s => s -> IO ()
68 = do (h, addr) <- accept so
69 tQueue <- newInteractionQueue
70 readerTID <- forkIO $ requestReader cnf tree fbs h addr tQueue
71 _writerTID <- forkIO $ responseWriter cnf h tQueue readerTID