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

pistache-server.api-source.mustache Maven / Gradle / Ivy

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

#include "{{classname}}.h"

{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}

using namespace {{modelNamespace}};

{{classname}}::{{classname}}(Pistache::Address addr)
    : httpEndpoint(std::make_shared(addr))
{ };

void {{classname}}::init(size_t thr = 2) {
    auto opts = Pistache::Http::Endpoint::options()
        .threads(thr)
        .flags(Pistache::Tcp::Options::InstallSignalHandler);
    httpEndpoint->init(opts);
    setupRoutes();
}

void {{classname}}::start() {
    httpEndpoint->setHandler(router.handler());
    httpEndpoint->serve();
}

void {{classname}}::shutdown() {
    httpEndpoint->shutdown();
}

void {{classname}}::setupRoutes() {
    using namespace Pistache::Rest;

    {{#operation}}
    Routes::{{httpMethod}}(router, base + "{{{vendorExtensions.x-codegen-pistache-path}}}", Routes::bind(&{{classname}}::{{operationIdSnakeCase}}_handler, this));
    {{/operation}}

    // Default handler, called when a route is not found
    router.addCustomHandler(Routes::bind(&{{classname}}::{{classnameSnakeLowerCase}}_default_handler, this));
}

{{#operation}}
void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
    {{#vendorExtensions.x-codegen-pistache-isParsingSupported}}
    {{#hasPathParams}}
    // Getting the path params
    {{#pathParams}}
    auto {{paramName}} = request.param(":{{paramName}}").as<{{dataType}}>();
    {{/pathParams}}
    {{/hasPathParams}}{{#hasBodyParam}}
    // Getting the body param
    {{#bodyParam}}
    {{^isPrimitiveType}}
    {{baseType}} {{paramName}};{{/isPrimitiveType}}
    {{#isPrimitiveType}}
    {{dataType}} {{paramName}};
    {{/isPrimitiveType}}
    {{/bodyParam}}
    {{/hasBodyParam}}{{#hasQueryParams}}
    // Getting the query params
    {{#queryParams}}
    auto {{paramName}} = request.query().get("{{baseName}}");
    {{/queryParams}}
    {{/hasQueryParams}}{{#hasHeaderParams}}
    // Getting the header params
    {{#headerParams}}
    auto {{paramName}} = request.headers().tryGetRaw("{{baseName}}");
    {{/headerParams}}
    {{/hasHeaderParams}}

    try {
    {{#hasBodyParam}}
    {{#bodyParam}}
      nlohmann::json request_body = nlohmann::json::parse(request.body());
    {{^isPrimitiveType}}
      {{paramName}}.fromJson(request_body);
    {{/isPrimitiveType}}
    {{#isPrimitiveType}}
      // The conversion is done automatically by the json library
      {{paramName}} = request_body;
    {{/isPrimitiveType}}
    {{/bodyParam}}
    {{/hasBodyParam}}
      this->{{operationIdSnakeCase}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}response);
    {{/vendorExtensions.x-codegen-pistache-isParsingSupported}}
    {{^vendorExtensions.x-codegen-pistache-isParsingSupported}}
    try {
      this->{{operationIdSnakeCase}}(request, response);
    {{/vendorExtensions.x-codegen-pistache-isParsingSupported}}
    } catch (std::runtime_error & e) {
      //send a 400 error
      response.send(Pistache::Http::Code::Bad_Request, e.what());
      return;
    }

}
{{/operation}}

void {{classname}}::{{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
    response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
}

{{#apiNamespaceDeclarations}}
}
{{/apiNamespaceDeclarations}}

{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy