Skip to content

Commit 3cd9add

Browse files
committed
feat: update DotEnv.cs to lookup for .env file in nearest parent folder
1 parent 2298678 commit 3cd9add

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

OsmoDoc.API/DotEnv.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
public static class DotEnv
44
{
5-
public static void Load(string filePath)
5+
public static void LoadEnvFile(string fileName = ".env")
66
{
7-
if (!File.Exists(filePath))
7+
DirectoryInfo? dir = new DirectoryInfo(Directory.GetCurrentDirectory());
8+
9+
while (dir != null)
810
{
9-
return;
11+
string envPath = Path.Combine(dir.FullName, fileName);
12+
if (File.Exists(envPath))
13+
{
14+
Load(envPath);
15+
return;
16+
}
17+
18+
dir = dir.Parent;
1019
}
1120

21+
throw new FileNotFoundException(".env file not found");
22+
}
23+
24+
public static void Load(string filePath)
25+
{
1226
foreach (string line in File.ReadAllLines(filePath))
1327
{
1428
// Check if the line contains '='
@@ -31,5 +45,4 @@ public static void Load(string filePath)
3145
Environment.SetEnvironmentVariable(key, value);
3246
}
3347
}
34-
3548
}

OsmoDoc.API/Program.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using OsmoDoc.API.Models;
1414
using OsmoDoc.Services;
1515
using System.IdentityModel.Tokens.Jwt;
16+
using System.Runtime.InteropServices;
1617

1718
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
1819

@@ -25,23 +26,17 @@
2526
});
2627

2728
// Load .env file
28-
string root = Directory.GetCurrentDirectory();
29-
string dotenv = Path.GetFullPath(Path.Combine(root, "..", ".env"));
30-
if (File.Exists(dotenv))
31-
{
32-
OsmoDoc.API.DotEnv.Load(dotenv);
33-
}
34-
else
29+
OsmoDoc.API.DotEnv.LoadEnvFile();
30+
31+
// Initialize PDF tool path once at startup (on Windows)
32+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
3533
{
36-
throw new FileNotFoundException($".env file not found at path: {dotenv}");
34+
OsmoDocPdfConfig.WkhtmltopdfPath = Path.Combine(
35+
builder.Environment.WebRootPath,
36+
builder.Configuration.GetSection("STATIC_FILE_PATHS:HTML_TO_PDF_TOOL").Value!
37+
);
3738
}
3839

39-
// Initialize PDF tool path once at startup
40-
OsmoDocPdfConfig.WkhtmltopdfPath = Path.Combine(
41-
builder.Environment.WebRootPath,
42-
builder.Configuration.GetSection("STATIC_FILE_PATHS:HTML_TO_PDF_TOOL").Value!
43-
);
44-
4540
// Register REDIS service
4641
builder.Services.AddSingleton<IConnectionMultiplexer>(
4742
ConnectionMultiplexer.Connect(Environment.GetEnvironmentVariable("REDIS_URL") ?? throw new Exception("No REDIS URL specified"))

0 commit comments

Comments
 (0)