-
Notifications
You must be signed in to change notification settings - Fork 0
Server Side Files Documentation
Before reading this, you should take a look at the Directory Structure as this is a follow-up page.
Short table of contents:
- lib
- res/maps
- server.js
- server.log
This is the database-access module. All functions necessary for storing and retrieving data from the MySQL-Database housing user- and session-data (Users / Sessions) are implemented here.
As the name suggests, this function is used to retrieve a user's entry from the users
table. (Attributes of users are mentioned in Users / Sessions).
What it will pretty much do is, execute the following SQL-Query ...
SELECT *
FROM users
WHERE uname = [uname];
... and, dependent on whether or not successful, call cb
with the given error and no result or call cb
with no error and the according user-entry.
Used to get the set of privileges specified by the given pid
(privilege id). For a better understanding of privileges have a look at Users / Sessions
SELECT *
FROM privs
WHERE id = [pid];
This function's purpose is to create a new session-entry in the sessions
table based on the given KainSession
's properties' values.
The following SQL-Query will be executed with the given values:
INSERT INTO sessions (ip, token, uname) VALUES([s.ip], [s.token], [s.uname]);
If the query fails, cb
will receive the error in question, otherwise, err
is null
.
This function retrieves the session-entry corresponding to the two identifying factors: addr
(IP-Address) and token
if it exists.
SELECT ip, token, uname,
TIMESTAMPDIFF(MINUTE, timestamp, NOW()) "min"
FROM sessions
WHERE ip = [addr]
AND token = [token];
In unison with its name, this function "uses" a session - it updates its timestamp
.
UPDATE sessions
SET timestamp = NOW()
WHERE ip = [addr]
AND token = [token];
Once again, as the name suggests, this function deletes all sessions that haven't been used for a minimum of timeout
minutes - if it fails, it will call cb
with the corresponding error.
DELETE FROM sessions
WHERE TIMESTAMPDIFF(MINUTE, timestamp, NOW()) >= [timeout];
This is the session-control module. All interactions with sessions will have to use this module's functions. For more information on Sessions see Users / Sessions.
Most functions will involve calls to the db
module, in order to store and retrieve session info from the database.
This is the class representation of a session-entry which contains the most important attributes for session handling.
-----------------
| KainPlan |
-----------------
| ip: string |
| token: string |
| uname: string |
-----------------
Self-explanatory - Creates a new session with the given values and stores it in the database (db.add_session
). The token will be 8 random hex-encoded bytes (=> 16 chars).
Will get the corresponding session-entry from the database (db.get_session
) and call cb
with it. The error - if any occurred - will also be passed to cb
.
Calls db.use_session
with the values specified by sess
. Therefore, it "uses" a session, so to speak - it updates its timestamp.
verify_request(req: express.Request, res: express.Response, cb: (err: Error, sess: KainSession), err_hndlr: (err: Error, res: express.Response))
Can be used in combination with both an Express.Request
and Express.Response
in order to speed up the process of checking requests and sending appropriate error messages.
Flowchart of the verify_request
function
This is the config file for the database-module (db.js
). It contains the essential pieces of information needed in order to allow the module to connect to the MySQL-Database.
{
"host": "[host]", // The database's host address
"user": "[username]", // The database user's username
"pass": "[password]", // The database user's password
"db": "[database]" // The actual name of the database
}
This file stores the configuration details needed for handling sessions. It will be loaded from and used in session.js
.
{
"timeout": timeout, // Specifies how long ([minutes]) sessions should be kept for
"check_interval": chck_intv // Is the pause ([minutes]) between checks for inactive sessions
}
Simply put - this file stores information relevant to the loading of maps.
{
"current_map": "[map_name]" // Specifies the name of the current main map - the one shown to every user
}
The very heart of the backend itself. This is were everything comes together, both the general webserver and the backend logic behind KainPlan are implemented here.
Most of the functionality provided by this file is covered by API-Endpoints, since this file is mainly responsible for web-access.
This is were all errors that have caused an HTTP Error 500 will be logged to in order to discover the cause of them later on.