Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for additional mime-types. #750

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/commandline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<port>/%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:<port>/%s. |
| `--port` | Port to serve content for http://localhost serving. |
| `--mime-types` | Add additional MIME types to serve. Example ".avi=video/avi". |



Expand Down
32 changes: 29 additions & 3 deletions src/fsdocs-tool/BuildCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

[<Verb("build", HelpText = "build the documentation for a solution based on content and defaults")>]
type BuildCommand() =
inherit CoreBuildOptions(false)
Expand Down Expand Up @@ -1446,3 +1465,10 @@ type WatchCommand() =

[<Option("port", Required = false, Default = 8901, HelpText = "Port to serve content for http://localhost serving.")>]
member val port = 8901 with get, set

override x.mimeTypes_option = x.mimeTypes

[<Option("mime-types",
Required = false,
HelpText = "Add additional MIME types to serve. Example \".avi=video/avi\".")>]
member val mimeTypes: string seq = Seq.empty with get, set