Last active
June 27, 2024 19:07
-
-
Save object/2699c9aabc7dbae335951a225b645ea9 to your computer and use it in GitHub Desktop.
Fable workshop (2024). Step 8. WebServer.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module WebServer | |
| open System.IO | |
| open Microsoft.AspNetCore.Cors.Infrastructure | |
| open Giraffe | |
| open Saturn | |
| open Elmish.Bridge | |
| open Shared | |
| open SocketServer | |
| let getFiles ctx = | |
| Directory.EnumerateFiles (getFilesDirectory ()) | |
| |> Seq.map Path.GetFileName | |
| |> Seq.toArray | |
| |> Controller.json ctx | |
| let getFile ctx fileName = | |
| let filePath = Path.Combine [|getFilesDirectory (); fileName|] | |
| if File.Exists filePath then | |
| filePath |> Controller.file ctx | |
| else | |
| fileName |> Response.notFound ctx | |
| let fileController = controller { | |
| index getFiles | |
| show getFile | |
| } | |
| let webApp = | |
| router { | |
| get Route.hello (json "Hello from SAFE!") | |
| forward Route.files fileController | |
| forward Route.socket socketServer | |
| } | |
| let configureCors (builder : CorsPolicyBuilder) = | |
| builder | |
| .WithOrigins("http://localhost:8080") | |
| .AllowAnyMethod() | |
| .AllowAnyHeader() | |
| |> ignore | |
| let app = | |
| application { | |
| url "http://0.0.0.0:5000" | |
| app_config Giraffe.useWebSockets | |
| use_router webApp | |
| memory_cache | |
| use_static "public" | |
| use_json_serializer (Thoth.Json.Giraffe.ThothSerializer()) | |
| use_gzip | |
| use_cors "CORS_policy" configureCors | |
| } | |
| run app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment