Mantis App v0.1.13
Loading...
Searching...
No Matches
mantis Namespace Reference

router.h More...

Classes

struct  AdminTable
 
struct  AuthTable
 
struct  BaseTable
 
class  Context
 
class  CrudInterface
 
class  DatabaseUnit
 Database Management Class. More...
 
struct  ExprEvaluator
 Struct instance for handling evaluation of database access rules. More...
 
struct  Field
 
class  HttpUnit
 
class  JWT
 
class  LoggingUnit
 
class  MantisApp
 Mantis entry point. More...
 
class  MantisLoggerImpl
 Logger implementation for soci, allowing us to override the default logging behaviour with our own custom logger. More...
 
struct  RouteHandler
 Struct encompassing the list of middlewares and the handler function registered to a specific route. More...
 
struct  RouteKeyHash
 
class  Router
 Router class allows for managing routes as well as acting as a top-wrapper on the HttpUnit. More...
 
class  RouteRegistry
 
class  SettingsUnit
 Manages application settings. More...
 
class  SysTablesUnit
 Class to manage tables metadata like access rules, name, etc. More...
 
struct  SystemTable
 
struct  Table
 
class  TableUnit
 Class to model the behaviour of a table in database. More...
 
class  Validator
 
struct  ViewTable
 

Typedefs

using json = nlohmann::json
 Shorten JSON namespace.
 
using Request = httplib::Request
 

‍Shorthand for httplib::Request


 
using Response = httplib::Response
 

‍Shorthand for httplib::Response


 
using Middleware = std::function< bool(const Request &, Response &, Context &)>
 

‍Middleware shorthand for the function


 
using RouteHandlerFunc = std::function< void(const Request &, Response &, Context &)>
 

‍Route Handler function shorthand


 
using Method = std::string
 

‍Syntactic sugar for request method which is a std::string


 
using Path = std::string
 

‍Syntactic sugar for request path which is a std::string


 
using RouteKey = std::pair< Method, Path >
 

‍Shorthand notation for the request's method, path pair.


 
typedef enum mantis::LogLevel LogLevel
 
using Log = LoggingUnit
 
typedef enum mantis::TableType TableType
 
typedef std::string Rule
 

Enumerations

enum class  DbType { SQLITE = 0x01 , PSQL , MYSQL }
 Enum for which database is currently selected. More...
 
enum class  LogLevel : uint8_t {
  TRACE = 0 , DEBUG , INFO , WARN ,
  CRITICAL
}
 
enum class  TableType { Base = 1 , Auth , View }
 

Functions

 NLOHMANN_JSON_SERIALIZE_ENUM (TableType, { {TableType::Base, "base"}, {TableType::Auth, "auth"}, {TableType::View, "view"} }) typedef enum class FieldTypeDecl
 
 NLOHMANN_JSON_SERIALIZE_ENUM (FieldType, { { FieldType::XML, "xml" }, { FieldType::STRING, "string" }, { FieldType::DOUBLE, "double" }, { FieldType::DATE, "date" }, { FieldType::INT8, "int8" }, { FieldType::UINT8, "uint8" }, { FieldType::INT16, "int16" }, { FieldType::UINT16, "uint16" }, { FieldType::INT32, "int32" }, { FieldType::UINT32, "uint32" }, { FieldType::INT64, "int64" }, { FieldType::UINT64, "uint64" }, { FieldType::BLOB, "blob" }, { FieldType::JSON, "json" }, { FieldType::BOOL, "bool" }, }) const std
 
std::optional< FieldTypegetFieldType (const std::string &fieldName)
 
bool fieldExists (const TableType &type, const std::string &fieldName)
 
bool isValidFieldType (const std::string &fieldType)
 
fs::path joinPaths (const std::string &path1, const std::string &path2)
 < Use shorthand fs to refer to the std::filesystem
 
fs::path resolvePath (const std::string &input_path)
 
bool createDirs (const fs::path &path)
 Create directory, given a path.
 
std::string dirFromPath (const std::string &path)
 Returns a created/existing directory from a path.
 
void toLowerCase (std::string &str)
 Converts a string to its lowercase variant.
 
void toUpperCase (std::string &str)
 Converts a string to its uppercase variant.
 
std::string trim (const std::string &s)
 Trims leading and trailing whitespaces from a string.
 
std::optional< jsontryParseJsonStr (const std::string &json_str)
 Attempt to parse a JSON string.
 
std::string generateTimeBasedId ()
 Generate a time base UUID.
 
std::string generateReadableTimeId ()
 Generates a readable time-based UUID.
 
std::string generateShortId (size_t length=12)
 Generates a short UUID.
 
std::vector< std::string > splitString (const std::string &input, const std::string &delimiter)
 Split given string based on given delimiter.
 
std::string getEnvOrDefault (const std::string &key, const std::string &defaultValue)
 Retrieves a value from an environment variable or a default value if the env variable was not set.
 
std::string bcryptBase64Encode (const unsigned char *data, size_t len)
 Encode a Salt string to bcrypt base64 format.
 
