refactor(main): move file operation functions to new source (#2124)

This commit is contained in:
ReenigneArcher 2024-02-10 07:37:27 -05:00 committed by GitHub
commit cd2153f340
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 113 additions and 78 deletions

View file

@ -45,6 +45,8 @@ set(SUNSHINE_TARGET_FILES
"${CMAKE_SOURCE_DIR}/src/uuid.h"
"${CMAKE_SOURCE_DIR}/src/config.h"
"${CMAKE_SOURCE_DIR}/src/config.cpp"
"${CMAKE_SOURCE_DIR}/src/file_handler.cpp"
"${CMAKE_SOURCE_DIR}/src/file_handler.h"
"${CMAKE_SOURCE_DIR}/src/logging.cpp"
"${CMAKE_SOURCE_DIR}/src/logging.h"
"${CMAKE_SOURCE_DIR}/src/main.cpp"

View file

@ -0,0 +1,5 @@
file_handler
============
.. doxygenfile:: file_handler.h
:allow-dot-graphs:

View file

@ -15,6 +15,7 @@
#include <boost/property_tree/ptree.hpp>
#include "config.h"
#include "file_handler.h"
#include "logging.h"
#include "main.h"
#include "nvhttp.h"
@ -1215,7 +1216,7 @@ namespace config {
}
// Read config file
auto vars = parse_config(read_file(sunshine.config_file.c_str()));
auto vars = parse_config(file_handler::read_file(sunshine.config_file.c_str()));
for (auto &[name, value] : cmd_vars) {
vars.insert_or_assign(std::move(name), std::move(value));

View file

@ -29,6 +29,7 @@
#include "config.h"
#include "confighttp.h"
#include "crypto.h"
#include "file_handler.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
@ -162,7 +163,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "index.html");
std::string content = file_handler::read_file(WEB_DIR "index.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -174,7 +175,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "pin.html");
std::string content = file_handler::read_file(WEB_DIR "pin.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -186,7 +187,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "apps.html");
std::string content = file_handler::read_file(WEB_DIR "apps.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
headers.emplace("Access-Control-Allow-Origin", "https://images.igdb.com/");
@ -199,7 +200,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "clients.html");
std::string content = file_handler::read_file(WEB_DIR "clients.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -211,7 +212,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "config.html");
std::string content = file_handler::read_file(WEB_DIR "config.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -223,7 +224,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "password.html");
std::string content = file_handler::read_file(WEB_DIR "password.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -236,7 +237,7 @@ namespace confighttp {
send_redirect(response, request, "/");
return;
}
std::string content = read_file(WEB_DIR "welcome.html");
std::string content = file_handler::read_file(WEB_DIR "welcome.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -248,7 +249,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(WEB_DIR "troubleshooting.html");
std::string content = file_handler::read_file(WEB_DIR "troubleshooting.html");
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/html; charset=utf-8");
response->write(content, headers);
@ -324,7 +325,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(config::stream.file_apps.c_str());
std::string content = file_handler::read_file(config::stream.file_apps.c_str());
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "application/json");
response->write(content, headers);
@ -336,7 +337,7 @@ namespace confighttp {
print_req(request);
std::string content = read_file(config::sunshine.log_file.c_str());
std::string content = file_handler::read_file(config::sunshine.log_file.c_str());
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "text/plain");
response->write(SimpleWeb::StatusCode::success_ok, content, headers);
@ -542,7 +543,7 @@ namespace confighttp {
outputTree.put("platform", SUNSHINE_PLATFORM);
outputTree.put("version", PROJECT_VER);
auto vars = config::parse_config(read_file(config::sunshine.config_file.c_str()));
auto vars = config::parse_config(file_handler::read_file(config::sunshine.config_file.c_str()));
for (auto &[name, value] : vars) {
outputTree.put(std::move(name), std::move(value));
@ -575,7 +576,7 @@ namespace confighttp {
configStream << kv.first << " = " << value << std::endl;
}
write_file(config::sunshine.config_file.c_str(), configStream.str());
file_handler::write_file(config::sunshine.config_file.c_str(), configStream.str());
}
catch (std::exception &e) {
BOOST_LOG(warning) << "SaveConfig: "sv << e.what();

69
src/file_handler.cpp Normal file
View file

@ -0,0 +1,69 @@
/**
* @file file_handler.cpp
* @brief File handling functions.
*/
// standard includes
#include <filesystem>
#include <fstream>
// local includes
#include "file_handler.h"
#include "logging.h"
namespace file_handler {
/**
* @brief Read a file to string.
* @param path The path of the file.
* @return `std::string` : The contents of the file.
*
* EXAMPLES:
* ```cpp
* std::string contents = read_file("path/to/file");
* ```
*/
std::string
read_file(const char *path) {
if (!std::filesystem::exists(path)) {
BOOST_LOG(debug) << "Missing file: " << path;
return {};
}
std::ifstream in(path);
std::string input;
std::string base64_cert;
while (!in.eof()) {
std::getline(in, input);
base64_cert += input + '\n';
}
return base64_cert;
}
/**
* @brief Writes a file.
* @param path The path of the file.
* @param contents The contents to write.
* @return `int` : `0` on success, `-1` on failure.
*
* EXAMPLES:
* ```cpp
* int write_status = write_file("path/to/file", "file contents");
* ```
*/
int
write_file(const char *path, const std::string_view &contents) {
std::ofstream out(path);
if (!out.is_open()) {
return -1;
}
out << contents;
return 0;
}
} // namespace file_handler

14
src/file_handler.h Normal file
View file

@ -0,0 +1,14 @@
/**
* @file file_handler.h
* @brief Header file for file handling functions.
*/
#pragma once
#include <string>
namespace file_handler {
std::string
read_file(const char *path);
int
write_file(const char *path, const std::string_view &contents);
} // namespace file_handler

View file

@ -21,9 +21,9 @@
#include "config.h"
#include "crypto.h"
#include "file_handler.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h"
#include "platform/common.h"
@ -161,12 +161,12 @@ namespace http {
return -1;
}
if (write_file(pkey.c_str(), creds.pkey)) {
if (file_handler::write_file(pkey.c_str(), creds.pkey)) {
BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.pkey << ']';
return -1;
}
if (write_file(cert.c_str(), creds.x509)) {
if (file_handler::write_file(cert.c_str(), creds.x509)) {
BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.cert << ']';
return -1;
}

View file

@ -758,57 +758,3 @@ main(int argc, char *argv[]) {
return lifetime::desired_exit_code;
}
/**
* @brief Read a file to string.
* @param path The path of the file.
* @return `std::string` : The contents of the file.
*
* EXAMPLES:
* ```cpp
* std::string contents = read_file("path/to/file");
* ```
*/
std::string
read_file(const char *path) {
if (!std::filesystem::exists(path)) {
BOOST_LOG(debug) << "Missing file: " << path;
return {};
}
std::ifstream in(path);
std::string input;
std::string base64_cert;
while (!in.eof()) {
std::getline(in, input);
base64_cert += input + '\n';
}
return base64_cert;
}
/**
* @brief Writes a file.
* @param path The path of the file.
* @param contents The contents to write.
* @return `int` : `0` on success, `-1` on failure.
*
* EXAMPLES:
* ```cpp
* int write_status = write_file("path/to/file", "file contents");
* ```
*/
int
write_file(const char *path, const std::string_view &contents) {
std::ofstream out(path);
if (!out.is_open()) {
return -1;
}
out << contents;
return 0;
}

View file

@ -26,10 +26,6 @@ extern bool display_cursor;
// functions
int
main(int argc, char *argv[]);
std::string
read_file(const char *path);
int
write_file(const char *path, const std::string_view &contents);
void
launch_ui();
void

View file

@ -22,6 +22,7 @@
// local includes
#include "config.h"
#include "crypto.h"
#include "file_handler.h"
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
@ -1005,8 +1006,8 @@ namespace nvhttp {
load_state();
}
conf_intern.pkey = read_file(config::nvhttp.pkey.c_str());
conf_intern.servercert = read_file(config::nvhttp.cert.c_str());
conf_intern.pkey = file_handler::read_file(config::nvhttp.pkey.c_str());
conf_intern.servercert = file_handler::read_file(config::nvhttp.cert.c_str());
crypto::cert_chain_t cert_chain;
for (auto &[_, client] : map_id_client) {

View file

@ -3,8 +3,8 @@
* @brief todo
*/
#include "graphics.h"
#include "src/file_handler.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/video.h"
#include <fcntl.h>
@ -780,7 +780,7 @@ namespace egl {
for (int x = 0; x < count; ++x) {
auto &compiled_source = compiled_sources[x];
compiled_source = gl::shader_t::compile(read_file(sources[x]), shader_type[x % 2]);
compiled_source = gl::shader_t::compile(file_handler::read_file(sources[x]), shader_type[x % 2]);
gl_drain_errors;
if (compiled_source.has_right()) {