Skip to content

Commit

Permalink
Remove jwtSettings from AppServices
Browse files Browse the repository at this point in the history
  • Loading branch information
cgeorgii committed Sep 18, 2023
1 parent f911a20 commit 178e1cb
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 62 deletions.
2 changes: 1 addition & 1 deletion server/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Main where

import App
import App (run)

main :: IO ()
main = run
7 changes: 5 additions & 2 deletions spec/TaggerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import Tagger.Owned (Owned (Owned))
import Tagger.Tag (Tag (Tag))
import Tagger.User (User)
import Test.Hspec (Spec, around, describe, it, runIO, shouldMatchList, shouldSatisfy)
import TestServices (testServices)
import TestServices (mkTestEnv, testServices)
import Prelude hiding (getContents)

withTaggerApp :: (Port -> IO ()) -> IO ()
withTaggerApp = testWithApplication $ mkApp <$> testServices
withTaggerApp test = do
env <- mkTestEnv
let application = mkApp env <$> testServices env
testWithApplication application test

hasStatus :: Status -> Either ClientError a -> Bool
hasStatus status = \case
Expand Down
68 changes: 35 additions & 33 deletions spec/TestServices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,42 @@ import Infrastructure.SystemTime as SystemTime
import Optics
import Servant.Auth.Server (defaultJWTSettings, generateKey)

