-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract server related functions into new class AuthenticationServer …
…from Authentication
- Loading branch information
1 parent
7a8142a
commit ad8c527
Showing
4 changed files
with
65 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import express, { Express } from 'express'; | ||
import { Server } from 'http'; | ||
import Settings from '../../Settings'; | ||
import Authentication, { Listener, MaybeTokenResponse } from './Authentication'; | ||
|
||
/** | ||
* Creates an express app for the user to authorize the bot to allow changing command permissions | ||
*/ | ||
export default class AuthenticationServer { | ||
static #app: Express = express(); | ||
static #server?: Server = undefined; | ||
|
||
/** | ||
* Starts the express server if it has not been started. | ||
* If no guilds are left that need authentication, the express server is stopped | ||
*/ | ||
public static startServer(): void { | ||
if (AuthenticationServer.#server !== undefined) { | ||
if (!AuthenticationServer.#server.listening) | ||
AuthenticationServer.listen(); | ||
return; | ||
} | ||
if (Settings.settings.logging !== 'errors') | ||
console.log('Start express server'); | ||
|
||
AuthenticationServer.#app.get('/', Authentication.authenticationCallback.bind(Authentication)); | ||
AuthenticationServer.listen(); | ||
} | ||
|
||
/** | ||
* Updates internal server parameter to listen on the app | ||
*/ | ||
private static listen(): void { | ||
if ( | ||
Settings.settings['express-port'] !== undefined && | ||
Settings.settings['express-port'] !== null && | ||
Settings.settings['express-port']! > 0 | ||
) | ||
AuthenticationServer.#server = AuthenticationServer.#app.listen( | ||
Settings.settings['express-port'] | ||
); | ||
else AuthenticationServer.#server = AuthenticationServer.#app.listen(); | ||
} | ||
|
||
/** | ||
* Closes the server | ||
*/ | ||
static closeServer(): void { | ||
this.#server?.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters