aboutsummaryrefslogtreecommitdiff
path: root/src/Config.hs
blob: d494477447d5c6f4d63112569d84cf98d06d7e14 (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
{-# LANGUAGE OverloadedStrings #-}

module Config (
    Config(..),
    getConfig,
    makeOpenSearch
) where

import Data.Maybe (fromMaybe)
import Data.String (fromString, IsString)
import Data.Text (Text)
import Network.Wai.Handler.Warp (HostPreference)
import System.Environment (lookupEnv)

data Config = Config {
    confPort :: Int,
    confHost :: HostPreference,
    confBaseUrl :: Text,
    confFavicon :: Text,
    confUser :: Text,
    confPwHash :: Text
} deriving (Show, Eq)

getConfig :: IO Config
getConfig = Config
    <$> (read <$> getEnvOr "PORT" "20546")
    <*> getEnvOr "BIND_ADDR" "*6"
    <*> getEnvOr "BASE_URL" "http://localhost:20546"
    <*> getEnvOr "FAVICON_URL" "https://69owo.de/favicon.ico"
    <*> getEnvOr "ADMIN_USER" "bleb"
    <*> getEnvOr "ADMIN_PW_HASH" "" -- prevent login without manual pw
  where getEnvOr :: IsString s => String -> s -> IO s
        getEnvOr q def = fromMaybe def . fmap fromString <$> lookupEnv q

makeOpenSearch :: Config -> Maybe Text -> Text
makeOpenSearch cfg defBang =
       "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\"\n"
    <> "                       xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">\n"
    <> "    <ShortName>" <> name <> "</ShortName>\n"
    <> "    <Description>Handles search bangs (mostly) locally.</Description>\n"
    <> "    <InputEncoding>UTF-8</InputEncoding>\n"
    <> "    <Image width=\"16\" height=\"16\" type=\"image/x-icon\">" <> confFavicon cfg <> "</Image>\n"
    <> "    <Url type=\"text/html\" template=\"" <> searchUrl <> "\"/>\n"
    <> "</OpenSearchDescription>\n"

  where (searchUrl, name) = case defBang of
            Nothing -> (confBaseUrl cfg <> "/#{searchTerms}", "FastBangs")
            Just b  -> (confBaseUrl cfg <> "/#" <> b <> "#{searchTerms}", "FastBangs (" <> b <> ")")