refactor(main): move file operation functions to new source (#2124)
This commit is contained in:
@@ -45,6 +45,8 @@ set(SUNSHINE_TARGET_FILES
|
|||||||
"${CMAKE_SOURCE_DIR}/src/uuid.h"
|
"${CMAKE_SOURCE_DIR}/src/uuid.h"
|
||||||
"${CMAKE_SOURCE_DIR}/src/config.h"
|
"${CMAKE_SOURCE_DIR}/src/config.h"
|
||||||
"${CMAKE_SOURCE_DIR}/src/config.cpp"
|
"${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.cpp"
|
||||||
"${CMAKE_SOURCE_DIR}/src/logging.h"
|
"${CMAKE_SOURCE_DIR}/src/logging.h"
|
||||||
"${CMAKE_SOURCE_DIR}/src/main.cpp"
|
"${CMAKE_SOURCE_DIR}/src/main.cpp"
|
||||||
|
|||||||
5
docs/source/source_code/src/file_handler.rst
Normal file
5
docs/source/source_code/src/file_handler.rst
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
file_handler
|
||||||
|
============
|
||||||
|
|
||||||
|
.. doxygenfile:: file_handler.h
|
||||||
|
:allow-dot-graphs:
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
#include <boost/property_tree/ptree.hpp>
|
#include <boost/property_tree/ptree.hpp>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "file_handler.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "nvhttp.h"
|
#include "nvhttp.h"
|
||||||
@@ -1215,7 +1216,7 @@ namespace config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read config file
|
// 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) {
|
for (auto &[name, value] : cmd_vars) {
|
||||||
vars.insert_or_assign(std::move(name), std::move(value));
|
vars.insert_or_assign(std::move(name), std::move(value));
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "confighttp.h"
|
#include "confighttp.h"
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
#include "file_handler.h"
|
||||||
#include "httpcommon.h"
|
#include "httpcommon.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
@@ -162,7 +163,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -174,7 +175,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -186,7 +187,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
headers.emplace("Access-Control-Allow-Origin", "https://images.igdb.com/");
|
headers.emplace("Access-Control-Allow-Origin", "https://images.igdb.com/");
|
||||||
@@ -199,7 +200,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -211,7 +212,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -223,7 +224,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -236,7 +237,7 @@ namespace confighttp {
|
|||||||
send_redirect(response, request, "/");
|
send_redirect(response, request, "/");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string content = read_file(WEB_DIR "welcome.html");
|
std::string content = file_handler::read_file(WEB_DIR "welcome.html");
|
||||||
SimpleWeb::CaseInsensitiveMultimap headers;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -248,7 +249,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
headers.emplace("Content-Type", "text/html; charset=utf-8");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -324,7 +325,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "application/json");
|
headers.emplace("Content-Type", "application/json");
|
||||||
response->write(content, headers);
|
response->write(content, headers);
|
||||||
@@ -336,7 +337,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
print_req(request);
|
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;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
headers.emplace("Content-Type", "text/plain");
|
headers.emplace("Content-Type", "text/plain");
|
||||||
response->write(SimpleWeb::StatusCode::success_ok, content, headers);
|
response->write(SimpleWeb::StatusCode::success_ok, content, headers);
|
||||||
@@ -542,7 +543,7 @@ namespace confighttp {
|
|||||||
outputTree.put("platform", SUNSHINE_PLATFORM);
|
outputTree.put("platform", SUNSHINE_PLATFORM);
|
||||||
outputTree.put("version", PROJECT_VER);
|
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) {
|
for (auto &[name, value] : vars) {
|
||||||
outputTree.put(std::move(name), std::move(value));
|
outputTree.put(std::move(name), std::move(value));
|
||||||
@@ -575,7 +576,7 @@ namespace confighttp {
|
|||||||
|
|
||||||
configStream << kv.first << " = " << value << std::endl;
|
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) {
|
catch (std::exception &e) {
|
||||||
BOOST_LOG(warning) << "SaveConfig: "sv << e.what();
|
BOOST_LOG(warning) << "SaveConfig: "sv << e.what();
|
||||||
|
|||||||
69
src/file_handler.cpp
Normal file
69
src/file_handler.cpp
Normal 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
14
src/file_handler.h
Normal 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
|
||||||
@@ -21,9 +21,9 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
#include "file_handler.h"
|
||||||
#include "httpcommon.h"
|
#include "httpcommon.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "main.h"
|
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "nvhttp.h"
|
#include "nvhttp.h"
|
||||||
#include "platform/common.h"
|
#include "platform/common.h"
|
||||||
@@ -161,12 +161,12 @@ namespace http {
|
|||||||
return -1;
|
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 << ']';
|
BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.pkey << ']';
|
||||||
return -1;
|
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 << ']';
|
BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.cert << ']';
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
54
src/main.cpp
54
src/main.cpp
@@ -758,57 +758,3 @@ main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
return lifetime::desired_exit_code;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -26,10 +26,6 @@ extern bool display_cursor;
|
|||||||
// functions
|
// functions
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[]);
|
main(int argc, char *argv[]);
|
||||||
std::string
|
|
||||||
read_file(const char *path);
|
|
||||||
int
|
|
||||||
write_file(const char *path, const std::string_view &contents);
|
|
||||||
void
|
void
|
||||||
launch_ui();
|
launch_ui();
|
||||||
void
|
void
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
// local includes
|
// local includes
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
#include "file_handler.h"
|
||||||
#include "httpcommon.h"
|
#include "httpcommon.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
@@ -1005,8 +1006,8 @@ namespace nvhttp {
|
|||||||
load_state();
|
load_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
conf_intern.pkey = read_file(config::nvhttp.pkey.c_str());
|
conf_intern.pkey = file_handler::read_file(config::nvhttp.pkey.c_str());
|
||||||
conf_intern.servercert = read_file(config::nvhttp.cert.c_str());
|
conf_intern.servercert = file_handler::read_file(config::nvhttp.cert.c_str());
|
||||||
|
|
||||||
crypto::cert_chain_t cert_chain;
|
crypto::cert_chain_t cert_chain;
|
||||||
for (auto &[_, client] : map_id_client) {
|
for (auto &[_, client] : map_id_client) {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
* @brief todo
|
* @brief todo
|
||||||
*/
|
*/
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
|
#include "src/file_handler.h"
|
||||||
#include "src/logging.h"
|
#include "src/logging.h"
|
||||||
#include "src/main.h"
|
|
||||||
#include "src/video.h"
|
#include "src/video.h"
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -780,7 +780,7 @@ namespace egl {
|
|||||||
for (int x = 0; x < count; ++x) {
|
for (int x = 0; x < count; ++x) {
|
||||||
auto &compiled_source = compiled_sources[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;
|
gl_drain_errors;
|
||||||
|
|
||||||
if (compiled_source.has_right()) {
|
if (compiled_source.has_right()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user