Suave.RouteTypeProvider


Tutorial

Example

This example demonstrates using a the type provider to declare your routes in Suave.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
#r "Suave.RouteTypeProvider.dll"
#r "Suave.dll"
open RouteTypeProvider
open System
open Suave
open Suave.Operators
open Suave.Filters
open Suave.Successful

module Routes =
  type FindUserById = routeTemplate<"/findUser/{id:int}">
  type SayBonjour = routeTemplate<template="/bonjour/{Name:string}/{FirstName:string}/{Age:int}",
                                  description="Say hello in french">
  type AdditionRoute = routeTemplate<"/add/{value1:int}/{value2:int}">

let now1 : WebPart =
  fun (x : HttpContext) ->
    async {
      return! OK (DateTime.Now.ToString()) x
    }
let time1 = GET >=> path "/time" >=> now1

let api =
 choose [
    time1
    // go to http://localhost:8083/time

    GET >=> Routes.FindUserById.Returns(fun m -> OK <| sprintf "id is: %A" m.id)
    // go to http://localhost:8083/findUser/789

    GET >=> Routes.SayBonjour.Returns (
      fun m ->
        OK <| sprintf "Bonjour %s %s, your age is %d" m.Name m.FirstName m.Age
      )
    // go to http://localhost:8083/bonjour/homer/simpson/63

    GET >=> Routes.AdditionRoute.Returns(fun m -> OK <| (m.value1 + m.value2).ToString())
    // go to http://localhost:8083/add/98/5
  ]

startWebServer defaultConfig api

Without the type provider

If you want to use an exiting type with this URL format, you can do this:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
open Suave.RouteTypeProvider
open Suave.RouteTypeProvider.Helpers

[<CLIMutable>]
type UserModel =
  { Id:int
    Name:string
    Activated:bool }

let api =
 choose [
     // pathModel is used with an existing type
    pathModel<UserModel> "/user/{Id:int}/{Name:string}/{Activated:bool}"
      (fun m -> OK <| sprintf "%A" m )
    // go to: http://localhost:8083/user/87/roro/true

    // pathDico id used to get params in a dictionary
    pathDico "/user/{id:int}" (fun d -> OK <| sprintf "%A" d)
    // go to: http://localhost:8083/user/87
  ]

startWebServer defaultConfig api
namespace RouteTypeProvider
namespace System
namespace Suave
module Operators

from Suave
module Filters

from Suave
module Successful

from Suave
type FindUserById = routeTemplate<...>

Full name: Tutorial.Routes.FindUserById
type routeTemplate

Full name: RouteTypeProvider.routeTemplate


Create a route from template like "/user/modify/{id:int}/{name:string}/{birth:datetime}"
type SayBonjour = routeTemplate<...>

Full name: Tutorial.Routes.SayBonjour
type AdditionRoute = routeTemplate<...>

Full name: Tutorial.Routes.AdditionRoute
val now1 : x:HttpContext -> Async<HttpContext option>

Full name: Tutorial.now1
Multiple items
module WebPart

from Suave

--------------------
type WebPart = WebPart<HttpContext>

Full name: Suave.Http.WebPart

--------------------
type WebPart<'a> = 'a -> Async<'a option>

Full name: Suave.WebPart.WebPart<_>
val x : HttpContext
Multiple items
module HttpContext

from Suave.Http

--------------------
type HttpContext =
  {request: HttpRequest;
   runtime: HttpRuntime;
   connection: Connection;
   userState: Map<string,obj>;
   response: HttpResult;}
  member clientIp : trustProxy:bool -> sources:string list -> IPAddress
  member clientPort : trustProxy:bool -> sources:string list -> Port
  member clientProto : trustProxy:bool -> sources:string list -> string
  member clientIpTrustProxy : IPAddress
  member clientPortTrustProxy : Port
  member clientProtoTrustProxy : string
  member isLocal : bool
  static member clientIp_ : Property<HttpContext,IPAddress>
  static member clientPort_ : Property<HttpContext,Port>
  static member clientProto_ : Property<HttpContext,string>
  static member connection_ : Property<HttpContext,Connection>
  static member isLocal_ : Property<HttpContext,bool>
  static member request_ : Property<HttpContext,HttpRequest>
  static member response_ : Property<HttpContext,HttpResult>
  static member runtime_ : Property<HttpContext,HttpRuntime>
  static member userState_ : Property<HttpContext,Map<string,obj>>

Full name: Suave.Http.HttpContext
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val OK : body:string -> WebPart

Full name: Suave.Successful.OK
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
property DateTime.Now: DateTime
DateTime.ToString() : string
DateTime.ToString(provider: IFormatProvider) : string
DateTime.ToString(format: string) : string
DateTime.ToString(format: string, provider: IFormatProvider) : string
val time1 : (HttpContext -> Async<HttpContext option>)

Full name: Tutorial.time1
val GET : WebPart

Full name: Suave.Filters.GET
val path : pathAfterDomain:string -> WebPart

Full name: Suave.Filters.path
val api : WebPart<HttpContext>

Full name: Tutorial.api
val choose : options:WebPart<'a> list -> WebPart<'a>

Full name: Suave.WebPart.choose
module Routes

from Tutorial
routeTemplate<...>.Returns(handler: routeTemplate<...>.Model -> HttpContext -> Async<Option<HttpContext>>) : HttpContext -> Async<Option<HttpContext>>
val m : routeTemplate<...>.Model
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
property routeTemplate<...>.Model.id: int64
property routeTemplate<...>.Model.Name: string
property routeTemplate<...>.Model.FirstName: string
property routeTemplate<...>.Model.Age: int64
property routeTemplate<...>.Model.value1: int64
property routeTemplate<...>.Model.value2: int64
val startWebServer : config:SuaveConfig -> webpart:WebPart -> unit

Full name: Suave.Web.startWebServer
val defaultConfig : SuaveConfig

Full name: Suave.Web.defaultConfig
namespace Suave.RouteTypeProvider
module Helpers

from Suave.RouteTypeProvider
Multiple items
type CLIMutableAttribute =
  inherit Attribute
  new : unit -> CLIMutableAttribute

Full name: Microsoft.FSharp.Core.CLIMutableAttribute

--------------------
new : unit -> CLIMutableAttribute
type UserModel =
  {Id: int;
   Name: string;
   Activated: bool;}

Full name: Tutorial.UserModel
UserModel.Id: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
UserModel.Name: string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
UserModel.Activated: bool
type bool = Boolean

Full name: Microsoft.FSharp.Core.bool
val pathModel : format:string -> h:('t -> WebPart) -> WebPart

Full name: Suave.RouteTypeProvider.Helpers.pathModel
val m : UserModel
val pathDico : format:string -> h:(Collections.Generic.IDictionary<string,obj> -> WebPart) -> WebPart

Full name: Suave.RouteTypeProvider.Helpers.pathDico
val d : Collections.Generic.IDictionary<string,obj>
F# Project
Fork me on GitHub