std::string generateSalt (int cost=12)
 Generates a salt to be used in hashing user passwords.
 
json hashPassword (const std::string &password)
 Digests user password + a generated salt to yield a hashed password.
 
json verifyPassword (const std::string &password, const std::string &stored_hash)
 Verifies user password if it matches the given hashed password.
 

Variables

 FieldType
 
const std::vector< std::string > authFields = {"id", "created", "updated", "name", "email", "password"}
 

Detailed Description

router.h

Router file for higher level route registration and removal *

Typedef Documentation

◆ json

typedef nlohmann::json mantis::json

Shorten JSON namespace.

◆ Log

using mantis::Log = typedef LoggingUnit

◆ LogLevel

Enum for the different logging levels.

◆ Method

using mantis::Method = typedef std::string

‍Syntactic sugar for request method which is a std::string

◆ Middleware

using mantis::Middleware = typedef std::function<bool(const Request&, Response&, Context&)>

‍Middleware shorthand for the function

◆ Path

using mantis::Path = typedef std::string

‍Syntactic sugar for request path which is a std::string

◆ Request

using mantis::Request = typedef httplib::Request

‍Shorthand for httplib::Request

◆ Response

using mantis::Response = typedef httplib::Response

‍Shorthand for httplib::Response

◆ RouteHandlerFunc

using mantis::RouteHandlerFunc = typedef std::function<void(const Request&, Response&, Context&)>

‍Route Handler function shorthand

◆ RouteKey

using mantis::RouteKey = typedef std::pair<Method, Path>

‍Shorthand notation for the request's method, path pair.

◆ Rule

typedef std::string mantis::Rule

◆ TableType

Enumeration Type Documentation

◆ DbType

enum class mantis::DbType
strong

Enum for which database is currently selected.

Enumerator
SQLITE 
PSQL 

‍SQLite Database

MYSQL 

‍PostGreSQL Database

‍MySQL Database

◆ LogLevel

enum class mantis::LogLevel : uint8_t
strong

Enum for the different logging levels.

Enumerator
TRACE 
DEBUG 

‍Trace logging level

INFO 

‍Debug Logging Level

WARN 

‍Info Logging Level

CRITICAL 

‍Warning Logging Level

‍Critical Logging Level

◆ TableType

enum class mantis::TableType
strong
Enumerator
Base 
Auth 
View 

Function Documentation

◆ bcryptBase64Encode()

std::string mantis::bcryptBase64Encode ( const unsigned char *  data,
size_t  len 
)

Encode a Salt string to bcrypt base64 format.

Parameters
datachar* string data to encode.
lenLength of the string data.
Returns
A base64 encoded salt value.

◆ createDirs()

bool mantis::createDirs ( const fs::path &  path)

Create directory, given a path.

Creates the target directory iteratively, including any missing parent directories. his ensures, any parent directory is set up before attempting to create a child directory.

Parameters
pathThe directory path to create like /foo/bar.
Returns
True if creation was successful. If the directory exists, it returns false.

◆ dirFromPath()

std::string mantis::dirFromPath ( const std::string &  path)

Returns a created/existing directory from a path.

Given a file path, it first gets the file parent directory and ensures the directory together with any missing parent directories are created first using the createDirs(...).

See also
createDirs() for creating directories.
Parameters
pathThe file path
Returns
Returns the directory path if successful, else an empty string.

◆ fieldExists()

bool mantis::fieldExists ( const TableType type,
const std::string &  fieldName 
)

◆ generateReadableTimeId()

std::string mantis::generateReadableTimeId ( )

Generates a readable time-based UUID.

The first segment is the current time in ISO-formatted time + milliseconds + short random suffix. It is human-readable and sortable, just like

Sample Output: 20250531T221944517N3J

Returns
A readable-time based UUID
See also
generateTimeBasedId() For a time-based UUID.
generateShortId() For a short UUID.

◆ generateSalt()

std::string mantis::generateSalt ( int  cost = 12)

Generates a salt to be used in hashing user passwords.

Parameters
costCost parameter.
Returns
A base64 encoded salt value.
See also
https://app.studyraid.com/en/read/12358/398958/understanding-the-cost-factor-parameter

◆ generateShortId()

std::string mantis::generateShortId ( size_t  length = 12)

Generates a short UUID.

Similar to what platforms like YouTube use for videos, but in our case, making use of only alphanumeric characters.

Sample Output: Fz8xYc6a7LQw

Returns
A short alphanumeric UUID
See also
generateTimeBasedId() For a time-based UUID.
generateReadableTimeId() For a readable time-based UUID.

◆ generateTimeBasedId()

std::string mantis::generateTimeBasedId ( )

Generate a time base UUID.

The first part is made up of milliseconds since epoch while the last 4 digits a random component. This makes it lexicographically sortable by time

Sample Output: 17171692041233276

Returns
A time based UUID
See also
generateReadableTimeId() For a readable time-based UUID.
generateShortId() For a short UUID.

