All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cpp-qt5-qhttpengine-server.apirouter.h.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>licenseInfo}}
#ifndef {{prefix}}_APIROUTER_H
#define {{prefix}}_APIROUTER_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

{{#apiInfo}}{{#apis}}{{#operations}}#include "{{classname}}Handler.h"
{{/operations}}{{/apis}}{{/apiInfo}}

{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}

class {{prefix}}ApiRequestHandler : public  QHttpEngine::QObjectHandler
{
    Q_OBJECT
signals:
    void requestReceived(QHttpEngine::Socket *socket);

protected:
    virtual void process(QHttpEngine::Socket *socket, const QString &path){
        Q_UNUSED(path);

        // If the slot requires all data to be received, check to see if this is
        // already the case, otherwise, wait until the rest of it arrives
        if (socket->bytesAvailable() >= socket->contentLength()) {
            emit requestReceived(socket);
        } else {
            connect(socket, &Socket::readChannelFinished, [this, socket, m]() {
                emit requestReceived(socket);
            });
        }
    }
};

class {{prefix}}ApiRouter : public QObject
{
    Q_OBJECT
public:
    {{prefix}}ApiRouter();
    virtual ~{{prefix}}ApiRouter();

    void setUpRoutes();
    void processRequest(QHttpEngine::Socket *socket);
    {{#apiInfo}}{{#apis}}
    void set{{classname}}Handler(QSharedPointer<{{classname}}Handler> handler);{{/apis}}{{/apiInfo}}
private:
    QMap> Routes;
    QMultiMap> RoutesWithPathParam;

    bool handleRequest(QHttpEngine::Socket *socket);
    bool handleRequestAndExtractPathParam(QHttpEngine::Socket *socket);

    {{#apiInfo}}{{#apis}}
    QSharedPointer<{{classname}}Handler> m{{classname}}Handler;{{/apis}}{{/apiInfo}}
protected:
    // override this method to provide custom class derived from ApiHandler classes
    virtual void createApiHandlers();

private :
    inline QString fromQHttpEngineMethod(QHttpEngine::Socket::Method method){
        switch( method ){
            case QHttpEngine::Socket::Method::OPTIONS:
                return QStringLiteral("OPTIONS");
            case QHttpEngine::Socket::Method::GET:
                return QStringLiteral("GET");
            case QHttpEngine::Socket::Method::HEAD:
                return QStringLiteral("HEAD");
            case QHttpEngine::Socket::Method::POST:
                return QStringLiteral("POST");
            case QHttpEngine::Socket::Method::PUT:
                return QStringLiteral("PUT");
            case QHttpEngine::Socket::Method::DELETE:
                return QStringLiteral("DELETE");
            case QHttpEngine::Socket::Method::TRACE:
                return QStringLiteral("TRACE");
            case QHttpEngine::Socket::Method::CONNECT:
                return QStringLiteral("CONNECT");
        }
        return QStringLiteral("");
    }
    
    inline QRegularExpressionMatch getRequestMatch(QString serverTemplatePath, QString requestPath){
        QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
        serverTemplatePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
        serverTemplatePath.append("[\\/]?$");
        QRegularExpression pathExpr( serverTemplatePath );
        return pathExpr.match( requestPath );
    }

};


{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}

#endif // {{prefix}}_APIROUTER_H




© 2015 - 2024 Weber Informatics LLC | Privacy Policy