Skip to content

Instantly share code, notes, and snippets.

@Kimserey
Created November 15, 2015 01:27
Show Gist options
  • Save Kimserey/5e02f46e033a31b77cd2 to your computer and use it in GitHub Desktop.
Save Kimserey/5e02f46e033a31b77cd2 to your computer and use it in GitHub Desktop.
Sample Sitelet with UI.Next and Owin selfhost
namespace Sample
open WebSharper
open WebSharper.JavaScript
open WebSharper.Sitelets
open WebSharper.UI.Next
open WebSharper.UI.Next.Html
module Resources =
open WebSharper.Core.Resources
type Bootstrap() =
inherit BaseResource(
"//maxcdn.bootstrapcdn.com/bootstrap/3.3.5",
"css/bootstrap.min.css",
"js/bootstrap.min.js")
module Server =
[<Rpc>]
let DoWork (s: string) =
async {
return System.String(List.ofSeq s |> List.rev |> Array.ofList)
}
[<JavaScript>]
module Client =
open WebSharper.UI.Next.Client
let doWork value onResult =
async {
let! data = Server.DoWork value
onResult(data)
}
|> Async.Start
[<Require(typeof<Resources.Bootstrap>)>]
let Main () =
let input = Var.Create ""
let output = Var.Create ""
div [
form [ Doc.Input [] input
Doc.Button "Send" [attr.``type`` "submit"] (fun () -> doWork input.Value (Var.Set output)) ]
hr []
h4Attr [attr.``class`` "text-muted"] [ text "The server responded:" ]
divAttr [attr.``class`` "jumbotron"] [ Doc.TextView output.View ]
]
module Site =
open WebSharper.UI.Next.Server
let main =
Application.SinglePage (fun _ ->
Content.Page(
Title = "Sample",
Body = [ divAttr [attr.``class`` "container"]
[ h1 [text "Say Hi to the server"]
div [client <@ Client.Main() @>] ] ]
))
module SelfHostedServer =
open global.Owin
open Microsoft.Owin.Hosting
open Microsoft.Owin.StaticFiles
open Microsoft.Owin.FileSystems
open WebSharper.Owin
[<EntryPoint>]
let Main = function
| [| rootDirectory; url |] ->
use server = WebApp.Start(url, fun builder ->
builder.UseStaticFiles(StaticFileOptions(FileSystem = PhysicalFileSystem(rootDirectory)))
.UseSitelet(rootDirectory, Site.main)
|> ignore)
stdout.WriteLine("Serving {0}", url)
stdin.ReadLine() |> ignore
0
| _ ->
eprintfn "Usage: Sample ROOT_DIRECTORY URL"
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment