Skip to content

Instantly share code, notes, and snippets.

@gerardtoconnor
Last active July 17, 2018 11:29
Show Gist options
  • Save gerardtoconnor/7518bb3bf0f834cc54cd18121a395e94 to your computer and use it in GitHub Desktop.
Save gerardtoconnor/7518bb3bf0f834cc54cd18121a395e94 to your computer and use it in GitHub Desktop.
module App =
//////////////////
type Zapp<'T> = State<'T> -> unit // Zapp<'T> is temporary name for Zebra function handler
type DepContainer = {
Name :string
}
type Person = {
Name:string
Address:string
Age:int
Gender:string
}
type SQLDummy(connString:String) =
member __.QueryAsync<'T>(query:string) = Task.FromResult (List.empty<'T>)
interface IDisposable with
member __.Dispose () = ()
let deps = { Name = "Gerard" } // Optional simple static dependency object
let dummy : Zapp<'T> = (fun ctx -> ()) // most basic of handlers that do nothing but implicitly continue
let writeTest : Zapp<'T> = (fun ctx -> ctx {
text "Welcome" // Example of how internal buffer allows muliple writes in mutliple handlers.
text " to"
text " the"
text " House"
text " of Pain"
return true
})
let nestTest : Zapp<'T> = (fun ctx -> ctx {
text "Rub a dub dub"
})
let jsonTest : Zapp<DepContainer> = fun ctx -> ctx { // example of json customOperation
json {
Name="Gerard";
Address="London";
Age=33;
Gender="Male"
}}
// WEB APP
let webapp = // Main Web App Builder function
router [
get "/" => writeTest => dummy => dummy
get "/index" => (fun (ctx:State<DepContainer>) -> ctx { text "you are in the index?" } )
get1 "/user/%s/" => (fun s ctx ->
if ctx.Dependencies.Name = s then // control flow not allowed in CE with customOps
ctx { text ("Hello " + ctx.Dependencies.Name) }
else
ctx { text ("You're not Authorised " + s) }
)
get3 "/app/%i/%s/%f" => (fun i s f ctx -> ctx { // Parsing params are now curried
text (sprintf "hello, your trying to reach app %i , %s, %f" i s f)
})
post1 "personQuery/%s" => (fun q ctx -> ctx {
use conn = SQLDummy("connectionString") // all disposables registered are subsequently disposed of at end of request pipeline
let! people = conn.QueryAsync<Person>(q) // binds Tasks as normal
render CompiledView people // 'render' custom operation in design completion (not completed)
})
get "/json" => jsonTest
subRoute "/sub1" [
get "/sub2" => nestTest
get "/choose" => choose [
fun ctx -> ctx { return false } // choose continues to next fallback on list if false
fun ctx -> ctx { return false }
fun ctx -> ctx { text "choice working" } // all customops implicitly move to Next
fun ctx -> ctx { text "choice IS NOT working" } // The prior fun returns true so this never hit
]
]
]
let fallback : Zapp<_> = (fun ctx -> ctx { // fall back function for the app if we return false
text "Url Not Found"
status 404
})
// ---------------------------------
// Main
// ---------------------------------
[<EntryPoint>]
let main _ =
let contentRoot = Directory.GetCurrentDirectory()
let webRoot = Path.Combine(contentRoot, "WebRoot")
WebHost.CreateDefaultBuilder()
.UseWebRoot(webRoot)
.Configure(fun app -> app.UseZebraMiddleware<DepCont>(deps,fallback,webapp)) // Middleware requires 1) Dependency Obj,
.Build() // 2) a fallback/not found Handler
.Run() // 3) The web app handler tree
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment