Skip to content

Instantly share code, notes, and snippets.

@object
Last active October 15, 2024 07:12
Show Gist options
  • Save object/72c91ecbc2f82b6402d231a61d46fdea to your computer and use it in GitHub Desktop.
Save object/72c91ecbc2f82b6402d231a61d46fdea to your computer and use it in GitHub Desktop.
Fable workshop (2024). Step 2. Server.fs
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