MantisBase v0.4.9
Loading...
Searching...
No Matches
mb::MantisBase Class Reference

MantisBase entry point. More...

#include <mantisbase.h>

Public Member Functions

 ~MantisBase ()
 MantisBase (const MantisBase &)=delete
MantisBase & operator= (const MantisBase &)=delete
 MantisBase (MantisBase &&)=delete
MantisBase & operator= (MantisBase &&)=delete
int run ()
 Start the http server and start listening for requests.
void close ()
 Close the application and reset object instances that are dependent on the class.
int quit (const int &exitCode=0, const std::string &reason="Something went wrong!")
 Quit the running application immediately.
int port () const
 Retrieve HTTP Listening port.
void setPort (const int &port)
 Set a new port value for HTTP server.
int poolSize () const
 Retrieve the database pool size value.
void setPoolSize (const int &pool_size)
std::string host () const
 Retrieve HTTP Server host address. For instance, a host of 127.0.0.1, 0.0.0.0, etc.
void setHost (const std::string &host)
 Update HTTP Server host address.
std::string publicDir () const
 Retrieve the public static file directory.
void setPublicDir (const std::string &dir)
 Update HTTP server static file directory.
std::string dataDir () const
 Retrieves the data directory where SQLite db and files are stored.
void setDataDir (const std::string &dir)
 Update the data directory for MantisApp.
std::string scriptsDir () const
 Retrieves the scripts directory where JavaScript files are stored used for extending functionality in Mantis.
void setScriptsDir (const std::string &dir)
 Update the scripts directory for MantisApp.
std::string templatesDir () const
 Retrieves the templates directory where email template files are stored used for sending confirmation/password reset emails.
void setTemplatesDir (const std::string &dir)
 Update the templates directory for MantisBase.
std::string migrationsDir () const
 Retrieves the migrations directory path.
void setMigrationsDir (const std::string &dir)
 Update the migrations directory path.
bool skipAdminSetup () const
void setSkipAdminSetup (bool skip)
std::string dbType () const
 Retrieves the active database type.
void setDbType (const std::string &dbType)
 Update the active database type for Mantis.
std::string jwtSecretKey () const
 Retrieve the JWT secret key.
std::string snowflakeId () const
MbDatabase & db () const
 Get the database unit object.
argparse::ArgumentParser & cmd () const
MbRouter & router () const
MbKVStore & settings () const
MbLogger & logs () const
MbRealtimeDB & rt () const
MbFilesMgr & files () const
MbLogger & logger () const
MbAuth & auth () const
MbMailer & mailer () const
MbEntity entity (const std::string &entity_name) const
 Fetch a table schema encapsulated by an MbEntity object from given the table name. If table does not exist yet, return an emty object.
bool hasEntity (const std::string &entity_name) const
 Check if table schema encapsulated by an MbEntity object from given the table name exists. If table does not exist yet, return false.
void openBrowserOnStart () const
 Get the duktape context.
std::chrono::time_point< std::chrono::steady_clock > startTime () const
 Get the server start time in std::chrono::
bool isDevMode () const
bool isScriptingDisabled () const

Static Public Member Functions

static std::unique_ptr< MantisBase > create (int argc, char **argv)
 Create class instance given cmd args passed in.
static std::unique_ptr< MantisBase > create (const json &config=json::object())
 Convenience function to allow creating class instance given the values needed to set up the app without any need for passing in cmd args.
static std::string appVersion ()
static int appMajorVersion ()
static int appMinorVersion ()
static int appPatchVersion ()

Detailed Description

MantisBase entry point.

This class handles the entrypoint to the Mantis world, where we can set/get application flags and variables, as well as access other application units. These units are:

  • DatabaseMgr: For all database handling,
    See also
    DatabaseMgr for more information.
  • HttpMgr: Low-level http server operation and routing.
    See also
    HttpMgr for low-level access or
    RouterMgr for a high level routing methods.
  • LogsMgr: For logging capabilities,
    See also
    LoggingMgr for more details.
  • RouterMgr: High level routing wrapper on top of
    See also
    HttpMgr,
    RouterMgr for more details.
  • ValidatorMgr: A validation store using regex,
    See also
    Validator for more details.
    Note
    create() returns a std::unique_ptr<MantisBase> that you own. Pass a const MantisBase& (or capture it in lambdas) to reach services such as auth(), entity(), and db(). Types that participate in request handling (MbRequest, MbEntity, MbRouter, MbApiKeyMgr, etc.) inherit MbAppMixin and expose mbApp() for the same access. There is no global instance() accessor.

Constructor & Destructor Documentation

◆ ~MantisBase()

mb::MantisBase::~MantisBase ( )

◆ MantisBase() [1/2]

mb::MantisBase::MantisBase ( const MantisBase & )
delete

◆ MantisBase() [2/2]

mb::MantisBase::MantisBase ( MantisBase && )
delete

Member Function Documentation

◆ appMajorVersion()

