Yay
This commit is contained in:
@@ -4,14 +4,20 @@ project(dropshell_template_registry)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Find required packages
|
||||
find_package(Threads REQUIRED)
|
||||
find_package(SQLite3 REQUIRED)
|
||||
|
||||
# Find all source files in src directory
|
||||
file(GLOB_RECURSE SOURCES
|
||||
"src/*.cpp"
|
||||
"hash.cpp"
|
||||
)
|
||||
|
||||
# Find all header files in src directory
|
||||
file(GLOB_RECURSE HEADERS
|
||||
"src/*.hpp"
|
||||
"hash.hpp"
|
||||
)
|
||||
|
||||
# Add include directories
|
||||
@@ -25,16 +31,19 @@ add_executable(dropshell_template_registry ${SOURCES})
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(dropshell_template_registry
|
||||
pthread
|
||||
ssl
|
||||
crypto
|
||||
Threads::Threads # Use modern Threads target
|
||||
SQLite::SQLite3 # Use SQLite3 target
|
||||
# ssl and crypto might still be needed by httplib if used
|
||||
# ssl
|
||||
# crypto
|
||||
)
|
||||
|
||||
# Set compile options for static linking
|
||||
set_target_properties(dropshell_template_registry PROPERTIES
|
||||
LINK_SEARCH_START_STATIC ON
|
||||
LINK_SEARCH_END_STATIC ON
|
||||
)
|
||||
# Note: Static linking SQLite might require additional setup depending on the system
|
||||
# set_target_properties(dropshell_template_registry PROPERTIES
|
||||
# LINK_SEARCH_START_STATIC ON
|
||||
# LINK_SEARCH_END_STATIC ON
|
||||
# )
|
||||
|
||||
# Install target
|
||||
install(TARGETS dropshell_template_registry
|
||||
|
26
Dockerfile
26
Dockerfile
@@ -6,7 +6,7 @@ RUN apk add --no-cache \
|
||||
cmake \
|
||||
git \
|
||||
musl-dev \
|
||||
nlohmann-json-dev
|
||||
sqlite-dev
|
||||
|
||||
# Copy source code
|
||||
COPY . /src
|
||||
@@ -14,28 +14,24 @@ WORKDIR /src
|
||||
|
||||
# Build
|
||||
RUN mkdir build && cd build && \
|
||||
cmake .. && \
|
||||
cmake .. -DCMAKE_EXE_LINKER_FLAGS="-static" && \
|
||||
make -j$(nproc)
|
||||
|
||||
# Create final image
|
||||
FROM alpine:latest
|
||||
|
||||
# Install runtime dependencies
|
||||
RUN apk add --no-cache \
|
||||
libstdc++
|
||||
FROM scratch
|
||||
|
||||
# Copy binary from builder
|
||||
COPY --from=builder /src/build/dropshell_template_registry /usr/local/bin/
|
||||
COPY --from=builder /src/build/dropshell_template_registry /dropshell_template_registry
|
||||
|
||||
# Create data directory
|
||||
RUN mkdir -p /data
|
||||
VOLUME /data
|
||||
# Create data directory (though mounting is preferred)
|
||||
# RUN mkdir -p /data
|
||||
# VOLUME /data
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /data
|
||||
# Set working directory (optional for scratch)
|
||||
# WORKDIR /data
|
||||
|
||||
# Expose port
|
||||
EXPOSE 80
|
||||
|
||||
# Run server
|
||||
CMD ["dropshell_template_registry", "/data/config.json"]
|
||||
# Run server (assuming config is mounted at /data/config.json)
|
||||
ENTRYPOINT ["/dropshell_template_registry", "/data/config.json"]
|
4928
src/litecask.hpp
4928
src/litecask.hpp
File diff suppressed because it is too large
Load Diff
243
src/server.cpp
243
src/server.cpp
@@ -8,6 +8,8 @@
|
||||
#include <chrono> // For seeding random number generator
|
||||
#include <vector> // For getAllKeys
|
||||
#include <string_view> // For litecask values
|
||||
#include <sqlite3.h> // Include SQLite
|
||||
#include <stdexcept> // For std::runtime_error
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@@ -18,9 +20,11 @@ public:
|
||||
~ScopeFileDeleter() {
|
||||
if (!released_) {
|
||||
try {
|
||||
if (std::filesystem::exists(path_)) {
|
||||
std::filesystem::remove(path_);
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "Error deleting temp file: " << e.what() << std::endl;
|
||||
std::cerr << "Error deleting temp file: " << path_ << " - " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,8 +34,48 @@ private:
|
||||
bool released_;
|
||||
};
|
||||
|
||||
// Helper to execute SQL and check for errors
|
||||
void execute_sql(sqlite3* db, const char* sql, const std::string& error_msg_prefix) {
|
||||
char* err_msg = nullptr;
|
||||
int rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::string error_details = error_msg_prefix + ": " + (err_msg ? err_msg : "Unknown error");
|
||||
sqlite3_free(err_msg);
|
||||
throw std::runtime_error(error_details);
|
||||
}
|
||||
}
|
||||
|
||||
bool Server::init_db() {
|
||||
db_path_ = config_.object_store_path / "index.db";
|
||||
int rc = sqlite3_open(db_path_.c_str(), &db_);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "Failed to open/create SQLite database '" << db_path_ << "': " << sqlite3_errmsg(db_) << std::endl;
|
||||
db_ = nullptr; // Ensure db_ is null if open failed
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// Enable WAL mode for better concurrency
|
||||
execute_sql(db_, "PRAGMA journal_mode=WAL;", "Failed to set WAL mode");
|
||||
|
||||
// Create table if it doesn't exist
|
||||
const char* create_table_sql =
|
||||
"CREATE TABLE IF NOT EXISTS objects ("
|
||||
"label_tag TEXT PRIMARY KEY UNIQUE NOT NULL, "
|
||||
"hash TEXT NOT NULL);";
|
||||
execute_sql(db_, create_table_sql, "Failed to create objects table");
|
||||
|
||||
} catch (const std::runtime_error& e) {
|
||||
std::cerr << "Database initialization error: " << e.what() << std::endl;
|
||||
sqlite3_close(db_);
|
||||
db_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Server::Server(const ServerConfig& config)
|
||||
: config_(config), running_(false), _isInitialized(false) {
|
||||
: config_(config), running_(false), db_(nullptr) {
|
||||
// Ensure object store directory exists
|
||||
try {
|
||||
std::filesystem::create_directories(config_.object_store_path);
|
||||
@@ -40,33 +84,24 @@ Server::Server(const ServerConfig& config)
|
||||
return;
|
||||
}
|
||||
|
||||
// Set up and open the litecask datastore
|
||||
datastore_path_ = config_.object_store_path / "index";
|
||||
try {
|
||||
std::filesystem::create_directories(datastore_path_);
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "Failed to create datastore directory: " << datastore_path_ << " - " << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Open datastore
|
||||
if (datastore_.open(datastore_path_.string()) != litecask::Status::Ok) {
|
||||
std::cerr << "Failed to open litecask datastore at " << datastore_path_ << std::endl;
|
||||
} else {
|
||||
_isInitialized = true; // Mark as initialized if open succeeded
|
||||
// Initialize the database
|
||||
if (!init_db()) {
|
||||
// Error already printed in init_db
|
||||
// Consider throwing or setting an error state
|
||||
}
|
||||
}
|
||||
|
||||
Server::~Server() {
|
||||
stop();
|
||||
if (_isInitialized) {
|
||||
datastore_.close(); // Close the datastore only if initialized
|
||||
if (db_) {
|
||||
sqlite3_close(db_); // Close the database connection
|
||||
db_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool Server::start() {
|
||||
if (!_isInitialized) { // Check initialization flag
|
||||
std::cerr << "Datastore is not initialized. Cannot start server." << std::endl;
|
||||
if (!db_) { // Check if DB initialization failed
|
||||
std::cerr << "Database is not initialized. Cannot start server." << std::endl;
|
||||
return false;
|
||||
}
|
||||
setup_routes();
|
||||
@@ -90,9 +125,14 @@ void Server::stop() {
|
||||
}
|
||||
|
||||
void Server::setup_routes() {
|
||||
const std::string welcome_page = "<html><body><h1>Dropshell Template Registry</h1></body></html>";
|
||||
// Welcome page
|
||||
server_.Get("/index.html", [](const httplib::Request&, httplib::Response& res) {
|
||||
res.set_content("<html><body><h1>Dropshell Template Registry</h1></body></html>", "text/html");
|
||||
server_.Get("/", [welcome_page](const httplib::Request&, httplib::Response& res) {
|
||||
res.set_content(welcome_page, "text/html");
|
||||
});
|
||||
|
||||
server_.Get("/index.html", [welcome_page](const httplib::Request&, httplib::Response& res) {
|
||||
res.set_content(welcome_page, "text/html");
|
||||
});
|
||||
|
||||
// Get object by hash or label:tag
|
||||
@@ -106,9 +146,9 @@ void Server::setup_routes() {
|
||||
});
|
||||
|
||||
// Get directory listing
|
||||
// server_.Get("/dir", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
// handle_get_directory(req, res);
|
||||
// });
|
||||
server_.Get("/dir", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
handle_get_directory(req, res);
|
||||
});
|
||||
|
||||
// Upload object
|
||||
server_.Put("/([^/]+)/(.*)", [this](const httplib::Request& req, httplib::Response& res) { // Adjusted regex slightly for label:tag
|
||||
@@ -119,7 +159,6 @@ void Server::setup_routes() {
|
||||
void Server::handle_get_object(const httplib::Request& req, httplib::Response& res) {
|
||||
const auto& key = req.matches[1].str();
|
||||
std::string hash_str;
|
||||
lcVector<uint8_t> value_vec; // Use lcVector for litecask::get
|
||||
|
||||
// Check if the key looks like a hash (numeric)
|
||||
bool is_hash_lookup = true;
|
||||
@@ -131,30 +170,58 @@ void Server::handle_get_object(const httplib::Request& req, httplib::Response& r
|
||||
}
|
||||
|
||||
if (!is_hash_lookup) {
|
||||
// Lookup by label:tag in the datastore
|
||||
auto rc = datastore_.get(key.data(), key.size(), value_vec); // Use .data() and .size()
|
||||
if (rc == litecask::Status::Ok) {
|
||||
hash_str.assign(value_vec.begin(), value_vec.end()); // Convert vector to string
|
||||
} else if (rc == litecask::Status::EntryNotFound) {
|
||||
// Lookup by label:tag in the SQLite database
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
const char* sql = "SELECT hash FROM objects WHERE label_tag = ?;";
|
||||
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "Failed to prepare statement (get hash): " << sqlite3_errmsg(db_) << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Database error preparing statement", "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, key.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) {
|
||||
const unsigned char* text = sqlite3_column_text(stmt, 0);
|
||||
if (text) {
|
||||
hash_str = reinterpret_cast<const char*>(text);
|
||||
}
|
||||
} else if (rc == SQLITE_DONE) {
|
||||
// Not found
|
||||
sqlite3_finalize(stmt);
|
||||
res.status = 404;
|
||||
res.set_content("Object not found (label:tag)", "text/plain");
|
||||
return;
|
||||
} else {
|
||||
std::cerr << "Datastore get error: " << static_cast<int>(rc) << std::endl;
|
||||
std::cerr << "Failed to execute statement (get hash): " << sqlite3_errmsg(db_) << std::endl;
|
||||
sqlite3_finalize(stmt);
|
||||
res.status = 500;
|
||||
res.set_content("Datastore error on get", "text/plain");
|
||||
res.set_content("Database error executing statement", "text/plain");
|
||||
return;
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
} else {
|
||||
// Lookup directly by hash
|
||||
hash_str = key;
|
||||
}
|
||||
|
||||
if (hash_str.empty()) {
|
||||
// Should have been caught earlier if not found, but as a safeguard
|
||||
res.status = 404;
|
||||
res.set_content("Object hash could not be determined", "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
// Construct the file path using the hash string
|
||||
std::filesystem::path file_path = config_.object_store_path / hash_str;
|
||||
if (!std::filesystem::exists(file_path) || !std::filesystem::is_regular_file(file_path)) {
|
||||
res.status = 404;
|
||||
res.set_content("Object file not found", "text/plain");
|
||||
res.set_content("Object file not found for hash: " + hash_str, "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -165,22 +232,71 @@ void Server::handle_get_object(const httplib::Request& req, httplib::Response& r
|
||||
|
||||
void Server::handle_get_hash(const httplib::Request& req, httplib::Response& res) {
|
||||
const auto& label_tag = req.matches[1].str();
|
||||
lcVector<uint8_t> value_vec; // Use lcVector for litecask::get
|
||||
auto rc = datastore_.get(label_tag.data(), label_tag.size(), value_vec); // Use .data() and .size()
|
||||
|
||||
if (rc == litecask::Status::Ok) {
|
||||
res.set_content(std::string(value_vec.begin(), value_vec.end()), "text/plain"); // Convert vector to string
|
||||
} else if (rc == litecask::Status::EntryNotFound) {
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
const char* sql = "SELECT hash FROM objects WHERE label_tag = ?;";
|
||||
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "Failed to prepare statement (get hash direct): " << sqlite3_errmsg(db_) << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Database error preparing statement", "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, label_tag.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) {
|
||||
const unsigned char* text = sqlite3_column_text(stmt, 0);
|
||||
if (text) {
|
||||
res.set_content(reinterpret_cast<const char*>(text), "text/plain");
|
||||
}
|
||||
} else if (rc == SQLITE_DONE) {
|
||||
res.status = 404;
|
||||
res.set_content("Label:tag not found", "text/plain");
|
||||
} else {
|
||||
std::cerr << "Datastore get error: " << static_cast<int>(rc) << std::endl;
|
||||
std::cerr << "Failed to execute statement (get hash direct): " << sqlite3_errmsg(db_) << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Datastore error on get", "text/plain");
|
||||
res.set_content("Database error executing statement", "text/plain");
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
void Server::handle_get_directory(const httplib::Request& /*req*/, httplib::Response& res) {
|
||||
std::stringstream ss;
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
const char* sql = "SELECT label_tag, hash FROM objects;";
|
||||
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "Failed to prepare statement (get dir): " << sqlite3_errmsg(db_) << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Database error preparing statement", "text/plain");
|
||||
return;
|
||||
}
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
const unsigned char* label_tag_text = sqlite3_column_text(stmt, 0);
|
||||
const unsigned char* hash_text = sqlite3_column_text(stmt, 1);
|
||||
if (label_tag_text && hash_text) {
|
||||
ss << reinterpret_cast<const char*>(label_tag_text) << ","
|
||||
<< reinterpret_cast<const char*>(hash_text) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// handle_get_directory function removed
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "Failed to execute/iterate statement (get dir): " << sqlite3_errmsg(db_) << std::endl;
|
||||
// Don't overwrite potential results, but log error
|
||||
if (ss.str().empty()) { // Only send error if no data was retrieved
|
||||
res.status = 500;
|
||||
res.set_content("Database error executing statement", "text/plain");
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
res.set_content(ss.str(), "text/plain");
|
||||
}
|
||||
|
||||
void Server::handle_put_object(const httplib::Request& req, httplib::Response& res) {
|
||||
const auto& token = req.matches[1].str();
|
||||
@@ -226,7 +342,7 @@ void Server::handle_put_object(const httplib::Request& req, httplib::Response& r
|
||||
if (hash == 0) {
|
||||
res.status = 500;
|
||||
res.set_content("Failed to calculate hash", "text/plain");
|
||||
return; // Deleter will remove temp_file
|
||||
return;
|
||||
}
|
||||
|
||||
// Move file to final location
|
||||
@@ -235,31 +351,40 @@ void Server::handle_put_object(const httplib::Request& req, httplib::Response& r
|
||||
if (!std::filesystem::exists(final_path)) {
|
||||
try {
|
||||
std::filesystem::rename(temp_path, final_path);
|
||||
temp_file_deleter.release(); // Don't delete if rename succeeded
|
||||
temp_file_deleter.release();
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "Error renaming temp file: " << e.what() << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Failed to store object file", "text/plain");
|
||||
return; // Deleter will remove temp_file
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// File with the same hash already exists, no need to move
|
||||
// temp_file_deleter will automatically remove the temp file
|
||||
}
|
||||
|
||||
// Update datastore index
|
||||
auto rc = datastore_.put(label_tag.data(), label_tag.size(), hash_str.data(), hash_str.size()); // Use .data() and .size()
|
||||
if (rc != litecask::Status::Ok) {
|
||||
std::cerr << "Datastore put error: " << static_cast<int>(rc) << std::endl;
|
||||
// Update SQLite index (INSERT OR REPLACE)
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
const char* sql = "INSERT OR REPLACE INTO objects (label_tag, hash) VALUES (?, ?);";
|
||||
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
std::cerr << "Failed to prepare statement (put object): " << sqlite3_errmsg(db_) << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Failed to update datastore index", "text/plain");
|
||||
try {
|
||||
if (std::filesystem::exists(final_path) && !std::filesystem::remove(final_path)) {
|
||||
std::cerr << "Failed to remove object file after index failure: " << final_path << std::endl;
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "Error removing object file after index failure: " << e.what() << std::endl;
|
||||
res.set_content("Database error preparing statement", "text/plain");
|
||||
// Attempt to clean up the moved file if index fails
|
||||
try { if (std::filesystem::exists(final_path)) std::filesystem::remove(final_path); } catch(...) {};
|
||||
return;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, label_tag.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, hash_str.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "Failed to execute statement (put object): " << sqlite3_errmsg(db_) << std::endl;
|
||||
res.status = 500;
|
||||
res.set_content("Failed to update database index", "text/plain");
|
||||
// Attempt to clean up the moved file if index fails
|
||||
try { if (std::filesystem::exists(final_path)) std::filesystem::remove(final_path); } catch(...) {};
|
||||
return;
|
||||
}
|
||||
|
||||
|
@@ -3,12 +3,13 @@
|
||||
|
||||
#include "config.hpp"
|
||||
#include "httplib.hpp"
|
||||
#include "litecask.hpp"
|
||||
// #include "litecask.hpp" // Removed litecask
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <filesystem>
|
||||
#include <sqlite3.h> // Include SQLite header
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@@ -24,17 +25,20 @@ private:
|
||||
void setup_routes();
|
||||
void handle_get_object(const httplib::Request& req, httplib::Response& res);
|
||||
void handle_get_hash(const httplib::Request& req, httplib::Response& res);
|
||||
void handle_get_directory(const httplib::Request& req, httplib::Response& res);
|
||||
void handle_get_directory(const httplib::Request& req, httplib::Response& res); // Re-add directory handler
|
||||
void handle_put_object(const httplib::Request& req, httplib::Response& res);
|
||||
bool validate_write_token(const std::string& token) const;
|
||||
std::pair<std::string, std::string> parse_label_tag(const std::string& label_tag) const;
|
||||
|
||||
bool init_db(); // Helper for DB initialization
|
||||
|
||||
const ServerConfig& config_;
|
||||
httplib::Server server_;
|
||||
litecask::Datastore datastore_;
|
||||
std::filesystem::path datastore_path_;
|
||||
// Removed litecask members
|
||||
sqlite3* db_ = nullptr; // SQLite database connection
|
||||
std::filesystem::path db_path_;
|
||||
std::atomic<bool> running_;
|
||||
bool _isInitialized;
|
||||
// Removed _isInitialized - will rely on db_ pointer
|
||||
};
|
||||
|
||||
} // namespace dropshell
|
||||
|
Reference in New Issue
Block a user