Last active
October 15, 2024 07:12
-
-
Save object/72c91ecbc2f82b6402d231a61d46fdea to your computer and use it in GitHub Desktop.
Fable workshop (2024). Step 2. Server.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 Server | |
open System.IO | |
open Giraffe | |
open Saturn | |
open Shared | |
let getFilesDirectory () = | |
Path.Combine [|Directory.GetCurrentDirectory (); "public"|] | |
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 | |
} | |
let app = | |
application { | |
url "http://0.0.0.0:5000" | |
use_router webApp | |
memory_cache | |
use_static "public" | |
use_json_serializer (Thoth.Json.Giraffe.ThothSerializer()) | |
use_gzip | |
} | |
run app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment