|
| 1 | +module internal ConnectionLookup |
| 2 | + |
| 3 | +open System |
| 4 | +open System.IO |
| 5 | +open System.Text.Json |
| 6 | +open System.Text.Json.Serialization |
| 7 | + |
| 8 | +let private gev key = |
| 9 | + let var = Environment.GetEnvironmentVariable key |
| 10 | + if isNull var || String.IsNullOrWhiteSpace var |
| 11 | + then None |
| 12 | + else Some var |
| 13 | + |
| 14 | +let private makeConnStr = sprintf "AccountEndpoint=%s;AccountKey=%s;" |
| 15 | + |
| 16 | +let private stringHasValue str = |
| 17 | + printfn "checking %s" str |
| 18 | + let ret = not (isNull str || String.IsNullOrEmpty str) |
| 19 | + printfn "ret: %A" ret |
| 20 | + ret |
| 21 | + |
| 22 | +let private findFromEnv() = |
| 23 | + let host = gev "FSHARP_COSMOS_HOST" |
| 24 | + let key = gev "FSHARP_COSMOS_KEY" |
| 25 | + let cs = gev "FSHARP_COSMOS_CONNSTR" |
| 26 | + |
| 27 | + match host, key, cs with |
| 28 | + | (Some host, Some key, None) -> sprintf "AccountEndpoint=%s;AccountKey=%s;" host key |> Some |
| 29 | + | (None, None, cs) -> cs |
| 30 | + | _ -> None |
| 31 | + |
| 32 | +let private possibleFileNames = [ "appsettings.json"; "appsettings.Development.json" ] |
| 33 | + |
| 34 | +type AppSettings = |
| 35 | + { CosmosConnection: {| Host: String option; Key: String option; ConnectionString: String option |} option } |
| 36 | + |
| 37 | +let private findFromAppSettings relativeFile = |
| 38 | + let folders = |
| 39 | + [ Environment.CurrentDirectory |
| 40 | + ((FileInfo(relativeFile)).Directory).FullName ] |
| 41 | + |
| 42 | + let settingsFiles = |
| 43 | + possibleFileNames |
| 44 | + |> List.map (fun file -> folders |> List.map (fun folder -> Path.Combine(folder, file))) |
| 45 | + |> List.append |
| 46 | + (possibleFileNames |
| 47 | + |> List.map (fun file -> folders |> List.map (fun folder -> Path.Combine(folder, file.ToLower())))) |
| 48 | + |> List.collect id |
| 49 | + |> List.filter File.Exists |
| 50 | + |
| 51 | + let options = JsonSerializerOptions() |
| 52 | + options.Converters.Add(JsonFSharpConverter()) |
| 53 | + |
| 54 | + let parsedConfig = |
| 55 | + settingsFiles |
| 56 | + |> List.map (fun file -> |
| 57 | + let contents = File.ReadAllText file |
| 58 | + JsonSerializer.Deserialize<AppSettings>(contents, options)) |
| 59 | + |> List.filter (fun config -> |
| 60 | + match config.CosmosConnection with |
| 61 | + | Some cc -> |
| 62 | + match cc.ConnectionString, cc.Host, cc.Key with |
| 63 | + | (Some cs, None, None) when stringHasValue cs -> true |
| 64 | + | (Some cs, Some _, Some _) when stringHasValue cs -> true |
| 65 | + | (Some _, Some host, Some key) when stringHasValue host && stringHasValue key -> true |
| 66 | + | (None, Some host, Some key) when stringHasValue host && stringHasValue key -> true |
| 67 | + | (_, _, _) -> false |
| 68 | + | None -> false) |
| 69 | + |> List.tryHead |
| 70 | + |
| 71 | + match parsedConfig with |
| 72 | + | None -> None |
| 73 | + | Some config -> |
| 74 | + match config.CosmosConnection with |
| 75 | + | Some cc -> |
| 76 | + match cc.ConnectionString, cc.Host, cc.Key with |
| 77 | + | (Some cs, None, None) when stringHasValue cs -> Some cs |
| 78 | + | (Some cs, Some _, Some _) when stringHasValue cs -> Some cs |
| 79 | + | (Some _, Some host, Some key) when stringHasValue host && stringHasValue key -> |
| 80 | + makeConnStr host key |> Some |
| 81 | + | (None, Some host, Some key) when stringHasValue host && stringHasValue key -> |
| 82 | + makeConnStr host key |> Some |
| 83 | + | (_, _, _) -> None |
| 84 | + | None -> None |
| 85 | + |
| 86 | +let findConnectionString relativeFile = |
| 87 | + match findFromEnv() with |
| 88 | + | Some connStr -> Some connStr |
| 89 | + | None -> |
| 90 | + match findFromAppSettings relativeFile with |
| 91 | + | Some connStr -> Some connStr |
| 92 | + | None -> None |
0 commit comments