Create a default http document root if one doesn't exist. #885

This commit is contained in:
Penn, John M 047828115 2019-10-15 15:18:45 -05:00
parent bf288636cb
commit e1a67195d2

View File

@ -4,17 +4,146 @@ LIBRARY DEPENDENCIES:
((VariableServerSession.o)
(http_GET_handlers.o))
**************************************************************************/
#include <sys/stat.h> // for mkdir()
#include <unistd.h> // for symlink(), access()
#include <stdlib.h> // for getenv()
#include <dirent.h> // for opendir(), readdir()
#include <iostream>
#include <fstream>
#include "../include/http_server.hh"
#include "../include/http_GET_handlers.hh"
#include "../include/VariableServerSession.hh"
#include <string.h>
#include <string>
static const struct mg_str s_get_method = MG_MK_STR("GET");
static const struct mg_str s_put_method = MG_MK_STR("PUT");
static const struct mg_str s_delete_method = MG_MK_STR("DELETE");
static const struct mg_str http_api_prefix = MG_MK_STR("/api/http/");
static const struct mg_str ws_api_prefix = MG_MK_STR("/api/ws/");
static const char * style_css =
"h1 {"
"font-family: fantasy, cursive, serif;"
"font-size: 32px;"
"margin-left: 1em;"
"}"
"h2 {"
"font-family: sans-serif;"
"font-size: 18px;"
"margin-left: 1em;"
"}"
"a {"
"font-family: sans-serif;"
"font-size: 16px;"
"}"
"div.header { background-image: linear-gradient(#afafff, white); }";
static const char * index_html =
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n"
"<title>Trick Simulation</title>\n"
"<div class=\"header\">\n"
"<table>\n"
"<th><img src=\"images/trick_icon.png\" height=\"64\" width=\"64\"></th>\n"
"<th><h1>SIM_cannon_numeric</h1></th>\n"
"</table>\n"
"</div>\n"
"</head>\n"
"<body>\n"
"<div style=\"background:#efefef\">\n"
"<ul>\n"
"<li><a href=\"http://github.com/nasa/trick\">Trick on GitHub</a></li>\n"
"<li><a href=\"http://github.com/nasa/trick/wiki/Tutorial\">Trick Tutorial</a></li>\n"
"<li><a href=\"http://github.com/nasa/trick/wiki/Documentation-Home\">Trick Documentation</a></li>\n"
"</ul>\n"
"</div>\n"
"<div style=\"background:#efefef\">\n"
"<ul>\n"
"<li><a href=\"/apps/vs_connections.html\">Variable Server Connections</a></li>\n"
"<li><a href=\"/apps/alloc_info.html\">Trick Memory Allocations</a></li>\n"
"<li><a href=\"/apps/wsexp.html\">Websocket Experiment</a></li>\n"
"<li><a href=\"/apps/react/index.html\">Sim Control and TV Mockup</a></li>\n"
"</ul>\n"
"</div>\n"
"</body>\n"
"</html>n";
static int confirmDocumentRoot ( std::string documentRoot ) {
if ( access( documentRoot.c_str(), F_OK ) != -1 ) {
std::cout << "Trick Webserver: Document root \"" << documentRoot
<< "\" already exists." << std::endl;
} else {
std::cout << "Trick Webserver: Document root \"" << documentRoot
<< "\" doesn't exist so, we'll create it."
<< std::endl;
char* trick_home = getenv("TRICK_HOME");
std::string trickHome = std::string(trick_home);
if (trick_home != NULL) {
if ( mkdir( documentRoot.c_str(), 0700) == 0) {
std::string styleFilePath = documentRoot + "/style.css";
std::fstream style_fs (styleFilePath, std::fstream::out);
style_fs << style_css << std::endl;
style_fs.close();
std::string appsDirPath = documentRoot + "/apps";
if ( mkdir( appsDirPath.c_str(), 0700) == 0) {
DIR *dr;
struct dirent * dir_entry;
std::string trickAppsDirPath = trickHome + "/trick_source/web/apps";
if ( (dr = opendir(trickAppsDirPath.c_str())) != NULL) {
while (( dir_entry = readdir(dr)) != NULL) {
std::string fName = std::string( dir_entry->d_name);
std::string sPath = trickAppsDirPath + '/' + fName;
std::string dPath = appsDirPath + '/' + fName;
symlink(sPath.c_str(), dPath.c_str());
}
}
} else {
std::cout << "Failed to create \"" << appsDirPath << "\"." << std::endl;
return 1;
}
std::string imagesDirPath = documentRoot + "/images";
if ( mkdir( imagesDirPath.c_str(), 0700) == 0) {
DIR *dr;
struct dirent * dir_entry;
std::string trickImagesDirPath = trickHome + "/trick_source/web/images";
if ( (dr = opendir(trickImagesDirPath.c_str())) != NULL) {
while (( dir_entry = readdir(dr)) != NULL) {
std::string fName = std::string( dir_entry->d_name);
std::string sPath = trickImagesDirPath + '/' + fName;
std::string dPath = imagesDirPath + '/' + fName;
symlink(sPath.c_str(), dPath.c_str());
}
}
} else {
std::cout << "Failed to create \"" << imagesDirPath << "\"." << std::endl;
return 1;
}
std::string indexFilePath = documentRoot + "/index.html";
std::fstream index_fs (indexFilePath, std::fstream::out);
index_fs << index_html << std::endl;
index_fs.close();
} else {
std::cout << "Failed to create documentRoot." << std::endl;
return 1;
}
} else {
std::cout << "TRICK_HOME is not set.\n" << std::endl;
return 1;
}
}
return 0;
}
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
http_message *hm = (struct http_message *)ev_data;
@ -234,6 +363,8 @@ int HTTP_Server::http_init() {
http_server_options.document_root = document_root;
http_server_options.enable_directory_listing = "yes";
confirmDocumentRoot( std::string(document_root));
mg_mgr_init( &mgr, NULL );
memset(&bind_opts, 0, sizeof(bind_opts));