<commit_before>﻿//
// Functions related to the preview server.
//
// Author:
//       ipavl <<EMAIL>>
//

module WebServer
    open System
    open System.IO
    open System.Net
    open System.Text

    let httpServer = "http://localhost:8141/"

    let listener (handler:(HttpListenerRequest -> HttpListenerResponse -> Async<unit>)) =
        let httpListener = new HttpListener()
        httpListener.Prefixes.Add httpServer
        httpListener.Start()

        let task = Async.FromBeginEnd(httpListener.BeginGetContext, httpListener.EndGetContext)
        async {
            while true do
                let! context = task
                Async.Start(handler context.Request context.Response)
        } |> Async.Start

    let routeHandler (req : HttpListenerRequest, webRoot : String) =
        let file = Path.Combine(webRoot, Uri(httpServer).MakeRelativeUri(req.Url).OriginalString)
        printfn "Requested: '%s'" file

        if (file.Equals(webRoot)) then
            // If the file path is the web root, return the default index file
            File.ReadAllText(webRoot + "/index.html")
        else if (File.Exists file) then
            // Return the requested file
            File.ReadAllText(file)
        else
            "File not found"<commit_msg>Fix file not found exception with the default route<commit_after>﻿//
// Functions related to the preview server.
//
// Author:
//       ipavl <<EMAIL>>
//

module WebServer
    open System
    open System.IO
    open System.Net
    open System.Text

    let httpServer = "http://localhost:8141/"

    let listener (handler:(HttpListenerRequest -> HttpListenerResponse -> Async<unit>)) =
        let httpListener = new HttpListener()
        httpListener.Prefixes.Add httpServer
        httpListener.Start()

        let task = Async.FromBeginEnd(httpListener.BeginGetContext, httpListener.EndGetContext)
        async {
            while true do
                let! context = task
                Async.Start(handler context.Request context.Response)
        } |> Async.Start

    let routeHandler (req : HttpListenerRequest, webRoot : String) =
        let indexFile = webRoot + "/index.html"
        let file = Path.Combine(webRoot, Uri(httpServer).MakeRelativeUri(req.Url).OriginalString)
        printfn "Requested: '%s'" file

        if (file.Equals(webRoot) && File.Exists(indexFile)) then
            // If the file path is the web root, return the default index file
            File.ReadAllText(indexFile)
        else if (File.Exists file) then
            // Return the requested file
            File.ReadAllText(file)
        else
            "File not found"
{'fraction_non_alphanumeric': 0.09433962264150944, 'fraction_numerical': 0.0030188679245283017, 'mean_word_length': 2.2647783251231526, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1487423', 'n_tokens_mistral': 714, 'n_tokens_neox': 689, 'n_words': 203}