blob: 45f980807a3f5a49f6fc475f2197939ffe4f3a59 (
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
|
{-# LANGUAGE OverloadedStrings #-}
module Config (
getPort,
getHost,
getBaseUrl,
makeOpenSearch
) where
import Data.Maybe (fromMaybe)
import Data.String (fromString, IsString)
import Network.Wai.Handler.Warp (HostPreference)
import System.Environment (lookupEnv)
getPort :: IO Int
getPort = (read . fromMaybe "20546") <$> lookupEnv "PORT"
-- currently, "*" seems to bind only to ipv4 while "*6" binds to both
getHost :: IO HostPreference
getHost = (fromString . fromMaybe "*6") <$> lookupEnv "BIND_ADDR"
getBaseUrl :: IsString s => IO s
getBaseUrl = (fromString . fromMaybe "http://localhost:20546") <$> lookupEnv "BASE_URL"
faviconUrl :: IsString s => IO s
faviconUrl = (fromString . fromMaybe "https://69owo.de/favicon.ico")
<$> lookupEnv "FAVICON_URL"
makeOpenSearch :: (IsString s, Semigroup s) => Maybe s -> IO s
makeOpenSearch defBang = do
favicon <- faviconUrl
bp <- getBaseUrl
let (searchUrl, name) = case defBang of
Nothing -> (bp <> "/#{searchTerms}", "FastBangs")
Just b -> (bp <> "/#" <> b <> "#{searchTerms}", "FastBangs (" <> b <> ")")
return $ "<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\">" <> favicon <> "</Image>\n"
<> " <Url type=\"text/html\" template=\"" <> searchUrl <> "\"/>\n"
<> "</OpenSearchDescription>\n"
|