Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Created May 17, 2013 03:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsakamoto/5596729 to your computer and use it in GitHub Desktop.
Save jsakamoto/5596729 to your computer and use it in GitHub Desktop.
Small http daemon by F# script. run "> fsi.exe httpd.fsx", then you can access http://localhost:8080/anycontents
open System
open System.IO
open System.Net
let root = @"c:\inetpub\wwwroot"
let listener = new HttpListener()
listener.Prefixes.Add "http://*:8080/"
listener.Start ()
let rec procreq ():(unit->unit) =
let context = listener.GetContext ()
let req = context.Request
let res = context.Response
let path = Path.Combine (root, (req.RawUrl.TrimStart('/').Replace("/","\\")))
match (File.Exists path) with
| true -> path |> File.ReadAllBytes |> fun content -> res.OutputStream.Write(content, 0, content.Length)
| false -> res.StatusCode <- 404
res.Close ()
procreq ()
procreq ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment