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

Websocket with dynamic routes in 1.1.0 #796

Open
code-snips opened this issue Apr 16, 2024 · 1 comment
Open

Websocket with dynamic routes in 1.1.0 #796

code-snips opened this issue Apr 16, 2024 · 1 comment
Labels
question Issue can be closed by providing information

Comments

@code-snips
Copy link

Is there a way to set up a route

std::string route = "/test";
CROW_WEBSOCKET_ROUTE(app, route)

this gives the compile error:
error: could not convert ‘((WebSocketRouteHandler*)this)->WebSocketRouteHandler::routeUrl’ from ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘crow::black_magic::const_str’
[build] 13 | CROW_WEBSOCKET_ROUTE(app, routeUrl)

Or is there a better alternative than writing reusable objects like:

WebSocketRouteHandler(crow::App<crow::CORSHandler> &app, const std::string &routeUrl, std::string task);
@gittiver gittiver added the question Issue can be closed by providing information label Apr 19, 2024
@fameowner99
Copy link

I have similar question. I achieve this using templates.

const auto func = [this](auto handler_type)
	{
		// iterate through all <HandlerType, HttpMethod> specializations
		const auto body_iteration = [this, &handler_type](auto http_method)
		{
			if constexpr (Handler<handler_type, http_method>::endpoint.size() != 0)
			{
				constexpr auto endpoint = Handler<handler_type, http_method>::endpoint;
				constexpr auto parameter_tag = crow::black_magic::get_parameter_tag(endpoint);

				if constexpr (parameter_tag == 0)
				{
					app->route<crow::black_magic::get_parameter_tag(endpoint)>(std::string(endpoint))
					    .methods(http_method)(Handler<handler_type, http_method>::PreHandle);
				}
				else if constexpr (parameter_tag <= 6)
				{
					app->route<parameter_tag>(std::string(endpoint))
					    .methods(http_method)(
					        Handler<handler_type, http_method>::template PreHandleArgs<type_t<parameter_tag>>);
				}
				else
				{
					constexpr auto first_arg = parameter_tag % 6;
					constexpr auto second_arg = parameter_tag / 6;

					app->route<parameter_tag>(std::string(endpoint))
					    .methods(http_method)(Handler<handler_type, http_method>::template HandleArgs<
					                          type_t<first_arg>, type_t<first_arg>, type_t<second_arg>>);
				}
			}
		};

		// iterate through crow::HTTPMethods
		magic_enum::enum_for_each<crow::HTTPMethod>(body_iteration);
	};
	// iterate through HandlerTypes
	magic_enum::enum_for_each<HandlerType>(func);

where templete struct


template<HandlerType HandlerType, crow::HTTPMethod Method>
struct Handler
{
};

and enum

enum class HandlerType
{
};

and overload every templete.
Not super pretty but it works.
P.S you also need magic_enum or need to write own template recursion

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

3 participants