You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.1 KiB
132 lines
4.1 KiB
#include "AutoStore.h" |
|
#include "infrastructure/repositories/FileItemRepository.h" |
|
#include "infrastructure/adapters/SystemTimeProvider.h" |
|
#include "infrastructure/http/HttpOrderService.h" |
|
#include "infrastructure/auth/FileJwtAuthService.h" |
|
#include "webapi/controllers/StoreController.h" |
|
#include "webapi/controllers/AuthController.h" |
|
#include "infrastructure/http/HttpServer.h" |
|
#include "application/services/TaskScheduler.h" |
|
#include "application/commands/HandleExpiredItems.h" |
|
#include "infrastructure/adapters/SystemTimeProvider.h" |
|
#include "infrastructure/adapters/SystemThreadManager.h" |
|
#include "infrastructure/adapters/CvBlocker.h" |
|
#include <iostream> |
|
#include <filesystem> |
|
#include <memory> |
|
|
|
namespace nxl::autostore { |
|
|
|
using namespace infrastructure; |
|
using namespace application; |
|
|
|
AutoStore::AutoStore(Config config, ILoggerPtr logger) |
|
: config{std::move(config)}, log{std::move(logger)} |
|
{} |
|
|
|
AutoStore::~AutoStore() |
|
{ |
|
if (httpServer && httpServer->isRunning()) { |
|
stop(); |
|
} |
|
} |
|
|
|
bool AutoStore::initialize() |
|
{ |
|
try { |
|
std::filesystem::create_directories(config.dataPath); |
|
|
|
// Initialize repositories and services |
|
std::string itemsDbPath = |
|
std::filesystem::path(config.dataPath) / "items.json"; |
|
itemRepository = std::make_unique<FileItemRepository>(itemsDbPath); |
|
|
|
clock = std::make_unique<SystemTimeProvider>(); |
|
|
|
orderService = std::make_unique<HttpOrderService>(log); |
|
|
|
// Initialize auth service |
|
std::string usersDbPath = |
|
std::filesystem::path(config.dataPath) / "users.json"; |
|
authService = std::make_unique<FileJwtAuthService>(usersDbPath); |
|
|
|
// Initialize dependencies for task scheduler |
|
timeProvider = std::make_unique<SystemTimeProvider>(); |
|
threadManager = std::make_unique<SystemThreadManager>(); |
|
auto blocker = std::make_unique<CvBlocker>(); |
|
|
|
// Initialize task scheduler (for handling expired items) |
|
taskScheduler = std::make_unique<TaskScheduler>( |
|
log, *timeProvider, *threadManager, std::move(blocker)); |
|
|
|
// Initialize HTTP server |
|
httpServer = std::make_unique<HttpServer>(log, *authService); |
|
|
|
// Initialize store controller |
|
storeController = std::make_unique<webapi::StoreController>( |
|
webapi::StoreController::Context{ |
|
application::AddItem{*itemRepository, *clock, *orderService}, |
|
application::ListItems{*itemRepository}, |
|
application::GetItem{*itemRepository}, |
|
application::DeleteItem{*itemRepository}, *authService}); |
|
|
|
// Initialize auth controller |
|
authController = std::make_unique<webapi::AuthController>( |
|
webapi::AuthController::Context{application::LoginUser{*authService}}); |
|
|
|
log->info("Data path: %s", config.dataPath); |
|
log->info("AutoStore initialized successfully, handling expired items..."); |
|
|
|
return true; |
|
} catch (const std::exception& e) { |
|
log->error("Failed to initialize AutoStore: %s", e.what()); |
|
return false; |
|
} |
|
} |
|
|
|
bool AutoStore::start() |
|
{ |
|
log->info("Starting AutoStore services..."); |
|
|
|
try { |
|
taskScheduler->schedule( |
|
[this]() { |
|
application::HandleExpiredItems{*itemRepository, *clock, *orderService} |
|
.execute(); |
|
}, |
|
00, 00, 00, // midnight (00:00:00) |
|
TaskScheduler::RunMode::Forever | TaskScheduler::RunMode::OnStart); |
|
taskScheduler->start(); |
|
|
|
storeController->registerRoutes(httpServer->getServer()); |
|
authController->registerRoutes(httpServer->getServer()); |
|
|
|
if (!httpServer->start(config.port, config.host)) { |
|
log->error("Failed to start HTTP server"); |
|
return false; |
|
} |
|
|
|
log->info("AutoStore services started successfully"); |
|
log->info("HTTP server listening on http://%s:%d", config.host, |
|
config.port); |
|
log->info("API endpoint: POST http://%s:%d/api/v1", config.host, |
|
config.port); |
|
|
|
return true; |
|
} catch (const std::exception& e) { |
|
log->error("Failed to start AutoStore services: %s", e.what()); |
|
return false; |
|
} |
|
} |
|
|
|
void AutoStore::stop() |
|
{ |
|
log->info("Stopping AutoStore services..."); |
|
|
|
if (httpServer && httpServer->isRunning()) { |
|
httpServer->stop(); |
|
} |
|
log->info("AutoStore services stopped"); |
|
} |
|
|
|
} // namespace nxl::autostore
|