about summary refs log tree commit diff
path: root/src/GopherProxy/Params.hs
blob: 72a6991805be5d308adb7151f0f0ba83a0e131bc (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
module GopherProxy.Params
  ( Params (..)
  , params
  , helpfulParams
  ) where

import qualified Data.ByteString as BS
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Text (Text (), pack)
import Data.Text.Encoding
import Network.Mime (MimeType (), defaultMimeType)
import Network.Socket (HostName (), PortNumber ())
import Options.Applicative

data Params
  = Params
    { hostname     :: HostName
    , port         :: PortNumber
    , httpPort     :: Int
    , cssPath      :: Maybe FilePath
    , cssUrl       :: BS.ByteString
    , baseUrl      :: Text
    , listenPublic :: Bool
    , defaultMime  :: MimeType
    , timeoutms    :: Int
    , serverName   :: Maybe HostName
    , title        :: String
    }

helpfulParams :: ParserInfo Params
helpfulParams = info (helper <*> params) fullDesc

params :: Parser Params
params = Params
  <$> strOption
    (long "host"
    <> metavar "HOSTNAME"
    <> help "hostname of the target gopher server")
  <*> optionalWithDefault 70 (option auto
    (long "port"
    <> metavar "PORT"
    <> help "port of the target gopher server"))
  <*> option auto
    (long "http-port"
    <> metavar "PORT"
    <> help "port gopher-proxy should listen on")
  <*> optional (strOption
    (long "css-path"
    <> metavar "PATH"
    <> help "path of the css to be used"))
  <*> optionalWithDefault "/gopher-proxy.css" (option utf8ByteString
    (long "css-url"
    <> metavar "PATH"
    <> help "absolute location of the css on the web server, defaults to \"/gopher-proxy.css\""))
  <*> optionalWithDefault "/" (option auto
    (long "base-url"
    <> metavar "PATH"
    <> help "base url where gopher-proxy is running, defaults to \"/\""))
  <*> switch
    (long "listen-public"
    <> help "wether gopher-proxy should accept connection on public IP addresses.")
  <*> optionalWithDefault defaultMimeType (option auto
    (long "default-mime-type"
    <> metavar "MIMETYPE"
    <> help "gopher-proxy uses this mimetype, if it can't guess the type, defaults to application/octet-stream"))
  <*> optionalWithDefault 10000000 (option auto
    (long "timeout"
    <> metavar "MILLISECONDS"
    <> help "timeout for connecting to the specified gopher server, defaults to 10s."))
  <*> optional (strOption
    (long "server-name"
    <> metavar "HOSTNAME"
    <> help "The server name of the target gopher server, it sends in gopher menus. Defaults to hostname."))
  <*> optionalWithDefault "gopher-proxy" (strOption
    (long "title"
    <> metavar "STRING"
    <> help "title of the resulting html page"))


utf8ByteString :: ReadM BS.ByteString
utf8ByteString = encodeUtf8 . pack <$> str

optionalWithDefault :: a -> Parser a -> Parser a
optionalWithDefault def p = fromMaybe def <$> optional p

-- Thanks enum! <3
#if !MIN_VERSION_network(2,6,3)
instance Read PortNumber where
  readsPrec i str = fmap (\(i,str) -> (fromInteger i,str)) $ readsPrec i str
#endif