ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.47k stars 888 forks source link

crow::black_magic::const_str doesn't seem to be able to be used with const std::string types #278

Open cmpunches opened 6 years ago

cmpunches commented 6 years ago

These classes don't seem to be able to play well with built-ins which impacts abstractions during wrapping:

#include "crow/amalgamate/crow_all.h"

struct route
{
    // the relative URL used
    std::string URL;

    // the HTTP method to use
    std::string method;

    // a function pointer expected to process the route entry
    void (*route_handler)(crow::SimpleApp &, route &);
};

// -------

void index_handler( crow::SimpleApp &app, route route_specs )
{
    const std::string url =   (crow::black_magic::const_str) route_specs.URL;
    std::string method = (crow::black_magic::const_str) route_specs.method;

    // TODO need to figure out how this ""_method operator actually works to interpolate route_specs.method
    CROW_ROUTE(app, url).methods("GET"_method)
            ( [] () {
                // expects (route)index
                return "API is online.";
            });
}

void add_two_numbers( crow::SimpleApp &app, route route_specs )
{
    std::string url =   (crow::black_magic::const_str) route_specs.URL;
    std::string method = (crow::black_magic::const_str) route_specs.method;

    // TODO need to figure out how this ""_method operator actually works to interpolate route_specs.method
    CROW_ROUTE(app, url).methods("POST"_method)
            ( [] (const crow::request& req) {
                auto x = crow::json::load(req.body);
                auto y = crow::json::dump(x);
                if (!x)
                    return crow::response(400);
                int sum = x["a"].i()+x["b"].i();
                std::ostringstream os;
                os << sum;
                return crow::response{os.str()};
            } );
}

// -------

class RestAPI
{
    public:
        RestAPI( crow::SimpleApp &app, std::list<route> shape );
};

RestAPI::RestAPI( crow::SimpleApp &app, std::list<route> shape ) {
    // executes 3rd member of all members of (APIShape)shape with its parent struct as its first argument?
    // for entry in shape run entry.handler
    std::list<route>::iterator it;
    for (it = shape.begin(); it != shape.end(); ++it )
    {
        it->route_handler( app, *it );
    }
}
// ------

int main(int argc, char *argv[])
{
    crow::SimpleApp app;

    std::list<route> apiShape;

    route index =       { "/", "GET", &index_handler };
    route add_nums  =   { "/add_two_numbers/<int>/<int>", "GET", &add_two_numbers };

    apiShape.push_back( index );
    apiShape.push_back( add_nums );

    RestAPI restAPI( app, apiShape );

/*
    CROW_ROUTE(app, "/").methods("GET"_method)
            ( [] () {
                return "Hello world!";
            });

    CROW_ROUTE(app,"/hello/<int>/<int>").methods("GET"_method)
            ( [] (int count, int count2) {
                if (count > 100)
                    return crow::response(400);
                std::ostringstream os;
                os << count << " bottles of beer!" << "and " << count2;
                return crow::response(os.str());
            } );

    CROW_ROUTE(app, "/add_json").methods("POST"_method)
            ( [] (const crow::request& req) {
                auto x = crow::json::load(req.body);
                auto y = crow::json::dump(x);
                if (!x)
                    return crow::response(400);
                int sum = x["a"].i()+x["b"].i();
                std::ostringstream os;
                // os << sum;
                os << y;
                return crow::response{os.str()};
            } );
*/
    app.port(18080).run();

}
cmpunches commented 6 years ago

Particularly:

    const std::string url =   (crow::black_magic::const_str) route_specs.URL;
    const std::string method = (crow::black_magic::const_str) route_specs.method;
cmpunches commented 6 years ago

const char * is also incompatible with crow::black_magic::const_str

szulyoben commented 6 years ago

It is compatible with constexpr char array. Have a look for the referenced issue for a small example. #281

nam20485 commented 10 months ago

It is compatible with constexpr char array. Have a look for the referenced issue for a small example. #281

It's not meant to work with const strings of any type, by design. Because it is declared constexpr. So the value must be known at compile-time.

You have to use route_dynamic, which is explained (with an example) in the linked issue.

It's also mentioned briefly in the documentation: https://crowcpp.org/master/guides/routes/#macro. But it's unclear why exactly its usage is "not recommended." It appears that many people still make use of it.

Probably this issue could be closed as works as designed, but I think this repo is abandoned.

55cc commented 8 months ago

/* #define CROW_ROUTE(app, url) app.route<crow::black_magic::get_parameter_tag(url)>(url) */


std::string url ="/xxx"; app.route<crow::black_magic::get_parameter_tag("")>(url.c_str())