Folder-based middleware #584
Replies: 7 comments 10 replies
-
Hello and welcome to SQLPage ! Your suggestion is interesting. Trying to split the problem and the suggested solution, I see two pain points when using SQLPage Problems encountered
Proposed solutionI see obvious advantages to your proposed solution
I see some big drawbacks too:
Alternative solution
@djyotta @matthewlarkin @Pieter3033 @lozdown @alexisrc1 @pchemguy I would be happy to have your thoughts on this. |
Beta Was this translation helpful? Give feedback.
-
Regarding drawbacks, perhaps a better answer is to put middleware into sqlpage/middleware path, so that it's separated from regular requests. And then to add a return value from run_sql which allows controlling copying variables to the current scope, as setting up the request context for the user in each script can be a lot of repeated code. So the new file instead of /auth/index.msql is something like /sqlpage/middleware/auth.sql select 'dynamic' as component, sqlpage.run_sql('middleware/auth.sql') as properties_copy_scope; -- properties_copy_scope means copy the variables. This design may address these drawbacks:
So maybe the only drawback that still exists are the potential to overwrite variables and to be confused by that. Maybe having it as a separate return property from run_sql handles that since they are choosing to do so? Also with this sort of plan, now that it's being called explicitly, the utility of what I was thinking is reduced from what I originally proposed. So the main problem this would solve is now things like setting the user_id in scope. I think the issue of "forgetting" authentication is a big problem that should be handled somehow though, and a lot of frameworks that exist out there provide a magical way to restrict URLs. Here are some examples of what I find myself repeating:
|
Beta Was this translation helpful? Give feedback.
-
Well, the main concept behind SQLPage, which is using mostly pure SQL for all web building tasks has both advantages and limitations. One limitation, which I discussed in one of my posts, is the lack of inherent ability to structure code modules. Among other things, comments can be used to provide visual separation of common code as "header/footer" blocks. Such blocks may include, for example, code that provides an authentication guard and sets module-level variables, such as authenticated user id. Personally, I do not type every module from scratch, in which case it might be easier to forget something. Instead, I have a template for pages requiring authentication that includes appropriate code in the "header" block. Yes, there is still some violation of the DRY concept, but for what it is worth (I have not attempted to develop a big project with SQLPage) I do not see it as a big issue. See CRUD - Authentication for an example. Also, I agree that "magic" is a double-edged sword, as it abstracts certain pieces of code, but usually makes it more difficult to follow the code for someone not familiar with it. |
Beta Was this translation helpful? Give feedback.
-
Sorry for jumping in so late in the discussion. I haven't put much thought into an ideal solution for this yet, but have faced this issue. TLDR: I would appreciate a way to "include" sql into a page. I feel this is the least magic approach. Maybe something like this could work:
Alternatively, add a comment to include a file:
And just preprocess each file looking for the special comments and modifying the contents before passing to the parser. The former (if possible/practical) seems most elegant to me. The latter is probably the most simple approach to implement. I don't like that it's abusing comments, and I don't like that it's a preprocessor instruction. But at least it's not invisible magic. Either approach will introduce complexity to showing correct line of source code on errors. For those who are interested, this is how I work around it using run_sql alone... My "solution" was to funnel all pages through the same entry point and then call run_sql on an "inner" sql depending on the get/post params. The biggest issues I've had with my work around (it's not really a solution) includes:
Code probably explains it better than I can...
Live app: https://shandan.one/clip Example 2: There is common filter I use on every page, and so I set the filter_options in the entry page and then descend into run_sql to render the actual page based on the Live app: https://shandan.one/grocery/volume.sql Unfortunately, the most intricate example is an app I wrote at work that I can not share here. But yeah, you can work around the need to copy-paste same logic over and over just using run_sql. @lovasoa an option to configure recursion depth of |
Beta Was this translation helpful? Give feedback.
-
I ended up building something quite like the pre-processor suggestion to meet my requirements. It lives at paulpr0/simple-include (or https://crates.io/crates/simple-include). It can run as a simple pre-processor for deployment but also has a watcher option so that I can keep it running in the background for development. I make changes in the src directory, and run sqlpage from the target, so simple-include runs in the background and handles copying changes from src to target. My use case is fairly simple, so I haven't built in nested processing (so if file1.sql is included in file2.sql, including file2.sql in another file won't work), but if you are building something like this into SqlPage this would be useful. @lovasoa the CALL syntax you suggest looks good to me. |
Beta Was this translation helpful? Give feedback.
-
What about this:
Should mean line num are accurate for error case. This approach stinks of a framework, and I don't particularly like this sort of magic. But could be rather elegant approach. |
Beta Was this translation helpful? Give feedback.
-
I like sveltekit's folder based routing. |
Beta Was this translation helpful? Give feedback.
-
In my projects, a lot of header code gets repeated. Authentication, role checking, shell inclusion, etc. Also I often have to get the user_id from the session table using the cookie in order to filter queries.
It would be helpful to create a "middleware" script for a given folder that runs whenever any script in that folder is accessed. It would be like run_sql(...) except that any variables it creates internally are copied into the parent scope.
Here's an example of what I was thinking (postgres):
/auth/index.msql: (this is the middleware script)
/auth/index.sql:
/auth/detail.sql:
So the rule would for execution would be: In the current folder path, prior to executing any sql file in that path, look for an "index.msql" file and execute it. Take any resulting variables and copy them into the global scope, and then executed the requested script.
I have never written rust code before, but I could take a swing at this if it's something interesting.
Beta Was this translation helpful? Give feedback.
All reactions