int mb::MantisBase::appMajorVersion ( )
static
Returns
Major version component from appVersion().

◆ appMinorVersion()

int mb::MantisBase::appMinorVersion ( )
static
Returns
Minor version component from appVersion().

◆ appPatchVersion()

int mb::MantisBase::appPatchVersion ( )
static
Returns
Patch version component from appVersion().

◆ appVersion()

std::string mb::MantisBase::appVersion ( )
static

Fetch the application version

Returns
Application version

◆ auth()

MbAuth & mb::MantisBase::auth ( ) const
nodiscard
Returns
JWT, API key, and OAuth services.

◆ close()

void mb::MantisBase::close ( )

Close the application and reset object instances that are dependent on the class.

Internally, this stops running http server, disconnects from the database and does any required cleanup

◆ cmd()

argparse::ArgumentParser & mb::MantisBase::cmd ( ) const
nodiscard
Returns
Command-line argument parser populated during create.

◆ create() [1/2]

std::unique_ptr< MantisBase > mb::MantisBase::create ( const json & config = json::object())
static

Convenience function to allow creating class instance given the values needed to set up the app without any need for passing in cmd args.

The expected values are: { "db": "<db type>", "db_url": "<connection string>", "data-dir": "<path to dir>", "public-dir": "<path to dir>", "scripts-dir": "<path to dir>", "migrations-dir": "<path to dir>", "dev": true, "serve": { "port": <int value>, "host": "<host IP/addr>", "pool-size": <int value>, "skip-admin-setup": <boolean true/false> }, "admins": { "add": ["<email>", "<password>"], "ls": true, "rm": "<email or id>" }, "migrations": { "up": true, "down": false }, "schema": { "ls": true, "rm": "<entity>", "add": "<json>", "update": { "entity": "<name>", "body": "<json>" } } }

Note
All primary options above are optional, you can omit to use default values.
admins subcommand expects a subcommand with either the add or rm.
serve command can have an empty json object and the app will configure with defaults.
// Defaults only:
auto app = MantisBase::create(json::object());
return app->run();
// With configuration:
json cfg;
cfg["db"] = "postgresql";
cfg["db_url"] = "dbname=mantis user=postgres password=12342532";
cfg["dev"] = true;
cfg["serve"] = json::object();
auto app = MantisBase::create(cfg);
auto token = app->auth().createToken({{"id", "u1"}, {"table", "users"}});
return app->run();
static std::unique_ptr< MantisBase > create(int argc, char **argv)
Create class instance given cmd args passed in.
Definition mantisbase.cpp:75
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
Parameters
configJSON Object bearing the cmd args values to be used
Returns
Owned application instance

◆ create() [2/2]

std::unique_ptr< MantisBase > mb::MantisBase::create ( int argc,
char ** argv )
static

Create class instance given cmd args passed in.

See also
parseArgs() for expected cmd args to be passed in.
Parameters
argcNumber of cmd args
argvChar array list
Returns
Owned application instance

◆ dataDir()

std::string mb::MantisBase::dataDir ( ) const
nodiscard

Retrieves the data directory where SQLite db and files are stored.

Returns
MantisApp data directory.

◆ db()

MbDatabase & mb::MantisBase::db ( ) const
nodiscard

Get the database unit object.

◆ dbType()

std::string mb::MantisBase::dbType ( ) const
nodiscard

Retrieves the active database type.

Returns
Selected DatabaseType enum value.

◆ entity()

MbEntity mb::MantisBase::entity ( const std::string & entity_name) const
nodiscard

Fetch a table schema encapsulated by an MbEntity object from given the table name. If table does not exist yet, return an emty object.

Parameters
entity_nameName of the table of interest
Returns
MbEntity object for the selected table

◆ files()

MbFilesMgr & mb::MantisBase::files ( ) const
nodiscard
Returns
File asset manager for entity upload fields.

◆ hasEntity()

bool mb::MantisBase::hasEntity ( const std::string & entity_name) const
nodiscard

Check if table schema encapsulated by an MbEntity object from given the table name exists. If table does not exist yet, return false.

Parameters
entity_nameName of the table of interest
Returns
true if entity exists, false otherwise.

◆ host()

std::string mb::MantisBase::host ( ) const
nodiscard

Retrieve HTTP Server host address. For instance, a host of 127.0.0.1, 0.0.0.0, etc.

Returns
HTTP Server Host address.

◆ isDevMode()

bool mb::MantisBase::isDevMode ( ) const
nodiscard
Returns
true when the app was started with dev-mode flags enabled.

◆ isScriptingDisabled()

bool mb::MantisBase::isScriptingDisabled ( ) const
nodiscard
Returns
true when scripting was disabled via CLI or environment.

◆ jwtSecretKey()

std::string mb::MantisBase::jwtSecretKey ( ) const
nodiscard

Retrieve the JWT secret key.

Returns
JWT Secret value.

◆ logger()

MbLogger & mb::MantisBase::logger ( ) const
nodiscard
Returns
Structured application logger.

