diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Config.hs | 50 | ||||
-rw-r--r-- | src/Main.hs | 20 |
2 files changed, 43 insertions, 27 deletions
diff --git a/src/Config.hs b/src/Config.hs index d19e608..da9206a 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -1,27 +1,41 @@ {-# LANGUAGE OverloadedStrings #-} module Config ( - baseUrl, + getPort, + getHost, + getBaseUrl, makeOpenSearch ) where -import Data.String (IsString) +import Data.Maybe (fromMaybe) +import Data.String (fromString, IsString) +import Network.Wai.Handler.Warp (HostPreference) +import System.Environment (lookupEnv) -baseUrl :: IsString s => s -baseUrl = "http://localhost:20546" +getPort :: IO Int +getPort = (read . fromMaybe "20546") <$> lookupEnv "PORT" -faviconUrl :: IsString s => s -faviconUrl = "https://69owo.de/favicon.ico" +-- currently, "*" seems to bind only to ipv4 while "*6" binds to both +getHost :: IO HostPreference +getHost = (fromString . fromMaybe "*6") <$> lookupEnv "BIND_ADDR" -makeOpenSearch :: (IsString s, Semigroup s) => s -> s -makeOpenSearch searchUrl = - "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\"\n" - <> " xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">\n" - <> " <ShortName>Banger</ShortName>\n" - <> " <Description>Bangs von ddg, ohne ddg</Description>\n" - <> " <InputEncoding>UTF-8</InputEncoding>\n" - <> " <Image width=\"16\" height=\"16\" type=\"image/x-icon\">" <> faviconUrl <> "</Image>\n" - <> " <Url type=\"text/html\" template=\"" <> searchUrl <> "\"/>\n" - <> " <!--<Url type=\"application/x-suggestions+json\" template=\"[suggestionURL]\"/>-->\n" - <> " <!--<moz:SearchForm>[https://example.com/search]</moz:SearchForm>-->\n" - <> "</OpenSearchDescription>\n" +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) => s -> IO s +makeOpenSearch searchUrl = do + favicon <- faviconUrl + return $ "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\"\n" + <> " xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">\n" + <> " <ShortName>Banger</ShortName>\n" + <> " <Description>Bangs von ddg, ohne ddg</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" + <> " <!--<Url type=\"application/x-suggestions+json\" template=\"[suggestionURL]\"/>-->\n" + <> " <!--<moz:SearchForm>[https://example.com/search]</moz:SearchForm>-->\n" + <> "</OpenSearchDescription>\n" diff --git a/src/Main.hs b/src/Main.hs index 5ab2322..2599df5 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -4,7 +4,7 @@ module Main (main) where import Yesod -import Network.Wai.Handler.Warp +import Network.Wai.Handler.Warp hiding (getPort, getHost) import Data.Function ((&)) import Data.Functor ((<&>)) @@ -52,23 +52,25 @@ postSubmitR = return "TODO" getOpenSearchR :: Handler TypedContent getOpenSearchR = do + baseUrl <- liftIO getBaseUrl url <- lookupGetParam "default" <&> \case Nothing -> baseUrl <> "/#{searchTerms}" Just b -> baseUrl <> "/#" <> b <> "#{searchTerms}" - return - $ TypedContent "application/opensearchdescription+xml" - $ toContent - $ makeOpenSearch url + liftIO $ + TypedContent "application/opensearchdescription+xml" + . toContent + <$> makeOpenSearch url main :: IO () main = do s <- Search <$> initBangState - sApp <- toWaiApp s -- includes middlewares + + host <- getHost + port <- getPort let settings = defaultSettings - -- currently, "*" seems to bind only to ipv4 while "*6" binds to both - & setHost "*6" - & setPort 20546 + & setHost host + & setPort port runSettings settings sApp |