-
Notifications
You must be signed in to change notification settings - Fork 16
feat(authserver): initialize auth server with database and authentication logic #2784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drazisil
wants to merge
5
commits into
dev
Choose a base branch
from
add-authserver
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9cbc648
feat(authserver): initialize auth server with database and authentica…
drazisil 1649ee5
Update app/authserver/package.json
drazisil 8fade59
fix(authserver): use default port value instead of config for server …
drazisil 06ab92c
feat(authserver): refactor server initialization and add main entry p…
drazisil 6180cfa
fix(authserver): remove database connection check from server startup
drazisil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| AUTH_DB_CONNECTION_URL="sqlite://authserver.db" | ||
| LOG_LEVEL="debug" |
This file contains hidden or 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,69 @@ | ||
| import { IncomingMessage, ServerResponse, createServer } from "http"; | ||
| import { getServerLogger } from "rusty-motors-shared"; | ||
| import { AuthServerConfig } from "./config.ts"; | ||
| import { handleAuthLogin } from "./handleAuthLogin.ts"; | ||
| import { handleShardList } from "./handleShardList.ts"; | ||
|
|
||
| export class AuthServer { | ||
|
|
||
| constructor(private config: AuthServerConfig, private log: ReturnType<typeof getServerLogger>) { | ||
| this.log = log.child({ name: "auth-server" }); | ||
| this.config = config; | ||
| } | ||
|
|
||
| handleRequest(req: IncomingMessage, res: ServerResponse) { | ||
| if (!req.url || !req.method) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request\n'); | ||
| return; | ||
| } | ||
|
|
||
| // Handle incoming requests here | ||
| this.log.info(`Received request: ${req.method} ${new URL(req.url, `http://${req.headers.host}`).pathname}`); | ||
|
|
||
| if (req.url.startsWith("/AuthLogin")) { | ||
| // Handle AuthLogin request | ||
| handleAuthLogin.call(this, req, res); | ||
| } else if (req.url === "/ShardList/") { | ||
| // Handle ShardList request | ||
| // Implement shard list retrieval logic here | ||
| handleShardList.call(this, req, res); | ||
| } | ||
| else { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not Found\n'); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| public start() { | ||
| this.log.info("AuthServer started successfully."); | ||
| // Initialize server components here (e.g., HTTP server, routes, etc.) | ||
| const server = createServer((this.handleRequest).bind(this)); | ||
|
|
||
| const port = parseInt("3000", 10); | ||
| server.listen(port, '0.0.0.0', () => { | ||
| this.log.info(`AuthServer listening on port ${port}`); | ||
| }); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| this.log.info('Received SIGINT. Shutting down gracefully...'); | ||
| server.close(() => { | ||
| this.stop(); | ||
| process.exit(0); | ||
| }); | ||
| }); | ||
|
|
||
| process.on('SIGTERM', () => { | ||
| this.log.info('Received SIGTERM. Shutting down gracefully...'); | ||
| server.close(() => { | ||
| this.stop(); | ||
| process.exit(0); | ||
| }); | ||
| }); | ||
| } | ||
| public stop() { | ||
| this.log.info("AuthServer stopped successfully."); | ||
| // Clean up resources here | ||
| } | ||
| } |
This file contains hidden or 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,27 @@ | ||
| // detroit is a game server, written from scratch, for an old game | ||
| // Copyright (C) <2017> <Drazi Crendraven> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published | ||
| // by the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| export interface AuthServerConfig { | ||
| dbConnectionUrl: string; | ||
| logLevel: string; | ||
| } | ||
|
|
||
| export function getConfig() : AuthServerConfig { | ||
| return { | ||
| dbConnectionUrl: process.env.AUTH_DB_CONNECTION_URL || "sqlite://:memory:", | ||
| logLevel: process.env.LOG_LEVEL || "debug", | ||
| }; | ||
| } | ||
This file contains hidden or 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,26 @@ | ||
| // Constants | ||
| export const DATABASE_PATH = process.env["DATABASE_PATH"] ?? "data/lotus.db"; | ||
| // SQL Queries | ||
| export const SQL = { | ||
| CREATE_USER_TABLE: ` | ||
| CREATE TABLE IF NOT EXISTS user( | ||
| username TEXT UNIQUE NOT NULL, | ||
| password TEXT NOT NULL, | ||
| customerId INTEGER PRIMARY KEY NOT NULL | ||
| ) STRICT`, | ||
| CREATE_SESSION_TABLE: ` | ||
| CREATE TABLE IF NOT EXISTS session( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| contextId TEXT UNIQUE NOT NULL, | ||
| customerId INTEGER NOT NULL, | ||
| profileId INTEGER DEFAULT 0 | ||
|
|
||
| ) STRICT`, | ||
| INSERT_USER: | ||
| "INSERT INTO user (username, password, customerId) VALUES (?, ?, ?)", | ||
| FIND_USER: "SELECT * FROM user WHERE username = ? AND password = ?", | ||
| GET_ALL_USERS: "SELECT * FROM user", | ||
| UPDATE_SESSION: | ||
| "INSERT OR REPLACE INTO session (contextId, customerId, profileId) VALUES (?, ?, ?)", | ||
| FIND_SESSION_BY_CONTEXT: "SELECT * FROM session WHERE contextId = ?", | ||
| } as const; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing port configuration: The AuthServer tries to read a 'port' property from the config but it's not defined in the AuthServerConfig interface or getConfig function. This will cause the server to always use the default port 3000.
Did we get this right? 👍 / 👎 to inform future reviews.