dotnet-websharper / core

WebSharper - Full-stack, functional, reactive web apps and microservices in F# and C#
https://websharper.com
Apache License 2.0
593 stars 52 forks source link

Customizing service handlers #1357

Open granicz opened 10 months ago

granicz commented 10 months ago

Given a record or class-based IApi definition, one usually injects it into the server's routes via:

type IApi = {
    [<Rpc>]
    GetAllUsers : unit -> Async<DTO.User list>
}

let server : IApi = {
    GetAllUsers = fun () -> ...
    }
}

WebSharper.Core.Remoting.AddHandler typeof<IApi> server

This underneath the cover creates paths like /IApi/*, but the client is shielded from having to know the details and instead can just call using the established interface:

let server = Remote<IApi>
let doSomething() = 
    async {
        let! hello = server.GetAllUsers()
        printfn $"{hello}"
    }
    |> Async.StartImmediate

However, it would be beneficial to be able to customize how the various RPCs in the supplied service record/class are exposed.

Proposal: allow [<EndPoint>] on the RPC type to override the URL prefix for the service group (so changing /IApi/* to /api/*):

[<EndPoint "/api">]
type IApi = { ... }