|
| 1 | +open Falco |
| 2 | +open Falco.Datastar.Selector |
| 3 | +open Falco.Datastar.SignalPath |
| 4 | +open Falco.Markup |
| 5 | +open Falco.Routing |
| 6 | +open Falco.Datastar |
| 7 | +open Microsoft.AspNetCore.Builder |
| 8 | +open Microsoft.AspNetCore.Http |
| 9 | + |
| 10 | +module View = |
| 11 | + let template content = |
| 12 | + Elem.html [ Attr.lang "en" ] [ |
| 13 | + Elem.head [] [ Ds.cdnScript ] |
| 14 | + Elem.body [] content |
| 15 | + ] |
| 16 | + |
| 17 | +module App = |
| 18 | + let handleIndex : HttpHandler = |
| 19 | + let checkbox name = |
| 20 | + [ Text.raw $"{name}:"; Elem.input [ Attr.type' "checkbox"; Attr.name "checkboxes"; Attr.value name ] ] |
| 21 | + let html = |
| 22 | + View.template [ |
| 23 | + Text.h1 "Example: Input Form" |
| 24 | + Elem.div [] [ |
| 25 | + Elem.form [ Attr.id "myform" ] [ |
| 26 | + yield! checkbox "foo" |
| 27 | + yield! checkbox "bar" |
| 28 | + yield! checkbox "baz" |
| 29 | + Elem.button [ Ds.onClick (Ds.get("/endpoint1", RequestOptions.With(Form))) ] [ Text.raw "Submit GET Request" ] |
| 30 | + Elem.button [ Ds.onClick (Ds.post("/endpoint1", RequestOptions.With(Form))) ] [ Text.raw "Submit POST Request" ] |
| 31 | + ] |
| 32 | + Elem.button [ Ds.onClick (Ds.get("/endpoint1", RequestOptions.With(SelectedForm (sel"#myform")))) ] [ |
| 33 | + Text.raw "Submit GET request from outside the form" |
| 34 | + ] |
| 35 | + ] |
| 36 | + Elem.hr [] |
| 37 | + Elem.div [] [ |
| 38 | + Elem.form [ Ds.onEvent ("submit", (Ds.post ("/endpoint2", RequestOptions.With(Form)))) ] [ |
| 39 | + Text.raw "foo:" |
| 40 | + Elem.input [ Attr.type' "text"; Attr.name "foo"; Attr.required ] |
| 41 | + Elem.button [] [ Text.raw "Submit Form" ] |
| 42 | + ] |
| 43 | + ] |
| 44 | + ] |
| 45 | + Response.ofHtml html |
| 46 | + |
| 47 | + let handleEndpointOne (getForm:HttpContext -> RequestData) : HttpHandler = (fun ctx -> task { |
| 48 | + let method = ctx.Request.Method |
| 49 | + let form = ctx |> getForm |
| 50 | + let foo = form.GetStringList("checkboxes") |
| 51 | + |
| 52 | + let alertString = $"Form data received via {method} request: checkboxes = {foo}" |
| 53 | + let alertScript = $"alert('{alertString}')" |
| 54 | + |
| 55 | + return Response.ofExecuteScript alertScript ctx |
| 56 | + }) |
| 57 | + |
| 58 | + let handleEndpointTwo (getForm:HttpContext -> RequestData): HttpHandler = (fun ctx -> task { |
| 59 | + let method = ctx.Request.Method |
| 60 | + let form = ctx |> getForm |
| 61 | + let foo = form.GetString("foo") |
| 62 | + |
| 63 | + let alertString = $"Form data received via {method} request: foo = {foo}" |
| 64 | + let alertScript = $"alert('{alertString}')" |
| 65 | + |
| 66 | + return Response.ofExecuteScript alertScript ctx |
| 67 | + }) |
| 68 | + |
| 69 | + |
| 70 | +[<EntryPoint>] |
| 71 | +let main args = |
| 72 | + let wapp = WebApplication.Create() |
| 73 | + |
| 74 | + let endpoints = |
| 75 | + [ |
| 76 | + get "/" App.handleIndex |
| 77 | + all "/endpoint1" [ |
| 78 | + GET, (App.handleEndpointOne Request.getQuery) |
| 79 | + POST, (App.handleEndpointOne (fun ctx -> (ctx |> Request.getForm).Result)) |
| 80 | + ] |
| 81 | + all "/endpoint2" [ |
| 82 | + GET, (App.handleEndpointTwo Request.getQuery) |
| 83 | + POST, (App.handleEndpointTwo (fun ctx -> (ctx |> Request.getForm).Result)) |
| 84 | + ] |
| 85 | + ] |
| 86 | + |
| 87 | + wapp.UseRouting() |
| 88 | + .UseFalco(endpoints) |
| 89 | + .Run() |
| 90 | + 0 // Exit code |
0 commit comments