Saturn: Controller Magic

in gaming •  last month

While OAuth fortifies multiple variants of sanction, subsidiary for different use cases, here we will verbalize about sanction code flow. The sanction code grant type is the most commonly utilized because it is optimized for server-side applications (such as web applications utilizing Saturn), where source code is not publicly exposed, and Client Secret confidentiality can be maintained. This is a redirection-predicated flow, which betokens that the application must be capable of interacting with the utilizer-agent (i.e. the user’s web browser) and receiving API sanction codes that are routed through the utilizer-agent.
Take note of the values for Client ID and Client Secret, that will be shown on the next screen after pressing Register application button, as you will require those shortly when registering the OAuth.
Path goes forward
Next step is engendering incipient Saturn application. This can be facilely done utilizing dotnet CLI implement.
Diving deep
Now, at last, it’s time to visually examine some code. Open Program.fs file that contains rudimental configuration of your Saturn application. Firstly, edit URL to utilize port 5000 . Secondly integrate incipient ingression in the application block — use_github_oauth . This custom operation takes couple of input parameters — Client ID, Client Secret, Callback URL, and the list of tuples used to mapping from GitHub’s JSON replication to the claims that Saturn understands.
The matchUpUsers function prototypes check of the data emanating from GitHub with your authentic users database, loggedIn pipeline forces authentication on the request, and isAdmin pipeline checks if the request is done by someone with Admin role.
Now, it’s time to plug it into our routing. Open Router.fs file. We will engender adscititious router that will exhibit two views — one for authenticated in utilizer, second one for the admin. Now you require to plug this incipient router into our top level router — browserRouter . The last thing you require to do here is additionally handling the URL that will be called by GitHub after prosperous authentication. We will redirect it into logged utilizer view.
Saturn is incipient F# web framework that implements well ken design pattern — MVC — in more functional way. Despite Saturn being fairly puerile project it’s getting more and more popular among F# community and industrial users. One of the main Saturn’s goals is to engender high caliber abstractions that will enable developers to fixate on inditing domain, business code in lieu of fixating on engendering correct routing for your application or setting right replication headers. One of such abstractions, that I optate to verbalize about today, is controller.
This article was engendered for Saturn version 0.7.x

Fundamental utilization

controller is simple computation expression (CE) that enables you to facilely implement application endpoint following REST-ish conventions. It can be utilized for implementing endpoints that renders views if you’re building application utilizing server side rendering, or just return serialized data if you’re building API or your application is utilizing client side rendering. Just like all other CEs utilized in Saturn, controller provides set of custom operations that you can utilize. And, what’s paramount, all operations in controller CE are optional, which denotes you can facilely optate which subset of functionality you require.

Let’s now go piecemeal, and describe each operation:

index — mapped into GET request at / endpoint. Conventionally used to render a view displaying list of items, or return whole list of items.
show — mapped into GET request at /:id endpoint. Conventionally used to render a view displaying details of particular item, or return single item with given id.
add — mapped into GET request at /integrate endpoint. Used to render a form for integrating incipient item. Conventionally not utilized in API controllers.
edit — mapped into GET request at /:id/edit. Used to render a form for editing subsisting item. Customarily not utilized in API controllers.
create — mapped into POST request at / endpoint. Used to engender and preserve incipient item.
update — mapped into POST and PUT requests at /:id endpoint. Used to update subsisting item. Conventionally supersedes pristine item (keeping id), and requires are fields to be filled in the incoming item.
patch — mapped into PATCH request at /:id endpoint. Used to update subsisting item. Customarily only changes some fields of pristine item, request body contains only transmuted fields or JSON Patch object.
delete — mapped into EFFACE request at /:id endpoint. Used to efface or deactivate subsisting item.
deleteAll — mapped into EFFACE request at / endpoint. Used to expunge or deactivate all items.
Please recollect that Saturn is not enforcing demeanor or inputs of actions any way, so above descriptions are suggestions and best practices, not something that’s encoded in framework. The only thing that controller provides is set in stone routing structure.

Actions implementation

All actions indexAction, showAction … are simple F# functions. All of them as first parameter accepts HttpContext object — it’s an ASP.NET class that contains all information about incoming request, replication, server, environment and other data that was injected into it by framework. Actions that are utilizing ID of the item, such as showAction or editAction are functions that get id as a second parameter. The id may be generic but we currently fortifies circumscribed set of the possible types to which we can decode ID from URL.
Fortified types:
In case you’d need some custom ID type, I’d recommend utilizing string and deserializing it manually. Another consequential inhibition of current controllers is fact that all actions needs to utilize same ID type in one controller instance. Again, if you’d need different ID types — use string and deserialize it manually.

BTT Address:;u=2545267
ETH Address: 0x08648179816c23c1193b3223684F8A576C420F7B


Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!