File: h2c-server.hs

package info (click to toggle)
haskell-http2 5.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 55,120 kB
  • sloc: haskell: 7,911; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download
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
64
65
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Concurrent
import qualified Control.Exception as E
import Control.Monad
import Network.HTTP2.Server
import Network.Run.TCP
import System.Console.GetOpt
import System.Environment
import System.Exit

import Monitor
import Server

options :: [OptDescr (Options -> Options)]
options =
    [ Option
        ['m']
        ["monitor"]
        (NoArg (\opts -> opts{optMonitor = True}))
        "run thread monitor"
    ]

showUsageAndExit :: String -> IO a
showUsageAndExit msg = do
    putStrLn msg
    putStrLn $ usageInfo usage options
    exitFailure

serverOpts :: [String] -> IO (Options, [String])
serverOpts argv =
    case getOpt Permute options argv of
        (o, n, []) -> return (foldl (flip id) defaultOptions o, n)
        (_, _, errs) -> showUsageAndExit $ concat errs

newtype Options = Options
    { optMonitor :: Bool
    }
    deriving (Show)

defaultOptions :: Options
defaultOptions =
    Options
        { optMonitor = False
        }

usage :: String
usage = "Usage: h2c-server [OPTION] <addr> <port>"

main :: IO ()
main = do
    labelMe "h2c-server main"
    args <- getArgs
    (opts, ips) <- serverOpts args
    (host, port) <- case ips of
        [h, p] -> return (h, p)
        _ -> showUsageAndExit usage
    when (optMonitor opts) $ void $ forkIO $ monitor $ threadDelay 1000000
    runTCPServer (Just host) port $ \s -> do
        E.bracket
            (allocSimpleConfig' s 4096 5000000)
            freeSimpleConfig
            (\conf -> run defaultServerConfig conf server)