◆ logs()

MbLogger & mb::MantisBase::logs ( ) const
nodiscard
Returns
MbLogger (alias of logger).

◆ mailer()

MbMailer & mb::MantisBase::mailer ( ) const
nodiscard
Returns
Mailer service.

◆ migrationsDir()

std::string mb::MantisBase::migrationsDir ( ) const
nodiscard

Retrieves the migrations directory path.

Returns
Migrations directory.

◆ openBrowserOnStart()

void mb::MantisBase::openBrowserOnStart ( ) const

Get the duktape context.

Launch browser with the admin dashboard page. If all goes well, the default OS browser should open (if not opened) with the admin dashboard URL.

Added in v0.1.6

◆ operator=() [1/2]

MantisBase & mb::MantisBase::operator= ( const MantisBase & )
delete

◆ operator=() [2/2]

MantisBase & mb::MantisBase::operator= ( MantisBase && )
delete

◆ poolSize()

int mb::MantisBase::poolSize ( ) const
nodiscard

Retrieve the database pool size value.

Returns
SOCI's database pool size.

◆ port()

int mb::MantisBase::port ( ) const
nodiscard

Retrieve HTTP Listening port.

Returns
Http Listening Port.

◆ publicDir()

std::string mb::MantisBase::publicDir ( ) const
nodiscard

Retrieve the public static file directory.

Returns
MantisApp public directory.

◆ quit()

int mb::MantisBase::quit ( const int & exitCode = 0,
const std::string & reason = "Something went wrong!" )

Quit the running application immediately.

Parameters
exitCodeExit code value
reasonUser-friendly reason for the exit.
Returns
exitCode value.

◆ router()

MbRouter & mb::MantisBase::router ( ) const
nodiscard
Returns
HTTP router (route registration, schema cache, SSE manager).

◆ rt()

MbRealtimeDB & mb::MantisBase::rt ( ) const
nodiscard
Returns
Realtime change-detection worker (SQLite/PostgreSQL → SSE/WS).

◆ run()

int mb::MantisBase::run ( )
nodiscard

Start the http server and start listening for requests.

Returns
0 if execution was okay, else a non-zero value.

◆ scriptsDir()

std::string mb::MantisBase::scriptsDir ( ) const
nodiscard

Retrieves the scripts directory where JavaScript files are stored used for extending functionality in Mantis.

Returns
MantisApp scripts directory.

◆ setDataDir()

void mb::MantisBase::setDataDir ( const std::string & dir)

Update the data directory for MantisApp.

Parameters
dirNew data directory.

◆ setDbType()

void mb::MantisBase::setDbType ( const std::string & dbType)

Update the active database type for Mantis.

Parameters
dbTypeNew database type (sqlite3 or postgresql).

◆ setHost()

void mb::MantisBase::setHost ( const std::string & host)

Update HTTP Server host address.

Parameters
hostNew HTTP Server host address.

◆ setMigrationsDir()

void mb::MantisBase::setMigrationsDir ( const std::string & dir)

Update the migrations directory path.

Parameters
dirNew migrations directory.

◆ setPoolSize()

void mb::MantisBase::setPoolSize ( const int & pool_size)

◆ setPort()

void mb::MantisBase::setPort ( const int & port)

Set a new port value for HTTP server.

Parameters
portNew HTTP port value.

◆ setPublicDir()

void mb::MantisBase::setPublicDir ( const std::string & dir)

Update HTTP server static file directory.

Parameters
dirNew directory path.

◆ setScriptsDir()

void mb::MantisBase::setScriptsDir ( const std::string & dir)

Update the scripts directory for MantisApp.

Parameters
dirNew scripts directory.

◆ setSkipAdminSetup()

void mb::MantisBase::setSkipAdminSetup ( bool skip)

Enable or disable first-boot admin setup on serve.

◆ setTemplatesDir()

void mb::MantisBase::setTemplatesDir ( const std::string & dir)

Update the templates directory for MantisBase.

Parameters
dirNew templates directory.

◆ settings()

MbKVStore & mb::MantisBase::settings ( ) const
nodiscard
Returns
Application settings key-value store (/api/v1/sys/settings).

◆ skipAdminSetup()

bool mb::MantisBase::skipAdminSetup ( ) const
nodiscard

Whether first-boot admin setup (browser) should be skipped on serve.

◆ snowflakeId()

std::string mb::MantisBase::snowflakeId ( ) const
Returns
Next Snowflake ID string for records and sessions.

◆ startTime()

std::chrono::time_point< std::chrono::steady_clock > mb::MantisBase::startTime ( ) const
nodiscard

Get the server start time in std::chrono::

Returns
Server start time

◆ templatesDir()

std::string mb::MantisBase::templatesDir ( ) const
nodiscard

Retrieves the templates directory where email template files are stored used for sending confirmation/password reset emails.

Returns
MantisApp scripts directory.

The documentation for this class was generated from the following files: