Skip to content

Commit

Permalink
Merge pull request #28 from BrechtSerckx/env
Browse files Browse the repository at this point in the history
Use envvars to construct env for recycle-ics server
  • Loading branch information
BrechtSerckx committed Feb 2, 2024
2 parents c1d7aa3 + 5911a52 commit 9d545f8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ RECYCLE_ICS_PORT=3332
REACT_APP_RECYCLE_ICS_SERVER_URL=http://localhost:$RECYCLE_ICS_PORT

# Override these in local/env
RECYCLE_ICS_CONSUMER="recycleapp.be"
RECYCLE_ICS_SECRET="get-this-by-inspecting-requests-to-recycle-app.be"
45 changes: 39 additions & 6 deletions recycle-ics/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import Colog.Message
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IORef (newIORef)
import Data.String (IsString (fromString))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Language.Haskell.TH.Env as Env
import qualified Language.Haskell.TH.Env as TH.Env
import Network.HTTP.Client.TLS
( newTlsManagerWith,
tlsManagerSettings,
Expand All @@ -26,29 +28,60 @@ import Servant.Client
Scheme (..),
mkClientEnv,
)
import qualified System.Environment as Env
import Text.Read (readMaybe)

main :: IO ()
main = do
Opts {apiClientOpts = ApiClientOpts {..}, ..} <- parseOpts
Opts {..} <- parseOpts
httpManager <- newTlsManagerWith tlsManagerSettings
let clientEnv =
mkClientEnv httpManager $ BaseUrl Https "api.fostplus.be" 443 ""
logAction = LogAction $ liftIO . putStrLn . T.unpack . fmtMessage

authResult <- newIORef Nothing
let env = Env {..}
case cmd of
GenerateIcs GenerateIcsOpts {..} -> do
GenerateIcs GenerateIcsOpts {apiClientOpts = ApiClientOpts {..}, ..} -> do
let env = Env {..}
vCalendar <- flip runRecycle env $ runCollectionQuery collectionQuery
let bs = printVCalendar vCalendar
case outputFile of
Just f -> BSL.writeFile f bs
Nothing -> BSL.putStr bs
ServeIcs ServeIcsOpts {..} -> do
ServeIcs ServeIcsOpts -> do
port <-
lookupEnvRead
"RECYCLE_ICS_PORT"
"Port where recycle-ics runs on."
consumer <-
lookupEnvString
"RECYCLE_ICS_CONSUMER"
"X-Consumer header, get it by inspecting requests to recycleapp.be"
authSecret <-
lookupEnvString
"RECYCLE_ICS_SECRET"
"X-Authorization header, get it by inspecting requests to recycleapp.be"
let env = Env {..}
putStrLn "Starting server"
run port
. logStdoutDev
. simpleCors
$ recycleIcsApp
$$(Env.envQ' "RECYCLE_ICS_WWW_DIR")
$$(TH.Env.envQ' "RECYCLE_ICS_WWW_DIR")
env

lookupEnvRead :: (Read a) => Text -> Text -> IO a
lookupEnvRead var descr = do
mStr <- Env.lookupEnv (T.unpack var)
case mStr of
Nothing -> error . T.unpack $ "Environment variable " <> var <> " expected: " <> descr
Just str -> case readMaybe str of
Nothing -> error . T.unpack $ "Could not read environment variable " <> var <> ": " <> descr
Just a -> pure a

lookupEnvString :: (IsString a) => Text -> Text -> IO a
lookupEnvString var descr = do
mStr <- Env.lookupEnv (T.unpack var)
case mStr of
Nothing -> error . T.unpack $ "Environment variable " <> var <> " expected: " <> descr
Just str -> pure $ fromString str
23 changes: 6 additions & 17 deletions recycle-ics/app/Opts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module Opts
where

import qualified Data.Char as Char
import qualified Network.Wai.Handler.Warp as Warp
import Options.Applicative
import Recycle.Ics.ICalendar
import Recycle.Ics.Types
Expand All @@ -18,8 +17,7 @@ import Recycle.Types.Optparse
import Text.Read (readMaybe)

data Opts = Opts
{ cmd :: Cmd,
apiClientOpts :: ApiClientOpts
{ cmd :: Cmd
}

parseOpts :: IO Opts
Expand All @@ -33,7 +31,6 @@ parseOpts =
pOpts :: Parser Opts
pOpts = do
cmd <- pCmd
apiClientOpts <- pApiClientOpts
pure Opts {..}

data Cmd
Expand All @@ -55,7 +52,8 @@ pCmd =

data GenerateIcsOpts = GenerateIcsOpts
{ outputFile :: Maybe FilePath,
collectionQuery :: CollectionQuery
collectionQuery :: CollectionQuery,
apiClientOpts :: ApiClientOpts
}

pGenerateIcsOpts :: Parser GenerateIcsOpts
Expand All @@ -66,6 +64,7 @@ pGenerateIcsOpts = do
<> help
"output file"
collectionQuery <- pCollectionQuery
apiClientOpts <- pApiClientOpts
pure GenerateIcsOpts {..}

pCollectionQuery :: Parser CollectionQuery
Expand Down Expand Up @@ -139,17 +138,7 @@ pFractionEncoding =
pure $ EncodeFractionAsVTodo due
in pAsEvent <|> pAsTodo

newtype ServeIcsOpts = ServeIcsOpts
{ port :: Warp.Port
}
data ServeIcsOpts = ServeIcsOpts

pServeIcsOpts :: Parser ServeIcsOpts
pServeIcsOpts = do
port <-
option auto $
long "port"
<> short 'p'
<> metavar "PORT"
<> help
"port"
pure ServeIcsOpts {..}
pServeIcsOpts = pure ServeIcsOpts
8 changes: 2 additions & 6 deletions recycle-ics/recycle-ics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ CMD=(cabal run exe:recycle-ics --)
# CMD=(docker run -p "$RECYCLE_ICS_PORT:$RECYCLE_ICS_PORT" recycle:latest)
# CMD=($(nix-build -A recycle-ics)/bin/recycle-ics)

CMD+=(--secret "$RECYCLE_ICS_SECRET")

# Generate ICS file
ZIP_CODE="1234-56789"
STREET="https://data.vlaanderen.be/id/straatnaam-12345"
HOUSE_NUMBER=1
# CMD+=(
# generate-ics \
# --secret "$RECYCLE_ICS_SECRET" \
# --zipcode "$ZIP_CODE" \
# --street "$STREET" \
# --house-number "$HOUSE_NUMBER" \
Expand All @@ -24,10 +23,7 @@ HOUSE_NUMBER=1


# servce ics files
CMD+=(
serve-ics \
--port "$RECYCLE_ICS_PORT"
)
CMD+=(serve-ics)

# run it
"${CMD[@]}" "$@"

0 comments on commit 9d545f8

Please sign in to comment.