Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the routes be dynamic? (no crow::black_magic::const_str) #164

Closed
luxe opened this issue Jul 6, 2021 · 3 comments
Closed

Can the routes be dynamic? (no crow::black_magic::const_str) #164

luxe opened this issue Jul 6, 2021 · 3 comments
Labels
question Issue can be closed by providing information

Comments

@luxe
Copy link

luxe commented Jul 6, 2021

I'm looking to make my routes configurable- probably loaded from a file.
Is there a way to make the following work?

#include "include/crow.h"
int main()
{
    crow::SimpleApp app;

    std::string route = "/";
    CROW_ROUTE(app, route)([](){
        return "Hello world";
    });

    app.port(18080).multithreaded().run();
}
error: no viable conversion from 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char>>') to 'crow::black_magic::const_str'
@The-EDev The-EDev added the question Issue can be closed by providing information label Jul 6, 2021
@The-EDev
Copy link
Member

The-EDev commented Jul 6, 2021

Officially, Crow can only take rvalues as routes, meaning the value has to be temporary and cannot be a variable.
But basic testing shows that a bit of modification to app.h can make what you're trying to do work, specifically changing this:
https://github.com/CrowCpp/crow/blob/408166a06f36b4f6fe7be16ed0e5f2bb4ea9e76e/include/crow/app.h#L78
to this:

DynamicRule& route_dynamic(std::string& rule)

You can then use app.route_dynamic(route) instead of CROW_ROUTE(app, route). Which worked for me.

I should still mention that I'm not aware of what consequences this can have. And that it isn't advised to change routes after app.run() is called..

Edit: I should also mention that Crow absolutely moves the value of the rule variable, meaning you most likely won't be able to access the variable once the route is declared.

@The-EDev
Copy link
Member

The-EDev commented Jul 6, 2021

I'll look further into this when I have time. I might end up trying an official implementation so that settings can be loaded from a config file (which was one of the goals @ipkn was trying to achieve)

@The-EDev The-EDev changed the title Can the routes by dynamic? (no crow::black_magic::const_str) Can the routes be dynamic? (no crow::black_magic::const_str) Jul 6, 2021
@The-EDev
Copy link
Member

While I do believe settings coming from a config file would be a good addition, I'm not sure if routes should be a part of that. So I'll close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

No branches or pull requests

2 participants