Skip to content

Instantly share code, notes, and snippets.

@allykzam
Last active August 29, 2015 14:28
Show Gist options
  • Save allykzam/fb88fedf7b99a758bf4d to your computer and use it in GitHub Desktop.
Save allykzam/fb88fedf7b99a758bf4d to your computer and use it in GitHub Desktop.
Bad home-made server and basic Suave site for proxying calls for source code off to GitHub; for use with SourceLink
#r "packages/Suave/lib/net40/Suave.dll"
open Suave.Http
open Suave.Http.Applicatives
open Suave.Http.Successful
open Suave.Types
let hasText = not << System.String.IsNullOrWhiteSpace
let checkAuthentication = Authentication.authenticateBasic (fun (x, y) -> hasText x && hasText y)
let getGitHubData (r : HttpRequest) : WebPart =
match r.header("authorization") with
| Choice2Of2(_) -> RequestErrors.FORBIDDEN "Authorization is required to access private GitHub repositories. If using 2-factor authentication, use a token with repo-level access as your password when authenticating."
| Choice1Of2(auth) ->
try
use wc = new System.Net.WebClient()
wc.Headers.Add("Authorization", auth)
let url = "https://raw.githubusercontent.com" + r.url.AbsolutePath
let responseData = wc.DownloadString(url)
OK responseData
with
| :? System.Net.WebException as ex when ex.Message = "The remote server returned an error: (404) Not Found." -> RequestErrors.NOT_FOUND "GitHub can't find the specified file, or you do not have permission to view it."
| ex -> ServerErrors.INTERNAL_ERROR <| sprintf "Received the following error requesting data from GitHub:\n\n%s" ex.Message
let app =
choose
[
GET >>= choose
[
path "/index2.txt" >>= OK "Hello, Visual Studio! Yes, this server is running.";
checkAuthentication;
request getGitHubData;
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment