I want to try a web application with Actix. Normally, I create controllers for each API point. But I want to try a different application.
For example, I want to access the data in the model section dynamically via "/api/resource/:model". Is it possible to access the struct, which is the model structure, with the model name received when the request is sent by entering the model name?
In other words, is it possible for the user to send the model name as a string via the API service and call it to match the Struct structure with the incoming string?
I'm not exactly sure what you mean by dynamically. Are you trying to support arbitrary models or is there a set of predefined models? I assume it's the latter, in which case you could use a Model enum that has every supported model as variant and use that to extract the data from the request body, for example. I'm not sure this design has many merits over creating an endpoint for every model though.
Yes, each model is registered and specific in the struct structure. I want to process the model (struct) name sent in the URL. I am using sqlx for database. Normally, it is necessary to prepare separate controls for the models, but I wanted to find out if I can dynamically call them with a single control when the model name is called in the url parameter.For this, the parameter in the URL, for example /api/resource/User or /api/resource/Settings, is sent as a parameter and I want to find it dynamically.
If you mean finding types from a string, by their Rust name used in source code — that is not possible. Rust is deliberately not supporting this way of programming.
The names don't exist in the program after compilation, and their data and code may not even exist at all in the program if they're not explicitly referred to in the code.
You need to make mapping from strings to types/functions the boring hard way, explicitly listing every possible combination. Macros can help.
Thank you for your response. Yes, normally I thought of applying simple listing operations for the database on the models. But I see that it seems more logical to create separate controllers.
Thank you. I recently switched from Python to Rust. Sometimes my mind can wander to the structure in Python. It seems more logical to create separate process controls for each model.