summaryrefslogtreecommitdiff
path: root/src/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 46b6108..5bf7138 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -54,13 +54,16 @@ type PhotoApi =
:> AuthenticationHeader
:> ReqBody '[JSON] RenameRequest
:> Post '[JSON] (Either RenameError ())
+ :<|> "quit" :> Get '[JSON] ()
type Token = String
+{-# NOINLINE cache #-}
+cache = unsafePerformIO (newMVar [])
+
isAuthenticated = Authentication.isAuthenticated clientIds users cache
where clientIds = splitOn "," flags_client_ids
users = splitOn "," flags_allowed_users
- cache = unsafePerformIO (newMVar [])
config = Config
{ pendingPath = flags_pending_path
@@ -68,20 +71,23 @@ config = Config
}
checkAuthenticated :: Maybe Token -> EitherT ServantErr IO ()
-checkAuthenticated (Just token) = liftIO (isAuthenticated token) >>= \case
- True -> return ()
- False -> left err503 { errBody = "Not authenticated" }
+checkAuthenticated (Just token) = do
+ authenticated <- liftIO (isAuthenticated token)
+ unless authenticated $ left err503 { errBody = "Not authenticated" }
checkAuthenticated Nothing = left err503 { errBody = "Missing token" }
server :: Server PhotoApi
server = albums
:<|> rename
+ :<|> quit
where albums token = checkAuthenticated token >> liftIO (getAlbums config)
rename token (RenameRequest from to) = do
_ <- checkAuthenticated token
liftIO $ runEitherT (renameAlbum config from to)
+ quit = liftIO exitFailure
+
photoApi :: Proxy PhotoApi
photoApi = Proxy
app = logStdoutDev $ serve photoApi server