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

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

There is a newer version: 7.8.0
Show newest version
{{>licenseInfo}}
#include 
#include 
#include 
#include 
#include 

#include "{{prefix}}ApiRouter.h"
{{#apiInfo}}{{#apis}}{{#operations}}#include "{{classname}}Request.h"
{{/operations}}{{/apis}}{{/apiInfo}}

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

inline QHttpEngine::Socket::Method toQHttpEngineMethod(QString method){

    if( method == QString("OPTIONS"))
        return QHttpEngine::Socket::Method::OPTIONS;
    if( method == QString("GET"))
        return QHttpEngine::Socket::Method::GET;
    if( method == QString("HEAD"))
        return QHttpEngine::Socket::Method::HEAD;
    if( method == QString("POST"))
        return QHttpEngine::Socket::Method::POST;
    if( method == QString("PUT"))
        return QHttpEngine::Socket::Method::PUT;
    if( method == QString("DELETE"))
        return QHttpEngine::Socket::Method::DELETE;
    if( method == QString("TRACE"))
        return QHttpEngine::Socket::Method::TRACE;
    if( method == QString("CONNECT"))
        return QHttpEngine::Socket::Method::CONNECT;

     return static_cast(-1);
}

ApiRouter::ApiRouter() {    
    {{#apiInfo}}{{#apis}}{{classname}}ApiHandler = new {{classname}}Handler();
    {{/apis}}{{/apiInfo}} 
}

ApiRouter::~ApiRouter(){
    qDebug() << "~ApiRouter()";    
    {{#apiInfo}}{{#apis}}delete {{classname}}ApiHandler;
    {{/apis}}{{/apiInfo}}     
}

void ApiRouter::setUpRoutes() {
    {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{^pathParams}}
    Routes.insert("{{{basePathWithoutHost}}}{{{path}}}",[this](QHttpEngine::Socket *socket) {
        if(toQHttpEngineMethod("{{httpMethod}}") == socket->method()){
            auto reqObj = new {{classname}}Request(socket, {{classname}}ApiHandler);
            reqObj->{{nickname}}Request();
        }
    });{{/pathParams}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} 
}

void ApiRouter::processRequest(QHttpEngine::Socket *socket){
    if (Routes.contains(socket->path())) {
        for(auto endpoints : Routes.values(socket->path())) {
            endpoints.operator()(socket);
        }
    } else
    {  {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#pathParams}}        
        { 
            QString completePath("{{{basePathWithoutHost}}}{{{path}}}");
            QString {{paramName}}PathParam("{"); 
            {{paramName}}PathParam.append("{{baseName}}").append("}");
            completePath.replace("/", "\\/"); // replace '/' with '\/' for regex 
            completePath.replace({{paramName}}PathParam, "([^\\/]*?)"); // match anything but '/''
            completePath.append("$");  // End of string
            QRegularExpression re(completePath, QRegularExpression::CaseInsensitiveOption);
            QRegularExpressionMatch match = re.match(socket->path());
            if ((toQHttpEngineMethod("{{httpMethod}}") == socket->method()) && match.hasMatch() ) {
                QString pathparam = match.captured(1);
                auto reqObj = new {{classname}}Request(socket, {{classname}}ApiHandler);
                reqObj->{{nickname}}Request({{#hasPathParams}}{{#pathParams}}pathparam{{/pathParams}}{{/hasPathParams}});
                return; 
            }
        }{{/pathParams}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
    }
    socket->setStatusCode(QHttpEngine::Socket::NotFound);
    if(socket->isOpen()){
        socket->writeHeaders();
        socket->close();
    }
    return;
}
{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy