aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fastbangs.cabal1
-rw-r--r--fastbangs.yaml17
-rw-r--r--makefile4
-rw-r--r--package.yaml1
-rw-r--r--src/Config.hs34
-rw-r--r--src/Main.hs1
6 files changed, 47 insertions, 11 deletions
diff --git a/fastbangs.cabal b/fastbangs.cabal
index e103e10..386e42d 100644
--- a/fastbangs.cabal
+++ b/fastbangs.cabal
@@ -42,5 +42,6 @@ executable fastbangs
, stm
, text
, warp
+ , yaml
, yesod
default-language: Haskell2010
diff --git a/fastbangs.yaml b/fastbangs.yaml
new file mode 100644
index 0000000..1c2720b
--- /dev/null
+++ b/fastbangs.yaml
@@ -0,0 +1,17 @@
+# values need to be strings, so put quotes where appropriate (everywhere, if in doubt)
+# if environment variables are used as well, they overwrite settings from the config
+
+# port to bind to
+port: "20546"
+# address to bind to. can be a concrete address like 127.0.0.1, or *6 to indicate any ipv4/ipv6 address
+bind-addr: "*4"
+
+# information used for the search description
+# the base-url MUSTN'T include a trailing slash
+base-url: http://localhost:5035
+favicon-url: http://69owo.de/favicon.ico
+
+# access to the UI for accepting/rejecting user-submitted bangs.
+# for help with the admin-pw-hash please read the README.md
+admin-user: bleh
+admin-pw-hash: ""
diff --git a/makefile b/makefile
index 8aea40d..399eb73 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,6 @@
ESFLAGS = --target=esnext --format=esm
-deploy-dir: deploy deploy/bundle.js deploy/style.css deploy/index.html deploy/fastbangs
+deploy-dir: deploy deploy/bundle.js deploy/style.css deploy/index.html deploy/fastbangs deploy/fastbangs.yaml
.PHONY: watch-script watch-style clean deploy-dir
watch-script: frontend/fuzzysort.js
@@ -22,6 +22,8 @@ deploy:
mkdir -p deploy
deploy/index.html: frontend/index.html
cp $< $@
+deploy/fastbangs.yaml: fastbangs.yaml
+ cp $< $@
deploy/fastbangs: $(shell find src -name '*.hs')
stack install --local-bin-path deploy
deploy/bundle.js: $(shell find frontend -name '*.ts') frontend/fuzzysort.js
diff --git a/package.yaml b/package.yaml
index 95a8de7..825d37c 100644
--- a/package.yaml
+++ b/package.yaml
@@ -28,6 +28,7 @@ dependencies:
- persistent-sqlite
- cryptonite
- memory
+- yaml
ghc-options:
- -Wall
diff --git a/src/Config.hs b/src/Config.hs
index d494477..071cc26 100644
--- a/src/Config.hs
+++ b/src/Config.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE OverloadedStrings, LambdaCase #-}
module Config (
Config(..),
@@ -6,9 +6,15 @@ module Config (
makeOpenSearch
) where
+import Prelude hiding (lookup)
+
+import Control.Applicative ((<|>))
+import Data.Aeson.KeyMap (empty, lookup)
+import Data.Functor ((<&>))
import Data.Maybe (fromMaybe)
import Data.String (fromString, IsString)
import Data.Text (Text)
+import Data.Yaml
import Network.Wai.Handler.Warp (HostPreference)
import System.Environment (lookupEnv)
@@ -22,15 +28,23 @@ data Config = Config {
} 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
+getConfig = do
+ confFile <- decodeFileEither "fastbangs.yaml" <&> \case
+ Right ob -> ob
+ Left _ -> empty
+
+ Config
+ <$> (read <$> resolveVal (lookup "port" confFile) "PORT" "20546")
+ <*> resolveVal (lookup "bind-addr" confFile) "BIND_ADDR" "*6"
+ <*> resolveVal (lookup "base-url" confFile) "BASE_URL" "http://localhost:20546"
+ <*> resolveVal (lookup "favicon-url" confFile) "FAVICON_URL" ""
+ <*> resolveVal (lookup "admin-user" confFile) "ADMIN_USER" "bleb"
+ <*> resolveVal (lookup "admin-pw-hash" confFile) "ADMIN_PW_HASH" "" -- prevent login without manual pw
+
+ where resolveVal :: IsString s => Maybe String -> String -> String -> IO s
+ resolveVal mayConf q def = do
+ mayEnv <- lookupEnv q
+ return $ fromString $ fromMaybe def $ mayEnv <|> mayConf
makeOpenSearch :: Config -> Maybe Text -> Text
makeOpenSearch cfg defBang =
diff --git a/src/Main.hs b/src/Main.hs
index 74cc170..0f0146c 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -160,6 +160,7 @@ main = runStdoutLoggingT $ withSqlitePool "banger.db" 2 $ \pool -> do
bs <- liftIO initBangState
cfg <- liftIO getConfig
+ $(logInfo) $ "Using config: " <> T.pack (show cfg)
sApp <- liftIO $ toWaiApp $ Search bs pool cfg -- includes middlewares