From 65bc764325ba0f907ed6f55bbaf3198f1dcff060 Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 2 Jun 2022 18:38:04 +0200 Subject: [PATCH] Add support for additional mime-types. --- docs/commandline.md | 13 +++++++------ src/fsdocs-tool/BuildCommand.fs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/docs/commandline.md b/docs/commandline.md index e6afcb7a3..86c5c5edc 100644 --- a/docs/commandline.md +++ b/docs/commandline.md @@ -72,12 +72,13 @@ report an error (e.g. "Problem loading...", "Connection was reset"). Restarting may be necesssary on changes to project files. The same parameters are accepted, plus these: -| Command Line Option | Description | -|:-----------------------|:-----------------------------------------| -| `--noserver` | Do not serve content when watching. | -| `--nolaunch` | Do not launch a browser window. | -| `--open` | URL extension to launch http://localhost:/%s. | -| `--port` | Port to serve content for http://localhost serving. | +| Command Line Option | Description | +|:-----------------------|:----------------------------------------------------------------| +| `--noserver` | Do not serve content when watching. | +| `--nolaunch` | Do not launch a browser window. | +| `--open` | URL extension to launch http://localhost:/%s. | +| `--port` | Port to serve content for http://localhost serving. | +| `--mime-types` | Add additional MIME types to serve. Example ".avi=video/avi". | diff --git a/src/fsdocs-tool/BuildCommand.fs b/src/fsdocs-tool/BuildCommand.fs index 37cfc114a..86399e911 100644 --- a/src/fsdocs-tool/BuildCommand.fs +++ b/src/fsdocs-tool/BuildCommand.fs @@ -627,15 +627,31 @@ module Serve = do! webSocket.send Close emptyResponse true } - let startWebServer rootOutputFolderAsGiven localPort = + let startWebServer rootOutputFolderAsGiven localPort mimeTypes = let defaultBinding = defaultConfig.bindings.[0] let withPort = { defaultBinding.socketBinding with port = uint16 localPort } + let mimeTypes = + if Seq.isEmpty mimeTypes then + defaultConfig.mimeTypesMap + else + Writers.defaultMimeTypesMap + @@ (fun ext -> + Seq.tryFind (fun (mt: string) -> mt.StartsWith(ext)) mimeTypes + |> Option.bind (fun mt -> + let parts = mt.Split('=') + + if parts.Length <> 2 then + None + else + Writers.createMimeType parts.[1] false)) + let serverConfig = { defaultConfig with bindings = [ { defaultBinding with socketBinding = withPort } ] - homeFolder = Some rootOutputFolderAsGiven } + homeFolder = Some rootOutputFolderAsGiven + mimeTypesMap = mimeTypes } let app = choose @@ -1391,7 +1407,7 @@ type CoreBuildOptions(watch) = this.port_option rootOutputFolderFullPath - Serve.startWebServer rootOutputFolderFullPath this.port_option + Serve.startWebServer rootOutputFolderFullPath this.port_option this.mimeTypes_option if not this.nolaunch_option then let url = sprintf "http://localhost:%d/%s" this.port_option this.open_option @@ -1419,6 +1435,9 @@ type CoreBuildOptions(watch) = abstract port_option: int default x.port_option = 0 + abstract mimeTypes_option: string seq + default x.mimeTypes_option = Seq.empty + [] type BuildCommand() = inherit CoreBuildOptions(false) @@ -1446,3 +1465,10 @@ type WatchCommand() = [] member val port = 8901 with get, set + + override x.mimeTypes_option = x.mimeTypes + + [] + member val mimeTypes: string seq = Seq.empty with get, set