Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar committed Sep 10, 2024
1 parent 3819c10 commit d6c3b09
Show file tree
Hide file tree
Showing 44 changed files with 1,268 additions and 1,965 deletions.
557 changes: 557 additions & 0 deletions .paket/Paket.Restore.targets

Large diffs are not rendered by default.

12 changes: 2 additions & 10 deletions examples/CORS/Program.fs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
module Program
module Program

open System
open System.Net

open Suave
open Suave.Logging
open Suave.Filters
open Suave.Writers
open Suave.Files
open Suave.Successful
open Suave.CORS
open Suave.State.CookieStateStore
open Suave.Utils
open Suave.Operators

let logger = Targets.create Verbose [||]

let corsConfig = { defaultCORSConfig with allowedUris = InclusiveOption.Some [ "http://localhost:8085" ] }

let app =
choose [
GET >=> path "/hello" >=> cors corsConfig >=> OK "CORS request accepted."
] >=> logStructured logger logFormatStructured
]

[<EntryPoint>]
let main argv =
Expand Down
10 changes: 4 additions & 6 deletions examples/Example/CounterDemo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ module private Helpers =

open Suave
open Suave.Sockets
open Suave.Sockets.Control
open Suave.Http
open Suave.EventSource
open Suave.Utils

Expand All @@ -29,13 +27,13 @@ open Helpers
let counterDemo (req : HttpRequest) (out : Connection) =

let write i =
socket {
task {
let msg = { id = i; data = string i; ``type`` = None }
do! msg |> send out
return! SocketOp.ofAsync (Async.Sleep 100)
let! _ = send out msg
return! Async.Sleep 100
}

socket {
task {
let lastEvtId =
(req.header "last-event-id" |> Choice.bind muint32) <!>
((req.queryParam "lastEventId") |> Choice.bind muint32) <.>
Expand Down
65 changes: 20 additions & 45 deletions examples/Example/Program.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module Program

open System
open System.Net

open Suave
open Suave.Sockets.Control
open Suave.Logging
open Suave.Operators
open Suave.EventSource
open Suave.Filters
Expand All @@ -17,31 +14,6 @@ open Suave.State.CookieStateStore
let basicAuth =
Authentication.authenticateBasic ((=) ("foo", "bar"))

// This demonstrates how to customise the console logger output.
// In most cases you wont need this. Instead you can use the more succinct:
// `let logger = Targets.create Verbose [||]`
let loggingOptions =
{ Literate.LiterateOptions.create() with
getLogLevelText = function Verbose->"V" | Debug->"D" | Info->"I" | Warn->"W" | Error->"E" | Fatal->"F" }

let logger = LiterateConsoleTarget(
name = [|"Suave";"Examples";"Example"|],
minLevel = Verbose,
options = loggingOptions,
outputTemplate = "[{level}] {timestampUtc:o} {message} [{source}]{exceptions}"
) :> Logger

/// With this workflow you can write WebParts like this
let task : WebPart =
fun ctx -> WebPart.asyncOption {
let! ctx = GET ctx
let! ctx = Writers.setHeader "foo" "bar" ctx
return ctx
}

/// we can still use the old symbol but now has a new meaning
let foo : WebPart = fun ctx -> GET ctx >>= OK "hello"

let myApp =
choose [
GET >=> choose
Expand All @@ -57,7 +29,6 @@ let myApp =
// typed routes
let testApp =
choose [
logStructured logger logFormatStructured >=> never
pathScan "/add/%d/%d" (fun (a,b) -> OK((a + b).ToString()))
pathScan "/minus/%d/%d" (fun (a,b) -> OK((a - b).ToString()))
pathScan "/divide/%d/%d" (fun (a,b) -> OK((a / b).ToString()))
Expand Down Expand Up @@ -86,9 +57,20 @@ let unzipBody : WebPart =
else
return ctx }

open System.IO
open Suave.Sockets
open Suave.Sockets.Control
open System.IO

let write (conn:Connection, _) =
task {
use ms = new MemoryStream()
ms.Write([| 1uy; 2uy; 3uy |], 0, 3)
ms.Seek(0L, SeekOrigin.Begin) |> ignore
// do things here
do! conn.asyncWriteLn (sprintf "Content-Length: %d\r\n" ms.Length)
do! conn.flush()
do! transferStream conn ms
return ()
}

let app =
choose [
Expand All @@ -99,15 +81,7 @@ let app =
path "/hello" >=> OK "Hello World"
path "/byte-stream" >=> (fun ctx ->

let write (conn:Connection, _) = socket {
use ms = new MemoryStream()
ms.Write([| 1uy; 2uy; 3uy |], 0, 3)
ms.Seek(0L, SeekOrigin.Begin) |> ignore
// do things here
do! conn.asyncWriteLn (sprintf "Content-Length: %d\r\n" ms.Length)
do! conn.flush()
do! transferStream conn ms
}


{ ctx with
response =
Expand Down Expand Up @@ -145,11 +119,12 @@ let app =
basicAuth <| choose [ // from here on it will require authentication
// surf to: http://localhost:8082/es.html to view the ES
GET >=> path "/events2" >=> request (fun _ -> EventSource.handShake (fun out ->
socket {
task {
let msg = { id = "1"; data = "First Message"; ``type`` = None }
do! msg |> send out
let! _ = send out msg
let msg = { id = "2"; data = "Second Message"; ``type`` = None }
do! msg |> send out
let! _ = send out msg
()
}))
GET >=> path "/events" >=> request (fun r -> EventSource.handShake (CounterDemo.counterDemo r))
GET >=> browseHome //serves file if exists
Expand Down Expand Up @@ -177,7 +152,7 @@ let app =
>=> OK "Doooooge"
RequestErrors.NOT_FOUND "Found no handlers"
]
] >=> logStructured logger logFormatStructured
] //>=> logStructured logger logFormatStructured

open System.Security.Cryptography.X509Certificates

Expand All @@ -195,7 +170,7 @@ let main argv =
mimeTypesMap = mimeTypes
homeFolder = None
compressedFilesFolder = None
logger = logger
//logger = logger
cookieSerialiser = new BinaryFormatterSerialiser()
hideHeader = false
maxContentLength = 1000000 }
Expand Down
3 changes: 1 addition & 2 deletions examples/Fibonacci/Program.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Suave
open Suave.Http
open Suave
open Suave.Filters
open Suave.Successful

Expand Down
5 changes: 1 addition & 4 deletions examples/Load/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ open System.Net

open Suave
open Suave.Operators
open Suave.Http
open Suave.Filters
open Suave.Files
open Suave.Logging
open System.Threading.Tasks

let logger = Targets.create Verbose [||]

let config =
{ defaultConfig with
bindings = [ HttpBinding.createSimple HTTP "127.0.0.1" 8082 ]
bufferSize = 2048
maxOps = 10000
logger = logger }
}

let listening, server = startWebServerAsync config (choose [ GET >=> browseHome ])
Task.WaitAll server
Expand Down
7 changes: 2 additions & 5 deletions examples/Stream/Program.fs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
module Program
module Program

open System.IO

open Suave
open Suave.Logging
open Suave.Filters
open Suave.Stream
open Suave.Operators

let logger = Targets.create Verbose [||]

let makeStream =
async {
let fileStream = File.Open("./kandinsky-composition-8.jpg", FileMode.Open, FileAccess.Read, FileShare.Read)
Expand All @@ -20,7 +17,7 @@ let makeStream =
let app =
choose [
GET >=> path "/art" >=> Writers.setMimeType "image/jpeg" >=> okStream makeStream
] >=> logStructured logger logFormatStructured
]

[<EntryPoint>]
let main argv =
Expand Down
3 changes: 1 addition & 2 deletions examples/WebSocket/Program.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

open Suave
open Suave.Http
open Suave.Operators
Expand Down Expand Up @@ -89,7 +88,7 @@ let app : WebPart =

[<EntryPoint>]
let main _ =
startWebServer { defaultConfig with logger = Targets.create Verbose [||] } app
startWebServer defaultConfig app
0

//
Expand Down
4 changes: 4 additions & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ group Examples
nuget FSharp.Core
nuget Topshelf.FSharp
nuget Topshelf
nuget Microsoft.Extensions.Logging
nuget Microsoft.Extensions.Logging.Console

group Docs
source https://api.nuget.org/v3/index.json
Expand All @@ -28,6 +30,8 @@ group Docs
nuget Argu
nuget FSharp.Core
nuget FsLibTool
nuget Microsoft.Extensions.Logging
nuget Microsoft.Extensions.Logging.Console

group SourceLink
source https://api.nuget.org/v3/index.json
Expand Down
Loading

0 comments on commit d6c3b09

Please sign in to comment.