Compare commits
No commits in common. "fix" and "main" have entirely different histories.
189
Program.fs
189
Program.fs
@ -1,32 +1,19 @@
|
|||||||
open Giraffe
|
open Giraffe
|
||||||
open System
|
|
||||||
open System.IO
|
|
||||||
open Microsoft.AspNetCore.Builder
|
|
||||||
open Microsoft.AspNetCore.Cors.Infrastructure
|
|
||||||
open Microsoft.AspNetCore.Hosting
|
|
||||||
open Microsoft.Extensions.Hosting
|
|
||||||
open Microsoft.Extensions.Logging
|
|
||||||
open Microsoft.Extensions.DependencyInjection
|
|
||||||
open Microsoft.AspNetCore.Http
|
open Microsoft.AspNetCore.Http
|
||||||
|
open Microsoft.AspNetCore.Builder
|
||||||
#if FABLE_COMPILER
|
open Microsoft.Extensions.DependencyInjection
|
||||||
|
open Microsoft.Extensions.Hosting
|
||||||
open Thoth.Json
|
open Thoth.Json
|
||||||
#else
|
|
||||||
open Thoth.Json.Net
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Here you can write your code as usual
|
|
||||||
|
|
||||||
type QueryFilter =
|
type QueryFilter =
|
||||||
| Equal of string * string
|
| Equal of string * string
|
||||||
| GreaterThan of string * float
|
| GreaterThan of string * float
|
||||||
| LessThan of string * float
|
| LessThan of string * float
|
||||||
|
|
||||||
type QueryDSL =
|
|
||||||
{ Select: string list
|
|
||||||
Filters: QueryFilter list
|
|
||||||
Sort: (string * string) list } // Field and direction
|
|
||||||
|
|
||||||
|
type QueryDSL = {
|
||||||
|
Select : string list
|
||||||
|
Filters : QueryFilter list
|
||||||
|
Sort : (string * string) list // Field and direction
|
||||||
|
}
|
||||||
let parseQuery (body: string) =
|
let parseQuery (body: string) =
|
||||||
// Example JSON parsing (using FSharp.Data or similar)
|
// Example JSON parsing (using FSharp.Data or similar)
|
||||||
// Example query JSON:
|
// Example query JSON:
|
||||||
@ -48,133 +35,85 @@ let parseQuery (body: string) =
|
|||||||
// parsed.["sort"].AsArray()
|
// parsed.["sort"].AsArray()
|
||||||
// |> Array.map (fun s -> s.[0].AsString(), s.[1].AsString())
|
// |> Array.map (fun s -> s.[0].AsString(), s.[1].AsString())
|
||||||
// |> List.ofArray
|
// |> List.ofArray
|
||||||
|
|
||||||
//{ Select = selectFields; Filters = filters; Sort = sort }
|
//{ Select = selectFields; Filters = filters; Sort = sort }
|
||||||
query
|
query
|
||||||
with ex ->
|
with
|
||||||
failwithf "Failed to parse query: %s" ex.Message
|
| ex -> failwithf "Failed to parse query: %s" ex.Message
|
||||||
|
|
||||||
let fetchData (queryDsl: QueryDSL) =
|
let fetchData (queryDsl: QueryDSL) =
|
||||||
// Mock database query (replace with actual DB logic)
|
// Mock database query (replace with actual DB logic)
|
||||||
let mockData =
|
let mockData = [
|
||||||
[ Map.ofList [ "name", box "Alice"; "age", box 30 ]
|
Map.ofList [ "name", box "Alice"; "age", box 30 ]
|
||||||
Map.ofList [ "name", box "Bob"; "age", box 25 ]
|
Map.ofList [ "name", box "Bob"; "age", box 25 ]
|
||||||
Map.ofList [ "name", box "Charlie"; "age", box 35 ] ]
|
Map.ofList [ "name", box "Charlie"; "age", box 35 ]
|
||||||
|
]
|
||||||
// Filter and transform the data based on the DSL
|
// Filter and transform the data based on the DSL
|
||||||
mockData
|
mockData
|
||||||
|> List.filter (fun row ->
|
|> List.filter (fun row ->
|
||||||
queryDsl.Filters
|
queryDsl.Filters |> List.forall (function
|
||||||
|> List.forall (function
|
|
||||||
| Equal(field, num) ->
|
| Equal(field, num) ->
|
||||||
Map.tryFind field row
|
Map.tryFind field row
|
||||||
|> Option.map (fun value -> (string value) = num)
|
|> Option.map(fun value -> (string value) = num)
|
||||||
|> Option.defaultValue false
|
|> Option.defaultValue false
|
||||||
//row.GetType().GetProperty(field).GetValue(row) = value
|
//row.GetType().GetProperty(field).GetValue(row) = value
|
||||||
//| GreaterThan(field, num) -> row.[field] |> float > num
|
//| GreaterThan(field, num) -> row.[field] |> float > num
|
||||||
| GreaterThan(field, num) ->
|
| GreaterThan(field, num) ->
|
||||||
Map.tryFind field row
|
Map.tryFind field row
|
||||||
|> Option.bind (fun value ->
|
|> Option.bind (fun value ->
|
||||||
match value with
|
match value with
|
||||||
| :? float as v -> Some(v > num)
|
| :? float as v -> Some (v > num)
|
||||||
| :? int as v -> Some(float v > num)
|
| :? int as v -> Some (float v > num)
|
||||||
| _ -> None)
|
| _ -> None)
|
||||||
|> Option.defaultValue false
|
|> Option.defaultValue false
|
||||||
| LessThan(field, num) ->
|
| LessThan(field, num) ->
|
||||||
Map.tryFind field row
|
Map.tryFind field row
|
||||||
|> Option.bind (fun value ->
|
|> Option.bind (fun value ->
|
||||||
match value with
|
match value with
|
||||||
| :? float as v -> Some(v < num)
|
| :? float as v -> Some (v < num)
|
||||||
| :? int as v -> Some(float v < num)
|
| :? int as v -> Some (float v < num)
|
||||||
| _ -> None)
|
| _ -> None)
|
||||||
|> Option.defaultValue false))
|
|> Option.defaultValue false))
|
||||||
|> List.map (fun row -> queryDsl.Select |> List.map (fun field -> field, row.[field]) |> dict)
|
|> List.map (fun row ->
|
||||||
|
queryDsl.Select |> List.map (fun field -> field, row.[field]) |> dict)
|
||||||
|> fun result -> result
|
|> fun result -> result
|
||||||
|
|
||||||
let queryHandler =
|
let queryHandler = fun (next: HttpFunc) (ctx: HttpContext) ->
|
||||||
fun (next: HttpFunc) (ctx: HttpContext) ->
|
task {
|
||||||
task {
|
//let! body = ctx.ReadBodyAsStringAsync()
|
||||||
//let! body = ctx.ReadBodyAsStringAsync()
|
let! body = ctx.ReadBodyBufferedFromRequestAsync()
|
||||||
let! body = ctx.ReadBodyBufferedFromRequestAsync()
|
return! parseQuery body
|
||||||
|
|> Result.map(fun queryDsl ->
|
||||||
return!
|
let data = fetchData queryDsl
|
||||||
parseQuery body
|
json data next ctx
|
||||||
|> Result.map (fun queryDsl ->
|
)
|
||||||
let data = fetchData queryDsl
|
|> Result.defaultWith (fun error -> json {|success = false;message = error|} next ctx)
|
||||||
json data next ctx)
|
}
|
||||||
|> Result.defaultWith (fun error -> json {| success = false; message = error |} next ctx)
|
let webApp () =
|
||||||
}
|
POST >=> route "query" >=> queryHandler
|
||||||
|
// [
|
||||||
let webApp =
|
//subRoute "/foo" [ GET [ route "/bar" (text "Aloha!") ] ]
|
||||||
choose
|
// POST
|
||||||
[ POST >=> choose [ route "/query" >=> queryHandler ]
|
// route "query" queryHandler
|
||||||
setStatusCode 404 >=> text "Not Found" ]
|
// RequestErrors.notFound "Not Found"
|
||||||
// ---------------------------------
|
|
||||||
// Error handler
|
// ]
|
||||||
// ---------------------------------
|
|
||||||
|
|
||||||
let errorHandler (ex: Exception) (logger: ILogger) =
|
|
||||||
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
|
|
||||||
clearResponse >=> setStatusCode 500 >=> text ex.Message
|
|
||||||
|
|
||||||
// ---------------------------------
|
|
||||||
// Config and Main
|
|
||||||
// ---------------------------------
|
|
||||||
|
|
||||||
let configureCors (builder: CorsPolicyBuilder) =
|
|
||||||
builder.WithOrigins("http://localhost:5000", "https://localhost:5001").AllowAnyMethod().AllowAnyHeader()
|
|
||||||
|> ignore
|
|
||||||
|
|
||||||
let configureApp (app: IApplicationBuilder) =
|
let configureApp (app: IApplicationBuilder) =
|
||||||
let env = app.ApplicationServices.GetService<IWebHostEnvironment>()
|
app.UseGiraffe queryHandler
|
||||||
|
|
||||||
(match env.IsDevelopment() with
|
|
||||||
| true -> app.UseDeveloperExceptionPage()
|
|
||||||
| false -> app.UseGiraffeErrorHandler(errorHandler).UseHttpsRedirection())
|
|
||||||
.UseCors(configureCors)
|
|
||||||
.UseStaticFiles()
|
|
||||||
.UseGiraffe(webApp)
|
|
||||||
|
|
||||||
let configureServices (services: IServiceCollection) =
|
let configureServices (services: IServiceCollection) =
|
||||||
services.AddCors() |> ignore
|
|
||||||
services.AddGiraffe() |> ignore
|
services.AddGiraffe() |> ignore
|
||||||
|
|
||||||
let configureLogging (builder: ILoggingBuilder) =
|
|
||||||
builder.AddConsole().AddDebug() |> ignore
|
|
||||||
|
|
||||||
[<EntryPoint>]
|
[<EntryPoint>]
|
||||||
let main args =
|
let main args =
|
||||||
let contentRoot = Directory.GetCurrentDirectory()
|
Host.CreateDefaultBuilder(args)
|
||||||
let webRoot = Path.Combine(contentRoot, "WebRoot")
|
.ConfigureWebHostDefaults(fun webBuilder ->
|
||||||
|
webBuilder
|
||||||
Host
|
//.Configure(configureApp)
|
||||||
.CreateDefaultBuilder(args)
|
.ConfigureServices(configureServices)
|
||||||
.ConfigureWebHostDefaults(fun webHostBuilder ->
|
|> ignore)
|
||||||
webHostBuilder
|
|
||||||
//.UseContentRoot(contentRoot)
|
|
||||||
//.UseWebRoot(webRoot)
|
|
||||||
.Configure(Action<IApplicationBuilder> configureApp)
|
|
||||||
.ConfigureServices(configureServices)
|
|
||||||
.ConfigureLogging(configureLogging)
|
|
||||||
|> ignore)
|
|
||||||
.Build()
|
.Build()
|
||||||
.Run()
|
.Run()
|
||||||
|
|
||||||
0
|
0
|
||||||
|
|
||||||
//let configureApp (app: IApplicationBuilder) =
|
|
||||||
// app.UseGiraffe queryHandler
|
|
||||||
|
|
||||||
//let configureServices (services: IServiceCollection) =
|
|
||||||
// services.AddGiraffe() |> ignore
|
|
||||||
|
|
||||||
//[<EntryPoint>]
|
|
||||||
//let main args =
|
|
||||||
// Host.CreateDefaultBuilder(args)
|
|
||||||
// .ConfigureWebHostDefaults(fun webBuilder ->
|
|
||||||
// webBuilder
|
|
||||||
//.Configure(configureApp)
|
|
||||||
// .ConfigureServices(configureServices)
|
|
||||||
// |> ignore)
|
|
||||||
// .Build()
|
|
||||||
// .Run()
|
|
||||||
// 0
|
|
||||||
|
@ -5,12 +5,9 @@
|
|||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<!--<PackageReference Include="FSharp.Json" Version="0.4.1" />-->
|
|
||||||
<PackageReference Include="Giraffe" Version="6.*" />
|
<PackageReference Include="Giraffe" Version="6.*" />
|
||||||
<PackageReference Include="FSharp.Data" Version="6.4.0" />
|
<PackageReference Include="FSharp.Data" Version="4.*" />
|
||||||
<PackageReference Include="Thoth.Json" Version="10.4.0" />
|
<PackageReference Include="Thoth.Json" Version="10.4.0" />
|
||||||
<PackageReference Include="Thoth.Json.Net" Version="12.0.0" />
|
|
||||||
<!--<PackageReference Include="Thoth.Json.Net" Version="10.4.0" />-->
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Program.fs" />
|
<Compile Include="Program.fs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user