Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Server Side Files Documentation

MattMoony edited this page Jan 14, 2020 · 3 revisions

Files (server-side) - A 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/db.js

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.

get_user(uname: string, cb: (err: Error, user: KPUserEntry))

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.

get_priv(pid: int, cb: (err: Error, priv: KPPrivEntry)

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];
add_session(s: KainSession, cb: (err: Error))

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.

get_session(addr: string, token: string, cb: (err: Error, sess: KainSession))

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];
use_session(addr: string, token: string, cb: (err: Error))

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];
del_outtimed_sessions(timeout: int, cb: (err: Error))

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];

lib/session.js

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.

KainSession

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 |
-----------------
new(addr: string, uname: string, cb: (err: Error, sess: KainSession))

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).

get(addr: string, token: string, cb: (err: Error, sess: KainSession))

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.

use(sess: KainSession, cb: (err: Error))

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 Flowchart of the verify_request function

lib/db.json

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
}

lib/session.json

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
}

res/maps/conf.json

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
}

server.js

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.

server.log

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.