◆ getEnvOrDefault()

std::string mantis::getEnvOrDefault ( const std::string &  key,
const std::string &  defaultValue 
)

Retrieves a value from an environment variable or a default value if the env variable was not set.

Parameters
keyEnvironment variable key.
defaultValueA default value if the key is not set.
Returns
The env value if found, else the default value passed in.

◆ getFieldType()

std::optional< mantis::FieldType > mantis::getFieldType ( const std::string &  fieldName)

◆ hashPassword()

json mantis::hashPassword ( const std::string &  password)

Digests user password + a generated salt to yield a hashed password.

Parameters
passwordPassword input to hash.
Returns
A JSON object having hash, salt and error values.

◆ isValidFieldType()

bool mantis::isValidFieldType ( const std::string &  fieldType)

◆ joinPaths()

fs::path mantis::joinPaths ( const std::string &  path1,
const std::string &  path2 
)

< Use shorthand fs to refer to the std::filesystem

Joins absolute path and a relative path, relative to the first path.

Parameters
path1The first absolute path or relative path
path2The relative path, subject to the first
Returns
An absolute path if successfully joined, else an empty path.

◆ NLOHMANN_JSON_SERIALIZE_ENUM() [1/2]

mantis::NLOHMANN_JSON_SERIALIZE_ENUM ( FieldType  ,
{ { FieldType::XML, "xml" }, { FieldType::STRING, "string" }, { FieldType::DOUBLE, "double" }, { FieldType::DATE, "date" }, { FieldType::INT8, "int8" }, { FieldType::UINT8, "uint8" }, { FieldType::INT16, "int16" }, { FieldType::UINT16, "uint16" }, { FieldType::INT32, "int32" }, { FieldType::UINT32, "uint32" }, { FieldType::INT64, "int64" }, { FieldType::UINT64, "uint64" }, { FieldType::BLOB, "blob" }, { FieldType::JSON, "json" }, { FieldType::BOOL, "bool" }, }   
) const

◆ NLOHMANN_JSON_SERIALIZE_ENUM() [2/2]

mantis::NLOHMANN_JSON_SERIALIZE_ENUM ( TableType  ,
{ {TableType::Base, "base"}, {TableType::Auth, "auth"}, {TableType::View, "view"} }   
)

◆ resolvePath()

fs::path mantis::resolvePath ( const std::string &  input_path)

Resolves given path as a string to an absolute path.

Given a relative path, relative to the cwd, we can resolve that path to the actual absolute path in our filesystem. This is needed for creating directories and files, especially for database and file-serving.

Parameters
input_pathThe path to resolve
Returns
Returns an absolute filesystem path.

◆ splitString()

std::vector< std::string > mantis::splitString ( const std::string &  input,
const std::string &  delimiter 
)

Split given string based on given delimiter.

Parameters
inputInput string to split.
delimiterThe string delimiter to use to split the input string.
Returns
A vector of strings.
auto parts = splitString("Hello, John!", ",");
std::cout << parts.size() << std::endl;
// > Should be a vector of two strings `Hello` and ` World`
std::vector< std::string > splitString(const std::string &input, const std::string &delimiter)
Split given string based on given delimiter.
Definition string_utils.cpp:100

◆ toLowerCase()

void mantis::toLowerCase ( std::string &  str)

Converts a string to its lowercase variant.

It converts the string in place.

Parameters
strThe string to convert.
See also
toUpperCase() To convert string to uppercase.

◆ toUpperCase()

void mantis::toUpperCase ( std::string &  str)

Converts a string to its uppercase variant.

It converts the string in place.

Parameters
strThe string to convert.
See also
toLowerCase() To convert string to lowercase.

◆ trim()

std::string mantis::trim ( const std::string &  s)

Trims leading and trailing whitespaces from a string.

Parameters
sThe string to trim.
Returns
String with all leading and trailing whitespaces removed.

◆ tryParseJsonStr()

std::optional< json > mantis::tryParseJsonStr ( const std::string &  json_str)

Attempt to parse a JSON string.

Parameters
json_strJSON string to parse
Returns
A JSON Object if successful, else a std::nullopt
auto user = tryParseJsonStr("{\"name\": \"John Doe\"}");
if(user.has_value()) {
// Do something ...
}
std::optional< json > tryParseJsonStr(const std::string &json_str)
Attempt to parse a JSON string.
Definition string_utils.cpp:9

◆ verifyPassword()

json mantis::verifyPassword ( const std::string &  password,
const std::string &  stored_hash 
)

Verifies user password if it matches the given hashed password.

Given a hashed password from the database (stored_hash), the method extracts the salt value, hashes the password value with the salt then compares the two hashes if they match.

Parameters
passwordUser password input.
stored_hashDatabase stored hashed user password.
Returns
JSON object indicating whether the verification was successful and an error value if any.

Variable Documentation

◆ authFields

const std::vector<std::string> mantis::authFields = {"id", "created", "updated", "name", "email", "password"}

◆ FieldType

mantis::FieldType