SAFE-Stack / SAFE-template

dotnet CLI template for SAFE project
MIT License
281 stars 87 forks source link

[Question] SAFE stack cors #431

Closed Freymaurer closed 3 years ago

Freymaurer commented 3 years ago

Hello, hopefully this is the correct place to ask this:

I am using the SAFE stack standard version and want to open my api for access from other apps.

To test this i am using postman OPTION requests on my api endpoint. I am expecting a similiar result to an OPTION (https://localhost:3000/api/IServiceAPIv1/getAppVersion) request on this url https://cors-test.appspot.com/test, but instead i get 404 not found, while an GET request on the same url returns the correct string.

I am not sure how to do this, or if i am even testing it correctly. The Repo in question is this one: https://github.com/nfdi4plants/Spawn

Thanks for the help.

Zaid-Ajaj commented 3 years ago

A default SAFE app can already be accessed from other server applications but not from web browsers from another domain which is what I assume your question about CORS is?

if i am even testing it correctly.

I don't think this test works because remoting endpoints don't handle the OPTION verbs with the exception of the auto generated docs when enabled at {configured docs url}/$schema.

Freymaurer commented 3 years ago

Okay let me tell you exactly what i want to do. I have this app: https://github.com/nfdi4plants/Swate It already has a lot of database functions, which i will also need for this app: https://github.com/nfdi4plants/Spawn

Both are SAFE stacks and use the awesome Fable.Remoting. Now, how do i access the api from 'Swate' from 'Spawn'.

While testing around i tried something like this:

module Route =
    let builderToSwate _ methodName =
        sprintf @"/%s" methodName

let swateApiv1 =
    Remoting.createApi()
    |> Remoting.withBaseUrl @"https://swate.denbi.uni-tuebingen.de/api/IAnnotatorAPIv1/"
    |> Remoting.withRouteBuilder Route.builderToSwate
    |> Remoting.buildProxy<ISwateAPI>

But when i try to use it, i get the following error: image image

Maybe you can point me in the right direction.

Zaid-Ajaj commented 3 years ago

@Freymaurer This is what you are doing which doesn't work because of CORS (but you should be able to enable it from Saturn)

what you are doing

What you should probably be doing in general is this because it relieves you of all the headache of CORS

what you are doing

Where the backend of Spawn talks to the backend of Swate, also via Remoting but using Fable.Remoting.DotnetClient because it is a server-server communication

What I am most curious about is: how are you sharing the types that are in Swate inside Spawn? duplicating the types and are you using a shared library?

Freymaurer commented 3 years ago

Ah i see, i did not know about Fable.Remoting.DotnetClient and will definitely give it a try! My initial idea was that going Frontend -> Backend -> Backend would be noticeably slower than directly forwarding to the Swate backend.

What I am most curious about is: how are you sharing the types that are in Swate inside Spawn? duplicating the types and are you using a shared library?

As we are currently in very early development , i just copy pasted the encessary types from Swate to Spawn. Will have to think about something later.

Thanks for the help!

Zaid-Ajaj commented 3 years ago

It's better to publish a shared library as a nuget called Sware.Types which can used across projects, this way they stay in sync