forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
190 lines (169 loc) · 5.43 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <cstdlib>
#include <string>
#include <thread>
#if defined(_WIN32)
#include <winsock2.h>
#include <websocketpp/error.hpp>
#endif
#include "lib/json/json.hpp"
#include "lib/easylogging/easylogging++.h"
#include "lib/filedialogs/portable-file-dialogs.h"
#include "auth/permission.h"
#include "auth/authbasic.h"
#include "server/neuserver.h"
#include "settings.h"
#include "resources.h"
#include "chrome.h"
#include "extensions_loader.h"
#include "api/app/app.h"
#include "api/window/window.h"
#include "api/os/os.h"
#include "api/debug/debug.h"
#define APP_LOG_FILE "/neutralinojs.log"
#define APP_LOG_FORMAT "%level %datetime %msg %loc %user@%host"
#define ELPP_THREAD_SAFE
INITIALIZE_EASYLOGGINGPP
using namespace std;
using json = nlohmann::json;
string navigationUrl = "";
void __wait() {
while(true) {
this_thread::sleep_for(20000ms);
}
}
void __startApp() {
json options = settings::getConfig();
string mode = settings::getMode();
if(mode == "browser") {
os::open(navigationUrl);
__wait();
}
else if(mode == "window") {
json windowOptions = options["modes"]["window"];
windowOptions["url"] = navigationUrl;
window::controllers::init(windowOptions);
}
else if(mode == "cloud") {
if(neuserver::isInitialized()) {
debug::log("INFO", options["applicationId"].get<string>() +
" is available at " + navigationUrl);
}
__wait();
}
else if(mode == "chrome") {
json chromeOptions = options["modes"]["chrome"];
chromeOptions["url"] = navigationUrl;
chrome::init(chromeOptions);
__wait();
}
}
void __configureLogger() {
bool enableLogging = true;
bool enableLogFile = true;
json logging = settings::getOptionForCurrentMode("logging");
if(!logging["enabled"].is_null()) {
enableLogging = logging["enabled"].get<bool>();
}
if(!logging["writeToLogFile"].is_null()) {
enableLogFile = logging["writeToLogFile"].get<bool>();
}
el::Configurations defaultConf;
defaultConf.setToDefault();
defaultConf.setGlobally(
el::ConfigurationType::Format, APP_LOG_FORMAT);
if(enableLogFile) {
defaultConf.setGlobally(
el::ConfigurationType::Filename, settings::joinAppPath(APP_LOG_FILE));
}
defaultConf.setGlobally(
el::ConfigurationType::ToFile, enableLogFile ? "true" : "false");
defaultConf.setGlobally(
el::ConfigurationType::Enabled, enableLogging ? "true" : "false");
el::Loggers::reconfigureLogger("default", defaultConf);
}
void __startServerAsync() {
navigationUrl = settings::getOptionForCurrentMode("url").get<string>();
json jEnableServer = settings::getOptionForCurrentMode("enableServer");
if(!jEnableServer.is_null() && jEnableServer.get<bool>()) {
try {
navigationUrl = neuserver::init();
}
catch(websocketpp::exception &e) {
json jPort = settings::getOptionForCurrentMode("port");
string errorMsg = "Neutralinojs can't initialize the application server";
if(!jPort.is_null()) {
errorMsg += " on port: " + to_string(jPort.get<int>());
}
pfd::message("Unable to start server",
errorMsg,
pfd::choice::ok,
pfd::icon::error);
std::exit(1);
}
neuserver::startAsync();
}
}
void __initFramework(const json &args) {
settings::setGlobalArgs(args);
resources::init();
json options = settings::getConfig();
if(options.is_null()) {
pfd::message("Unable to load app configuration",
"neutralino.config.json file is missing or corrupted.",
pfd::choice::ok,
pfd::icon::error);
std::exit(1);
}
if(options["applicationId"].is_null() || options["defaultMode"].is_null()
|| settings::getOptionForCurrentMode("url").is_null()) {
pfd::message("Missing mandatory configuration",
"Neutralinojs app config should contain applicationId, defaultMode, and url.",
pfd::choice::ok,
pfd::icon::error);
std::exit(1);
}
authbasic::init();
permission::init();
}
void __initExtra() {
bool enableExtensions = false;
bool exportAuthInfo = false;
json exts = settings::getOptionForCurrentMode("enableExtensions");
if(!exts.is_null()) {
enableExtensions = exts.get<bool>();
}
json exportAuth = settings::getOptionForCurrentMode("exportAuthInfo");
if(!exportAuth.is_null()) {
exportAuthInfo = exportAuth.get<bool>();
}
if(exportAuthInfo) {
authbasic::exportAuthInfo();
}
if(enableExtensions) {
extensions::init();
}
}
#if defined(_WIN32)
#define ARG_C __argc
#define ARG_V __argv
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
#define ARG_C argc
#define ARG_V argv
int main(int argc, char ** argv)
#endif
{
json args;
for (int i = 0; i < ARG_C; i++) {
args.push_back(ARG_V[i]);
}
__initFramework(args);
__startServerAsync();
__configureLogger();
__initExtra();
__startApp();
return 0;
}