SAFE-Stack / SAFE-template

dotnet CLI template for SAFE project
MIT License
282 stars 88 forks source link

Support for serverless backend instead of dedicated Saturn server, while still having shared types and behavior? #429

Closed thewoolleyman closed 1 year ago

thewoolleyman commented 3 years ago

Hello!

New to SAFE Stack (and the dotnet ecosystem), and excited to use it for some apps!

The one thing that seems to be missing first-class support is the ability to have a serverless backend (via Azure Functions, perhaps leveraging Azure Static Web Apps) instead of a Saturn server backend. Serverless is a much cheaper architecture for many types of apps than dedicated, always-running server instance(s).

I also want to ensure that I can still take advantage of the awesome ability to share types and behavior across the client and server(less) backend.

My first inclination was to try to modify or repurpose Fable.Remoting. But, I'm still new to this whole ecosystem, and don't know know where to start, or if that's the right approach.

I've done some research, and found the following articles which seem relevant:

QUESTIONS:

Thanks! -- Chad

Zaid-Ajaj commented 3 years ago

The one thing that seems to be missing first-class support is the ability to have a serverless backend

This is not missing, it is just not part of the default template that SAFE offers. It is important to understand that SAFE is just a bunch of libraries that can be switched around.

In case of a serverless application, you need to change the entry point of your application from default implementation that comes with the template to exposing a serverless function definition like in this application for which you will also need two libraries:

See the Yobo repository by @Dzoukr for reference because there some azure function specifics that you also need to take care of such as host.json and making sure the runtime used is compatible etc.

My first inclination was to try to modify or repurpose Fable.Remoting. But, I'm still new to this whole ecosystem, and don't know know where to start, or if that's the right approach.

That's not how you go about it since Fable.Remoting can be integrated as either a part of the Giraffe/Saturn application (exposing a so-called HttpHandler) or as a standard AspNet core middleware, both of which can be used out of the box in a serverless application (like in the Yobo application)

Another simpler example of a serverless SAFE application is Fantomas-UI which follows a similar approach to Yobo

It is true that the documentation is missing on how to go about turning the default template into a serverless one so there is definitely room for improvement. Another option to make a dedicated serverless SAFE template

isaacabraham commented 3 years ago

Just to add to this - you can actually hook Giraffe (and probably Saturn) into e.g. Azure Functions, and AWS Lambda definitely has support for Giraffe as there's even a template and some docs on the AWS site about it.

We should add a receipe (or two) for this as (despite Azure Functions shortcomings) it's an increasing pattern that we're seeing.

thewoolleyman commented 3 years ago

Thank you for the advice and links, @Zaid-Ajaj ! I will look into them.

@isaacabraham , yes, having this as a standard template recipe would be great.

thewoolleyman commented 3 years ago

@isaacabraham regarding this:

despite Azure Functions shortcomings

I am aware of the general risks and concerns with using serverless vs dedicated server architectures.

But are you referring to something specific about Azure Functions? If so, what?

Dzoukr commented 3 years ago

Lack of .NET 5 support (preview - YES! preview! 4 months after .NET 5 release! - announced on Thursday) Low performance of consumption-based hosts 12 seconds startup times Endless fights with dependencies

Pricing is good(-ish), but just be ready for "no free lunch"

isaacabraham commented 3 years ago

@Dzoukr thank you. expressed much more accurately and succinctly than I could have done.

thewoolleyman commented 3 years ago

Pricing is good(-ish), but just be ready for "no free lunch"

@Dzoukr Thanks for the inside info.

I'm planning to take a JAMStack-style approach, and have as much of the logic as possible live in the clients or mashed up from external APIs.

I hope to have the serverless component be mostly a lightweight implementation for backend persistence/API proxying, event sourcing, and scheduled jobs/events.

I haven't tried this on a real app before, so we'll see how it goes. ;)

Dzoukr commented 3 years ago

Don't get me wrong. I love serverless and I am actually a big fan of Azure Functions. Unfortunately, current state of funcs is way behind its potential and seems more like a hurry duck-type solution over WebJobs SDK to react somehow on AWS Lambda hype. But I am an optimist by nature and one day it will be a great service! 🤞

isaacabraham commented 1 year ago

My thoughts are nowadays on Azure to focus more on Container Apps going forwards personally. None the less let's leave this on as a "to-do" item for the backlog of recipes.

@thewoolleyman I know it's been two years since you opened this (!) - if it's something you still would like, could you open this onto the docs repo (https://github.com/SAFE-Stack/docs) and close this? Thanks

thewoolleyman commented 1 year ago

Thanks for the follow-up. I'm now working on this anymore, so I'll just close the issue. Thanks!