runMain :: IO ()
runMain = runStderrLoggingT . filterLogger (\_source level -> level >= LevelWarn)
       $ do
    confFile <- loadConf "auth_service"
    conf <- getAuthServiceConfig confFile
    mbLogging <- liftIO $ lookupEnv "log"
    let logM = case mbLogging of
                Just "true" -> logMiddleware
                _ -> Prelude.id
    conInfo <- getDBConnectInfo confFile
    withDBPool conInfo 5 doMigrate $ \pool -> do
        args <- liftIO getArgs
        noncePool <- liftIO SignedAuth.newNoncePool
        -- AppState for CLI invocations
        let appState = ApiState { apiStateConfig = conf
                                , apiStateAuditSource =
                                  AuditSourceCli { auditSourceCliArguments =
                                                     Text.pack <$> args
                                                 }
                                , apiStateNoncePool = noncePool
                                }
        let run :: forall a m. MonadIO m => API a -> m a
            run = liftIO . runAPI pool appState
        case args of
         ("adduser": args') -> do
             res <- run $ addUser (args' ^.. each . packed)
             case res of
              Nothing -> liftIO $ do
                  hPutStrLn stderr "Could not add user"
                  exitFailure
              Just (UserID uid) -> liftIO $ print uid
         ("chpass": args') -> run $ changePassword args'
         ("addrole" : args') -> run $ addRole args'
         ("rmrole" : args') -> run $ removeRole args'
         ("newinstance": args') -> run $ addInstance' args'
         ("addinstance": args') -> run $ userAddInstance args'
         ("removeinstance": args') -> run $ userRemoveInstance args'
         ("deactivateuser": args') -> run $ userDeactivate' args'
         ("reactivateuser": args') -> run $ userReactivate args'
         ["run"] -> do
           secrets <- getSecrets confFile
           liftIO $ Warp.run 80 (logM $ serveAPI pool noncePool conf secrets)
         _ -> liftIO $ do
             hPutStrLn stderr
               "Usage: auth-service [run|adduser|chpass|addrole|rmrole|newinstance|addinstance|removeinstance|\
               \deactivateuser|eeactivateuser] [options]"
             exitFailure

-- Compares the schema to the SQL server and prints out necessary changes. For
-- development.
runMain :: IO ()
runMain = runStderrLoggingT . filterLogger (\_source level -> level >= LevelWarn)
       $ do
    confFile <- loadConf "auth_service"
    conf <- getAuthServiceConfig confFile
    mbLogging <- liftIO $ lookupEnv "log"
    let logM = case mbLogging of
                Just "true" -> logMiddleware
                _ -> Prelude.id
    conInfo <- getDBConnectInfo confFile
    withDBPool conInfo 5 doMigrate $ \pool -> do
        args <- liftIO getArgs
        noncePool <- liftIO SignedAuth.newNoncePool
        -- AppState for CLI invocations
        let appState = ApiState { apiStateConfig = conf
                                , apiStateAuditSource =
                                  AuditSourceCli { auditSourceCliArguments =
                                                     Text.pack <$> args
                                                 }
                                , apiStateNoncePool = noncePool
                                }
        let run :: forall a m. MonadIO m => API a -> m a
            run = liftIO . runAPI pool appState
        case args of
         ("adduser": args') -> do
             res <- run $ addUser (args' ^.. each . packed)
             case res of
              Nothing -> liftIO $ do
                  hPutStrLn stderr "Could not add user"
                  exitFailure
              Just (UserID uid) -> liftIO $ print uid
         ("chpass": args') -> run $ changePassword args'
         ("addrole" : args') -> run $ addRole args'
         ("rmrole" : args') -> run $ removeRole args'
         ("newinstance": args') -> run $ addInstance' args'
         ("addinstance": args') -> run $ userAddInstance args'
         ("removeinstance": args') -> run $ userRemoveInstance args'
         ("deactivateuser": args') -> run $ userDeactivate' args'
         ("reactivateuser": args') -> run $ userReactivate args'
         ["run"] -> do
           secrets <- getSecrets confFile
           liftIO $ Warp.run 80 (logM $ serveAPI pool noncePool conf secrets)
         _ -> liftIO $ do
             hPutStrLn stderr
               "Usage: auth-service [run|adduser|chpass|addrole|rmrole|newinstance|addinstance|removeinstance|\
               \deactivateuser|eeactivateuser] [options]"
             exitFailure

-- Compares the schema to the SQL server and prints out necessary changes. For
-- development.
runMain = runStderrLoggingT . filterLogger (\_source level -> level >= LevelWarn)
       $ do
    confFile <- loadConf "auth_service"
    conf <- getAuthServiceConfig confFile
    mbLogging <- liftIO $ lookupEnv "log"
    let logM = case mbLogging of
                Just "true" -> logMiddleware
                _ -> Prelude.id
    conInfo <- getDBConnectInfo confFile
    withDBPool conInfo 5 doMigrate $ \pool -> do
        args <- liftIO getArgs
        noncePool <- liftIO SignedAuth.newNoncePool
        -- AppState for CLI invocations
        let appState = ApiState { apiStateConfig = conf
                                , apiStateAuditSource =
                                  AuditSourceCli { auditSourceCliArguments =
                                                     Text.pack <$> args
                                                 }
                                , apiStateNoncePool = noncePool
                                }
        let run :: forall a m. MonadIO m => API a -> m a
            run = liftIO . runAPI pool appState
        case args of
         ("adduser": args') -> do
             res <- run $ addUser (args' ^.. each . packed)
             case res of
              Nothing -> liftIO $ do
                  hPutStrLn stderr "Could not add user"
                  exitFailure
              Just (UserID uid) -> liftIO $ print uid
         ("chpass": args') -> run $ changePassword args'
         ("addrole" : args') -> run $ addRole args'
         ("rmrole" : args') -> run $ removeRole args'
         ("newinstance": args') -> run $ addInstance' args'
         ("addinstance": args') -> run $ userAddInstance args'
         ("removeinstance": args') -> run $ userRemoveInstance args'
         ("deactivateuser": args') -> run $ userDeactivate' args'
         ("reactivateuser": args') -> run $ userReactivate args'
         ["run"] -> do
           secrets <- getSecrets confFile
           liftIO $ Warp.run 80 (logM $ serveAPI pool noncePool conf secrets)
         _ -> liftIO $ do
             hPutStrLn stderr
               "Usage: auth-service [run|adduser|chpass|addrole|rmrole|newinstance|addinstance|removeinstance|\
               \deactivateuser|eeactivateuser] [options]"
             exitFailure

-- Compares the schema to the SQL server and prints out necessary changes. For
-- development.