<commit_before>﻿module Jim.WebServer

open Jim
open Jim.AppSettings
open Jim.Hawk
open Suave
open Logary //must be opened after Suave
open Suave.Http
open Suave.Http.Applicatives
open Suave.Types
open Suave.Web
open Suave.Extensions.ConfigDefaults
open Suave.Extensions.Guids
open Suave.Extensions.Json
open System.IO

let swaggerSpec = Files.browse_file' <| Path.Combine("static", "api-docs.json")

let index = Successful.OK "Hello from JIM"

let authenticateWithRepo repository partNeedingAuth =
    Hawk.authenticate'
        (hawkSettings repository)
        (fun err -> RequestErrors.UNAUTHORIZED (err.ToString()))
        (fun (attr, creds, user) -> partNeedingAuth)

let webApp postCommand repository =
    let requireAuth = authenticateWithRepo repository

    choose [
        GET >>= choose [
            url "/api-docs" >>= swaggerSpec
            url "/" >>= index
            url "/users" >>= (requireAuth <| QueryEndpoints.listUsers repository)
            url_scan_guid "/users/%s" (fun id -> requireAuth <| QueryEndpoints.getUser repository id) ]

        (* TODO this endpoint should always be via HTTPS *)
        POST >>= url "/users/create" >>= tryMapJson (CommandEndpoints.createUser postCommand)

        PUT >>= choose [ 
            url_scan_guid "/users/%s/name" (fun id -> requireAuth (tryMapJson <| CommandEndpoints.setName postCommand id))
            url_scan_guid "/users/%s/email" (fun id -> requireAuth (tryMapJson <| CommandEndpoints.setEmail postCommand id))
            url_scan_guid "/users/%s/password"  (fun id -> requireAuth (tryMapJson <| CommandEndpoints.setPassword postCommand id)) ]

        RequestErrors.NOT_FOUND "404 not found" ] 

[<EntryPoint>]
let main argv = 
    let web_config = makeConfig appSettings.Port (Suave.SuaveAdapter(Logging.logary.GetLogger "suave"))
    printfn "Starting JIM on %d" appSettings.Port

    try     
        let postCommand, repository = CommandEndpoints.getCommandPosterAndRepository()
        web_server web_config (webApp postCommand repository)
    with<commit_msg>change comment on remaining endpoint without auth<commit_after>﻿module Jim.WebServer

open Jim
open Jim.AppSettings
open Jim.Hawk
open Suave
open Logary //must be opened after Suave
open Suave.Http
open Suave.Http.Applicatives
open Suave.Types
open Suave.Web
open Suave.Extensions.ConfigDefaults
open Suave.Extensions.Guids
open Suave.Extensions.Json
open System.IO

let swaggerSpec = Files.browse_file' <| Path.Combine("static", "api-docs.json")

let index = Successful.OK "Hello from JIM"

let authenticateWithRepo repository partNeedingAuth =
    Hawk.authenticate'
        (hawkSettings repository)
        (fun err -> RequestErrors.UNAUTHORIZED (err.ToString()))
        (fun (attr, creds, user) -> partNeedingAuth)

let webApp postCommand repository =
    let requireAuth = authenticateWithRepo repository

    choose [
        GET >>= choose [
            url "/api-docs" >>= swaggerSpec
            url "/" >>= index
            url "/users" >>= (requireAuth <| QueryEndpoints.listUsers repository)
            url_scan_guid "/users/%s" (fun id -> requireAuth <| QueryEndpoints.getUser repository id) ]

        (* TODO this endpoint should have some sort of security - only open to pre-existing users sending their own Hawk credentials, or only available via HTTPS *)
        POST >>= url "/users/create" >>= tryMapJson (CommandEndpoints.createUser postCommand)

        PUT >>= choose [ 
            url_scan_guid "/users/%s/name" (fun id -> requireAuth (tryMapJson <| CommandEndpoints.setName postCommand id))
            url_scan_guid "/users/%s/email" (fun id -> requireAuth (tryMapJson <| CommandEndpoints.setEmail postCommand id))
            url_scan_guid "/users/%s/password"  (fun id -> requireAuth (tryMapJson <| CommandEndpoints.setPassword postCommand id)) ]

        RequestErrors.NOT_FOUND "404 not found" ] 

[<EntryPoint>]
let main argv = 
    let web_config = makeConfig appSettings.Port (Suave.SuaveAdapter(Logging.logary.GetLogger "suave"))
    printfn "Starting JIM on %d" appSettings.Port

    try     
        let postCommand, repository = CommandEndpoints.getCommandPosterAndRepository()
        web_server web_config (webApp postCommand repository)
    with
{'fraction_non_alphanumeric': 0.09742863882991272, 'fraction_numerical': 0.0014154281670205238, 'mean_word_length': 3.3801652892561984, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, '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': '3987758', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1214, 'n_words': 347}