added CORS
This commit is contained in:
4
source/database/database.cpp
Normal file
4
source/database/database.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
#include "databasepool.h"
|
||||
|
||||
std::unique_ptr<DatabasePool> dbpool = nullptr;
|
||||
|
||||
36
source/database/database.hpp
Normal file
36
source/database/database.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef __DATABASE_H__
|
||||
#define __DATABASE_H__
|
||||
|
||||
#include <string>
|
||||
#include "sqlite_orm.h"
|
||||
#include "ShadowrunDb.hpp"
|
||||
#include "loginDb.hpp"
|
||||
|
||||
inline auto make_database(const std::string& path) {
|
||||
auto storage = sqlite_orm::make_storage(path,
|
||||
sqlite_orm::make_table("users",
|
||||
sqlite_orm::make_column("id", &login::User::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("username", &login::User::username, sqlite_orm::unique() ),
|
||||
sqlite_orm::make_column("salt", &login::User::salt, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("password_hash", &login::User::password_hash, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("created_at", &login::User::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP"))
|
||||
),
|
||||
sqlite_orm::make_table("shadowrun_characters",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunCharacter::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("name", &shadowrun::ShadowrunCharacter::name, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunCharacter::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP"))
|
||||
),
|
||||
sqlite_orm::make_table("shadowrun_characters_data",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunCharacterData::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("character_id", &shadowrun::ShadowrunCharacterData::character_id, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("type", &shadowrun::ShadowrunCharacterData::type, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("json", &shadowrun::ShadowrunCharacterData::json),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunCharacterData::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::make_column("updated_at", &shadowrun::ShadowrunCharacterData::updated_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::foreign_key(&shadowrun::ShadowrunCharacterData::character_id).references(&shadowrun::ShadowrunCharacter::id).on_delete.cascade()
|
||||
));
|
||||
|
||||
return storage;
|
||||
}
|
||||
|
||||
#endif // __DATABASE_H__
|
||||
61
source/database/databasepool.h
Normal file
61
source/database/databasepool.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include "database.hpp"
|
||||
|
||||
class DatabasePool {
|
||||
public:
|
||||
// Construct pool with path + size
|
||||
DatabasePool(const std::string& path)
|
||||
: db_path(path)
|
||||
{
|
||||
// Enable multithreaded SQLite
|
||||
assert(sqlite3_config(SQLITE_CONFIG_MULTITHREAD) == SQLITE_OK);
|
||||
assert(sqlite3_initialize() == SQLITE_OK);
|
||||
}
|
||||
|
||||
bool init(size_t size){
|
||||
try{
|
||||
// Pre-create the pool
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
auto db = std::make_shared<decltype(make_database(db_path))>(make_database(db_path));
|
||||
db->sync_schema();
|
||||
pool.push(db);
|
||||
}
|
||||
// init the database
|
||||
auto db = acquire();
|
||||
release(db);
|
||||
return true;
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Acquire a database connection from the pool
|
||||
std::shared_ptr<decltype(make_database(std::string{}))> acquire() {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
cv.wait(lock, [&]{ return !pool.empty(); });
|
||||
auto db = pool.front();
|
||||
pool.pop();
|
||||
return db;
|
||||
}
|
||||
|
||||
// Return a connection to the pool
|
||||
void release(std::shared_ptr<decltype(make_database(std::string{}))> db) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
pool.push(db);
|
||||
lock.unlock();
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
std::queue<std::shared_ptr<decltype(make_database(std::string{}))>> pool;
|
||||
std::mutex mutex;
|
||||
std::condition_variable cv;
|
||||
const std::string db_path;
|
||||
};
|
||||
|
||||
extern std::unique_ptr<DatabasePool> dbpool;
|
||||
Reference in New Issue
Block a user