Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into 7-create-breached-password-generator-script
  • Loading branch information
ni-jessica committed Oct 22, 2023
2 parents 19290cc + a7133cc commit e7eedf4
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 8 deletions.
15 changes: 13 additions & 2 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ cmake_minimum_required(VERSION 3.27)
project(backend)

find_package(Crow REQUIRED)
find_package(SQLite3 REQUIRED)

add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} Crow::Crow)
set(SOURCES
src/main.cpp
src/database.cpp
)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(${PROJECT_NAME} ${SOURCES})

target_link_libraries(${PROJECT_NAME} Crow::Crow SQLite::SQLite3)

target_include_directories(${PROJECT_NAME} PRIVATE src)
8 changes: 8 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ From `/backend`, start the server:
```bash
cd build && ./backend
```

To fix VS Code import errors with Crow, try adding the following line to your `settings.json`:

```json
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
```

[Source](https://stackoverflow.com/questions/58077908/linking-conan-include-to-vs-code)
1 change: 1 addition & 0 deletions backend/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BackendRecipe(ConanFile):

def requirements(self):
self.requires("crowcpp-crow/1.0+5")
self.requires("sqlite3/3.42.0")

def build_requirements(self):
self.tool_requires("cmake/3.22.6")
76 changes: 76 additions & 0 deletions backend/src/database.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <filesystem>
#include <sqlite3.h>
#include "database.hpp"

namespace
{
// https://stackoverflow.com/questions/31146713/sqlite3-exec-callback-function-clarification
static int printRow(void *unused, int width, char **data, char **attributes)
{
for (int i = 0; i < width; i++)
{
printf("%s = %s\n", attributes[i], data[i] ? data[i] : "NULL");
}
printf("\n");
return 0;
}
}

namespace database
{
Database::Database(const std::string &file_path) : is_closed(false)
{
if (std::filesystem::exists(file_path))
{
std::remove(file_path.c_str());
}

int result = sqlite3_open_v2(file_path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

void Database::execute(const std::string &command)
{
int result = sqlite3_exec(db, command.c_str(), NULL, NULL, NULL);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

void Database::printTable(const std::string &table)
{
const std::string query = "SELECT * FROM " + table + ";";
int result = sqlite3_exec(db, query.c_str(), printRow, NULL, NULL);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

void Database::close()
{
sqlite3_close(db);
is_closed = true;
}

Database::~Database()
{
if (!is_closed)
{
int result = sqlite3_close(db);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
std::abort();
}
}
}
55 changes: 55 additions & 0 deletions backend/src/database.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <sqlite3.h>

#ifndef DATABASE_H
#define DATABASE_H

namespace database
{
/**
* @brief Class containing a SQLite database connection
*
* @warning Rule of Three/Five not currently defined for this class.
*/
class Database
{
public:
/**
* @brief Construct a new Database object
*
* @warning If a file already exists at the provided path, it is replaced.
* @param file_path The path for the .db file.
*/
Database(const std::string &file_path);

/**
* @brief Execute the provided SQL command.
*
* @param command The command to be executed.
*/
void execute(const std::string &command);

/**
* @brief Print the rows of the provided table.
*
* @param table The table to print
*/
void printTable(const std::string &table);

/**
* @brief Close the connection with the SQLite database.
*
*/
void close();

/**
* @brief Destroy the Database object
*
*/
~Database();

private:
sqlite3 *db;
bool is_closed;
};
}
#endif // DATABASE_H
22 changes: 16 additions & 6 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
#include <string.h>

#include "crow.h"
#include <sqlite3.h>
#include "database.hpp"

int main(void) {
// delcare crow application
crow::SimpleApp app;
int main()
{
database::Database db = database::Database("passwords.db");
db.execute("CREATE TABLE passwords (password TEXT);");
db.execute("INSERT INTO passwords (password) VALUES ('chocolate1');");
db.printTable("passwords");

// declare crow application
crow::SimpleApp app;

// define endpoint at the root directory
CROW_ROUTE(app, "/")([](){
CROW_ROUTE(app, "/")
([]()
{
crow::json::wvalue response;
response["status"] = "success";
return response;
});
return response; });

// set the port, set the app to run on multiple threads, and run the app
app.port(18080).multithreaded().run();
db.close();
}

0 comments on commit e7eedf4

Please sign in to comment.