testServices :: IO AppServices
testServices = do
mkTestEnv :: IO Env
mkTestEnv = do
key <- generateKey
userMap <- newTVarIO mempty
contentsMap <- newTVarIO mempty
SystemTime.withHandle $ \timeHandle ->
Logger.withHandle timeHandle $ \loggerHandle -> do
let env =
Env
{ config = error "[TestServices.hs] Config not loaded in tests.",
jwkKey = key,
handles =
Handles
{ logger = loggerHandle,
database = error "[TestServices.hs] Database handle not initialized in tests.",
systemTime = timeHandle
}
}
userRepository = Repo.User.inMemory userMap
contentsRepository = Repo.Content.inMemory contentsMap
passwordManager =
encryptedPasswordManager
(env & #handles % #logger %~ withContext "PasswordManager")
(bcryptPasswordManager (defaultJWTSettings env.jwkKey))
authenticator = Auth.authenticator userRepository passwordManager
authenticateUser =
connectedAuthenticateUser
(env & #handles % #logger %~ withContext "Authenticator")
authenticator
pure $
AppServices
{ jwtSettings = defaultJWTSettings key,
passwordManager = passwordManager,
contentRepository = connectedContentRepository env contentsRepository,
userRepository = connectedUserRepository env userRepository,
authenticateUser
pure
Env
{ config = error "[TestServices.hs] Config not loaded in tests.",
jwkKey = key,
handles =
Handles
{ logger = loggerHandle,
database = error "[TestServices.hs] Database handle not initialized in tests.",
systemTime = timeHandle
}
}

testServices :: Env -> IO AppServices
testServices env = do
userMap <- newTVarIO mempty
contentsMap <- newTVarIO mempty
let userRepository = Repo.User.inMemory userMap
contentsRepository = Repo.Content.inMemory contentsMap
passwordManager =
encryptedPasswordManager
(env & #handles % #logger %~ withContext "PasswordManager")
(bcryptPasswordManager (defaultJWTSettings env.jwkKey))
authenticator = Auth.authenticator userRepository passwordManager
authenticateUser =
connectedAuthenticateUser
(env & #handles % #logger %~ withContext "Authenticator")
authenticator
pure $
AppServices
{ passwordManager = passwordManager,
contentRepository = connectedContentRepository env contentsRepository,
userRepository = connectedUserRepository env userRepository,
authenticateUser
}
11 changes: 8 additions & 3 deletions src/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ data ApplicationAPI mode = ApplicationAPI
-- |
-- Setup all the application server, providing the services needed by the various endpoints
mkAPI :: AppServices -> ApplicationAPI AsServer
mkAPI AppServices {passwordManager, contentRepository, userRepository, authenticateUser} =
mkAPI services =
ApplicationAPI
{ tagger = authenticatedTaggerServer contentRepository,
{ tagger =
authenticatedTaggerServer services.contentRepository,
docs = Docs.api,
healthcheck = Healthcheck.api,
authentication = Authentication.api passwordManager authenticateUser userRepository
authentication =
Authentication.api
services.passwordManager
services.authenticateUser
services.userRepository
}

-- |
Expand Down
9 changes: 3 additions & 6 deletions src/API/AppServices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Infrastructure.Authentication.PasswordManager qualified as PasswordManage
import Infrastructure.Logging.Logger (withContext)
import Optics
import Servant (Handler)
import Servant.Auth.Server (JWTSettings, defaultJWTSettings)
import Servant.Auth.Server (defaultJWTSettings)
import Tagger.Authentication.Authenticator (Authenticator)
import Tagger.Authentication.Authenticator qualified as Auth
import Tagger.Repository.Content (ContentRepository)
Expand All @@ -30,8 +30,7 @@ import Prelude hiding (log)
-- |
-- Collection of services needed by the application to work
data AppServices = AppServices
{ jwtSettings :: JWTSettings,
passwordManager :: PasswordManager Handler,
{ passwordManager :: PasswordManager Handler,
contentRepository :: ContentRepository Handler,
userRepository :: UserRepository Handler,
authenticateUser :: Auth.Authenticator Handler
Expand All @@ -57,10 +56,8 @@ start env =
connectedAuthenticateUser
(env & #handles % #logger %~ withContext "Authenticator")
authenticator
jwtSettings = defaultJWTSettings env.jwkKey
in AppServices
{ jwtSettings,
passwordManager,
{ passwordManager,
contentRepository,
userRepository,
authenticateUser
Expand Down
8 changes: 5 additions & 3 deletions src/App.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module App (run) where

import API.AppServices qualified as AppServices
import API.Config (Config (..), Port (..), apiPort)
import API.Config qualified as Config
import App.Env (Env (..))
import Application (mkApp')
import Application (mkApp)
import Boot (Handles (..), boot)
import CLIOptions (CLIOptions (configPath))
import CLIOptions qualified
Expand All @@ -19,8 +20,9 @@ run = do

boot config $ \handles -> do
let port = config.api.apiPort.getPort
context = Env {handles, config, jwkKey}
application = mkApp' context
env = Env {handles, config, jwkKey}
services = AppServices.start env
application = mkApp env services

Logger.logInfo handles.logger $ "Accepting connections on port " <> show port <> "."
Warp.run port application
25 changes: 11 additions & 14 deletions src/Application.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
module Application (mkApp', mkApp) where
module Application (mkApp) where

import API
import API.AppServices as AppServices
import API.AppServices
import App.Env qualified as App
import Data.Proxy (Proxy (..))
import Middleware qualified
import Network.Wai (Application)
import Servant (Context (EmptyContext, (:.)), serveWithContext)
import Servant.Auth.Server (defaultCookieSettings)
import Servant.Auth.Server (defaultCookieSettings, defaultJWTSettings)

mkApp' :: App.Env -> Application
mkApp' context =
let services = AppServices.start context
app = mkApp services
mkApp :: App.Env -> AppServices -> Application
mkApp env services =
let jwtSettings = defaultJWTSettings env.jwkKey
app =
serveWithContext
(Proxy :: Proxy API)
(defaultCookieSettings :. jwtSettings :. EmptyContext)
(mkAPI services)
in Middleware.apply app

mkApp :: AppServices -> Application
mkApp appServices =
serveWithContext
(Proxy :: Proxy API)
(defaultCookieSettings :. jwtSettings appServices :. EmptyContext)
(mkAPI appServices)

0 comments on commit 178e1cb

Please sign in to comment.