| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
- Distributed under MIT license, or public domain if desired and
- recognized in your jurisdiction.
- See file LICENSE for detail.
- */
- #include "cppwebapplication.h"
- #include "cppwebcontroller.h"
- // clazy:excludeall=qgetenv
- CWF_BEGIN_NAMESPACE
- QPair<QString, qint64> getFileAndMaxSize()
- {
- QPair<QString, qlonglong> info;
- info.first = qgetenv(CONFIGURATION::CPP_LOG_VAR.toStdString().data());
- info.second = QByteArray(qgetenv(CONFIGURATION::CPP_LOG_MAX_VAR.toStdString().data())).toInt();
- if (info.second <= 0) {
- info.second = 20000000;
- }
- return info;
- }
- void writeLog(QtMsgType type, const QMessageLogContext &logContext, const QString &msg)
- {
- QPair<QString, qint64> info(getFileAndMaxSize());
- QFile file(info.first);
- if (file.size() > info.second) {
- file.resize(0);
- }
- if (file.open(QIODevice::Append)) {
- QTextStream out(&file);
- QString date = QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm:ss:zzz");
- out << "Date: " << date;
- out << "\nCategory: " << logContext.category;
- out << "\nFile: " << logContext.file;
- out << "\nFunction: " << logContext.function;
- out << "\nVersion: " << logContext.version;
- out << "\nMessage: " << msg << "\n\n";
- if (type == QtFatalMsg) {
- out << "\nFATAL: SERVER OFFLINE\n\n";
- file.close();
- abort();
- }
- file.close();
- }
- }
- CppWebApplication::CppWebApplication(const QString &serverPath, Filter *filter)
- : configuration(serverPath)
- {
- if (configuration.isValid()) {
- qunsetenv(CONFIGURATION::CPP_LOG_VAR.toStdString().data());
- qunsetenv(CONFIGURATION::CPP_LOG_MAX_VAR.toStdString().data());
- qputenv(CONFIGURATION::CPP_LOG_VAR.toStdString().data(),
- configuration.getLogFilePath().toUtf8());
- qputenv(CONFIGURATION::CPP_LOG_MAX_VAR.toStdString().data(),
- QByteArray::number(configuration.getMaxLogFile()));
- QPair<QString, qint64> info(getFileAndMaxSize());
- if (!QFile(info.first).exists()) {
- qDebug() << "Path not found to log file: " << configuration.getLogFilePath();
- qDebug() << "Note: Use only US-ASCII characters for the serverPath.";
- } else if (configuration.isValid()) {
- valid = true;
- //qInstallMessageHandler(writeLog);
- server = new CppWebServer(configuration, filter);
- if (configuration.getAccessServerPages()) {
- server->addController<CppWebController>("/example");
- server->addController<CppWebController>("/authors");
- server->addController<CppWebController>("/documentation");
- server->addController<CppWebController>("/ssl");
- server->addController<CppWebController>("/index");
- }
- }
- } else {
- qDebug() << "CPPWeb.ini not found. Please copy the CWF server folder to your project.";
- }
- }
- CppWebApplication::~CppWebApplication()
- {
- if (!server)
- delete server;
- }
- int CppWebApplication::start()
- {
- return start(configuration.getHost(), configuration.getPort());
- }
- int CppWebApplication::start(const QHostAddress &host, const int &port)
- {
- if (!server->listen(host, port)) {
- qDebug() << "Error: " << server->errorString() << "\n";
- qDebug() << "Host: " << host;
- qDebug() << "Port: " << port;
- qDebug() << "Server offline\n";
- return -1;
- }
- qDebug() << "Server online\n";
- qDebug() << "Host: " << host;
- qDebug() << "Port: " << port;
- qDebug() << "LogFilePath: " << configuration.getLogFilePath();
- return 1;
- }
- bool CppWebApplication::close()
- {
- if (server) {
- server->close();
- return true;
- }
- return false;
- }
- CWF_END_NAMESPACE
|