Backup data for future study
This commit is contained in:
parent
a2b0d13f55
commit
e785ca5448
34
main.cpp
34
main.cpp
@ -4,14 +4,19 @@
|
||||
#include "meal.hpp"
|
||||
#include "restaurants/restaurants.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <restbed>
|
||||
#include <sstream>
|
||||
#include <time.h>
|
||||
|
||||
std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants;
|
||||
|
||||
std::string file_info_name = "data_backup";
|
||||
struct tm last_save;
|
||||
|
||||
void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > session) {
|
||||
session->close(status_code, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } });
|
||||
}
|
||||
@ -23,7 +28,7 @@ void refresh( const std::shared_ptr< restbed::Session > session ) {
|
||||
sendResponse( "Refreshed menus!", restbed::OK, session );
|
||||
}
|
||||
|
||||
void get_all( const std::shared_ptr< restbed::Session > session ) {
|
||||
std::string get_all_json() {
|
||||
std::stringstream ss{};
|
||||
bool atleastonerest = false;
|
||||
ss << "[";
|
||||
@ -34,7 +39,28 @@ void get_all( const std::shared_ptr< restbed::Session > session ) {
|
||||
if(atleastonerest)
|
||||
ss.seekp(-1, ss.cur);
|
||||
ss << "]";
|
||||
sendResponse(ss.str(), restbed::OK, session);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void backupData() {
|
||||
time_t now = time(NULL);
|
||||
struct tm *local = localtime(&now);
|
||||
if(last_save.tm_year == local->tm_year && last_save.tm_mon == local->tm_mon && last_save.tm_mday == local->tm_mday)
|
||||
return;
|
||||
|
||||
std::ofstream output_file;
|
||||
output_file.open(file_info_name, std::ios_base::app);
|
||||
auto write_buffer = get_all_json();
|
||||
write_buffer += "\n";
|
||||
output_file.write(write_buffer.c_str(), write_buffer.size());
|
||||
|
||||
last_save.tm_year = local->tm_year;
|
||||
last_save.tm_mon = local->tm_mon;
|
||||
last_save.tm_mday = local->tm_mday;
|
||||
}
|
||||
|
||||
void get_all( const std::shared_ptr< restbed::Session > session ) {
|
||||
sendResponse(get_all_json(), restbed::OK, session);
|
||||
}
|
||||
|
||||
void get( const std::shared_ptr< restbed::Session > session ) {
|
||||
@ -106,6 +132,10 @@ int main(int argc, char **argv, char **env) {
|
||||
for(auto &restaurant : restaurants)
|
||||
restaurant.second->parse();
|
||||
std::cout << "Finished parsing" << std::endl;
|
||||
last_save.tm_year = 0;
|
||||
last_save.tm_mon = 0;
|
||||
last_save.tm_mday = 0;
|
||||
backupData();
|
||||
|
||||
restbed::Service service;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user