Skip to content

Commit

Permalink
Refactor code formatting and improve readability in Program.fs
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho committed Dec 26, 2024
1 parent 4dd1e0e commit 483e76e
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions api/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,41 @@ let getOrCreateJwtSecret pgConn jwtAudienceName =
let secret = JwtSecrets.GetJwtSecret pgConn jwtAudienceName
printfn "Existing JWT Secret found for %s" jwtAudienceName
Some secret
with :? NoResultsException -> None
with :? NoResultsException ->
None

let generateRandomKey () =
System.Convert.ToBase64String(System.Security.Cryptography.RandomNumberGenerator.GetBytes(32))

let getJwtKey () =
match Util.getEnvVar "JWT_SECRET" with
| null ->
let randomKey = generateRandomKey()
| null ->
let randomKey = generateRandomKey ()
printfn "Warning: JWT_SECRET not set. Using randomly generated key: %s" randomKey
randomKey
| key -> key

let getAudience () =
match Util.getEnvVar "JWT_AUDIENCE" with
| null ->
let defaultAudience = generateRandomKey()
| null ->
let defaultAudience = generateRandomKey ()
printfn "Warning: JWT_AUDIENCE not set. Using default audience: %s" defaultAudience
defaultAudience
| aud -> aud

let createNewSecret () =
let jwtSecretParams: JwtSecrets.CreateJwtSecretParams =
let jwtSecretParams: JwtSecrets.CreateJwtSecretParams =
{ Name = jwtAudienceName
Secret = getJwtKey()
Audience = getAudience() }
Secret = getJwtKey ()
Audience = getAudience () }

let createdSecret = JwtSecrets.CreateJwtSecret pgConn jwtSecretParams
printfn "New JWT Secret created for %s" jwtAudienceName
createdSecret

match getExistingSecret() with
match getExistingSecret () with
| Some secret -> secret
| None -> createNewSecret()
| None -> createNewSecret ()


let authService (services: IServiceCollection) =
Expand All @@ -103,7 +105,7 @@ let authService (services: IServiceCollection) =
let jwtAudienceName = "logbook"

let jwtSecret = getOrCreateJwtSecret pgConn jwtAudienceName

pgConn.Close()

let _ =
Expand All @@ -118,7 +120,8 @@ let authService (services: IServiceCollection) =
ValidateAudience = true,
ValidAudience = jwtSecret.Audience,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret.Secret))
IssuerSigningKey =
new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret.Secret))
))

services
Expand All @@ -133,7 +136,9 @@ webHost [||] {
add_service authService

// init db
add_service (fun services -> Database.InitDB.init Database.Config.connStr |> ignore ; services)
add_service (fun services ->
Database.InitDB.init Database.Config.connStr |> ignore
services)

// Use authorization middleware. Call before any middleware that depends on users being authenticated.
// jwt decode add set context.User.Identity.IsAuthenticated true if user is valid
Expand All @@ -152,11 +157,11 @@ webHost [||] {
get "/api/diary" Note.noteAllPart
get "/api/diary/{id}" Note.noteByIdPartDebug
put "/api/diary/{id}" Note.addNotePart
get "/api/todo" Note.todoListsHandler
get "/api/todo" Note.todoListsHandler
// export api
post "/api/export_json" Note.exportDiary
post "/api/export_md" Note.exportDiaryMarkdown
get "/api/export_all" Note.exportAllDiaries
]
get "/api/export_all" Note.exportAllDiaries ]

use_middleware serveVueFiles
}

0 comments on commit 483e76e

Please sign in to comment.