aboutsummaryrefslogtreecommitdiff
path: root/src/Sproxy/Config.hs
blob: e0f35a3816d2ec4d010d037a8566d1a0dc732530 (plain)
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
94
{-# LANGUAGE OverloadedStrings #-}
module Sproxy.Config (
  BackendConf(..)
, ConfigFile(..)
, OAuth2Conf(..)
) where

import Control.Applicative (empty)
import Data.Aeson (FromJSON, parseJSON)
import Data.HashMap.Strict (HashMap)
import Data.Int (Int64)
import Data.Text (Text)
import Data.Word (Word16)
import Data.Yaml (Value(Object), (.:), (.:?), (.!=))

import Sproxy.Logging (LogLevel(Debug))

data ConfigFile = ConfigFile {
  cfListen       :: Word16
, cfSsl          :: Bool
, cfUser         :: String
, cfHome         :: FilePath
, cfLogLevel     :: LogLevel
, cfSslCert      :: Maybe FilePath
, cfSslKey       :: Maybe FilePath
, cfSslCertChain :: [FilePath]
, cfKey          :: Maybe String
, cfListen80     :: Maybe Bool
, cfHttpsPort    :: Maybe Word16
, cfBackends     :: [BackendConf]
, cfOAuth2       :: HashMap Text OAuth2Conf
, cfDataFile     :: Maybe FilePath
, cfDatabase     :: Maybe String
, cfPgPassFile   :: Maybe FilePath
, cfHTTP2        :: Bool
} deriving (Show)

instance FromJSON ConfigFile where
  parseJSON (Object m) = ConfigFile <$>
        m .:? "listen"         .!= 443
    <*> m .:? "ssl"            .!= True
    <*> m .:? "user"           .!= "sproxy"
    <*> m .:? "home"           .!= "."
    <*> m .:? "log_level"      .!= Debug
    <*> m .:? "ssl_cert"
    <*> m .:? "ssl_key"
    <*> m .:? "ssl_cert_chain" .!= []
    <*> m .:? "key"
    <*> m .:? "listen80"
    <*> m .:? "https_port"
    <*> m .:  "backends"
    <*> m .:  "oauth2"
    <*> m .:? "datafile"
    <*> m .:? "database"
    <*> m .:? "pgpassfile"
    <*> m .:? "http2"          .!= True
  parseJSON _ = empty


data BackendConf = BackendConf {
  beName         :: String
, beAddress      :: String
, bePort         :: Maybe Word16
, beSocket       :: Maybe FilePath
, beCookieName   :: String
, beCookieDomain :: Maybe String
, beCookieMaxAge :: Int64
, beConnCount    :: Int
} deriving (Show)

instance FromJSON BackendConf where
  parseJSON (Object m) = BackendConf <$>
        m .:? "name"           .!= "*"
    <*> m .:? "address"        .!= "127.0.0.1"
    <*> m .:? "port"
    <*> m .:? "socket"
    <*> m .:? "cookie_name"    .!= "sproxy"
    <*> m .:? "cookie_domain"
    <*> m .:? "cookie_max_age" .!= (7 * 24 * 60 * 60)
    <*> m .:? "conn_count"     .!= 32
  parseJSON _ = empty


data OAuth2Conf = OAuth2Conf {
  oa2ClientId     :: String
, oa2ClientSecret :: String
} deriving (Show)

instance FromJSON OAuth2Conf where
  parseJSON (Object m) = OAuth2Conf <$>
        m .: "client_id"
    <*> m .: "client_secret"
  parseJSON _ = empty