Skip to content

Routers, and Templates

Pre-release
Pre-release
Compare
Choose a tag to compare
@AngelMunoz AngelMunoz released this 22 Jul 20:26
· 17 commits to main since this release

What's Changed

  • Saturn router in #2
  • Add dotnet Templates for Bix in #3

new bix dotnet templates

dotnet new --install Bix.Templates::*

bix new routers

open Bix
open Bix.Router
open Bix.Types
open Bix.Handlers

let private postHandler: HttpHandler =
    fun next ctx ->
        promise {
            let! content = ctx.Request.json ()
            return! (sendJson content) next ctx
        }

let basicRouter =
    Router.Empty
    |> Router.get ("/basic", sendHtml "<h1>Hello, World!</h1>")
    |> Router.post ("/text", postHandler)
    |> Router.patch ("/json", sendJson {| message = "posted to /json" |})
    |> Router.subRoute (
        "/profiles",
        (Router.get ("/", sendText "profiles")
         >> Router.post ("/", sendText "create profile")
         >> Router.put ("/:id", sendText "update profile")
         >> Router.delete ("/:id", sendText "delete profile"))
    )

module GiraffeLike =
    open Bix.Router.Giraffe

    let routes =
        choose
            [ route "/g" (sendText "g")
              GET [ route "/g1" (sendText "G1") ]
              POST [ route "/g2" postHandler ]
              subRoute
                  "/products"
                  [ GET
                        [ route "/" (sendText "hit get all products")
                          route "/:id" (sendText "hit get id product") ]
                    POST
                        [ route "/" (sendText "hit products post")
                          route "/:id/category" (sendText "hit products id category post") ]
                    subRoute
                        "/:id/offers"
                        [ route "/admin" (sendText "hit all offers for product id admin")
                          GET
                              [ route "" (sendText "hit get offers")
                                route "/:id" (sendText "hit get offer id get") ] ] ] ]

module SaturnLike =
    open Bix.Router.Saturn

    let routes =
        router {
            get "/" (sendHtml "<h1>Hello, World!</h1>")
            post "/json" postHandler

            forward
                "/offers"
                (router {
                    get "/" (sendText "hit get offers")
                    forward "/:id/tags" (router { get "/" (sendText "hit get offers id tags") })
                })

            forward
                "/todos"
                (controller {
                    find (fun _ -> Text "Found Hit" |> Promise.lift)
                    findOne (fun id _ -> Text $"Found Hit %A{id}" |> Promise.lift)
                    create (fun _ -> Text $"Create Hit" |> Promise.lift)
                    update (fun _ -> Text $"Update Hit" |> Promise.lift)
                    updateOne (fun id _ -> Text $"Update Hit %A{id}" |> Promise.lift)
                    delete (fun id _ -> Text $"Delete Hit %A{id}" |> Promise.lift)
                })
        }

Choose the router that works best for you, they are compatible between each other, so it is more about style and preference than true advantages from one and another

Full Changelog: v1.0.0-beta-002...v1.0.0-beta-003