Skip to content

Commit dbc0818

Browse files
committed
configuration of EdgeDb connection by project name
1 parent abbfc73 commit dbc0818

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed

WeatherControl/Wissance.WeatherControl.WebApi.V2/Config/DatabaseSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace Wissance.WeatherControl.WebApi.V2.Config
77
{
88
public class DatabaseSettings
99
{
10-
public string ConnStr { get; set; }
10+
public string ProjectName { get; set; }
1111
}
1212
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Wissance.WeatherControl.WebApi.V2.Data
4+
{
5+
public class EdgeDbProjectCredentials
6+
{
7+
public int Port { get; set; }
8+
public string User { get; set; }
9+
public string Password { get; set; }
10+
public string Database { get; set; }
11+
[JsonProperty("tls_cert_data")]
12+
public string TlsCertData { get; set; }
13+
[JsonProperty("tls_ca")]
14+
public string TlsCa { get; set; }
15+
[JsonProperty("tls_security")]
16+
public string TlsSecurity { get; set; }
17+
}
18+
}

WeatherControl/Wissance.WeatherControl.WebApi.V2/Managers/EdgeDbManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public async Task<OperationResultDto<Tuple<IList<TRes>,long>>> GetAsync(int page
111111
if (countQuery == null)
112112
throw new NotSupportedException($"EQL count query for model {_model} is not ready");
113113
long totalItems = await _edgeDbClient.QuerySingleAsync<long>(countQuery);
114-
// todo: modve to dictionary ...
114+
// todo: move to dictionary ...
115115
string getQuery = _resolver.GetQueryToFetchManyItems(_model, (page - 1) * size, size);
116116
if (getQuery == null)
117117
throw new NotSupportedException($"EQL get query for model {_model} is not ready");

WeatherControl/Wissance.WeatherControl.WebApi.V2/Startup.cs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Globalization;
9+
using System.IO;
910
using System.Linq;
1011
using System.Threading.Tasks;
1112
using Microsoft.Extensions.Logging;
1213
using Serilog;
1314
using EdgeDB;
1415
using Microsoft.AspNetCore.Routing.Internal;
16+
using Newtonsoft.Json;
1517
using Wissance.WeatherControl.WebApi.V2.Config;
18+
using Wissance.WeatherControl.WebApi.V2.Data;
1619

1720

1821
namespace Wissance.WeatherControl.WebApi.V2
@@ -69,33 +72,88 @@ private void ConfigureLogging(IServiceCollection services)
6972

7073
private void ConfigureDatabase(IServiceCollection services)
7174
{
72-
EdgeDBConnection conn = EdgeDBConnection.FromDSN(Settings.Database.ConnStr);
75+
ILoggerFactory loggerFactory = services.BuildServiceProvider().GetRequiredService<ILoggerFactory>();
76+
// todo(UMV): Resolve security settings using Project Name: that is why important to have same project name for all Backend developers
77+
// use ~AppData\Local\EdgeDB\config\credentials
78+
string connStr = GetEdgeDbConnStrByProjectName(Settings.Database.ProjectName, loggerFactory);
79+
EdgeDBConnection conn = EdgeDBConnection.FromDSN(connStr);
7380
conn.TLSSecurity = TLSSecurityMode.Insecure;
7481

7582
services.AddEdgeDB(conn, cfg =>
7683
{
7784
cfg.ClientType = EdgeDBClientType.Tcp;
85+
cfg.SchemaNamingStrategy = INamingStrategy.CamelCaseNamingStrategy;
7886
cfg.DefaultPoolSize = 256;
79-
cfg.ConnectionTimeout = 3000;
80-
cfg.MessageTimeout = 5000;
87+
cfg.ConnectionTimeout = 5000;
88+
cfg.MessageTimeout = 10000;
8189
});
8290
}
8391

8492
private void ConfigureWebApi(IServiceCollection services)
8593
{
8694
services.AddControllers();
87-
88-
// ConfigureManagers(services);
8995
}
9096

9197
/*private void ConfigureManagers(IServiceCollection services)
9298
{
9399
// services.AddScoped<StationManager>();
94100
// services.AddScoped<MeasurementsManager>();
95101
}*/
102+
103+
private string GetEdgeDbConnStrByProjectName(string projectName, ILoggerFactory loggerFactory)
104+
{
105+
ILogger<Startup> logger = loggerFactory.CreateLogger<Startup>();
106+
try
107+
{
108+
string projectCredentialsFile = GetEdgeDbProjectCredentialFile(projectName);
109+
string content = File.ReadAllText(projectCredentialsFile);
110+
EdgeDbProjectCredentials credentials = JsonConvert.DeserializeObject<EdgeDbProjectCredentials>(content);
111+
//JsonSerializer.Deserialize<EdgeDbProjectCredentials>(content);
112+
return string.Format(EdgeDbConnStrTemplate, credentials.User, credentials.Password, "localhost",
113+
credentials.Port,
114+
credentials.Database);
115+
}
116+
catch (Exception e)
117+
{
118+
logger.LogError($"An error occurred during attempt to build edgedb connection str: {e.Message}");
119+
return string.Empty;
120+
}
121+
}
122+
123+
private string GetEdgeDbProjectCredentialFile(string projectName)
124+
{
125+
string projectCredentialsFile = string.Empty;
126+
if (OperatingSystem.IsWindows())
127+
{
128+
// should be using ~AppData\Local\EdgeDB\config\credentials\{projectName}.json as a path
129+
string appData = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
130+
projectCredentialsFile = Path.Combine(new string[]
131+
{
132+
appData,
133+
"EdgeDB", "config", "credentials",
134+
projectName + ".json"
135+
});
136+
}
137+
138+
if (OperatingSystem.IsLinux())
139+
{
140+
// linux - /.config/edgedb/credentials/{projectName}.json relative to home dir
141+
string home = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
142+
projectCredentialsFile = Path.Combine(new string[]
143+
{
144+
home,
145+
".config", "edgedb", "credentials",
146+
projectName + ".json"
147+
});
148+
}
149+
150+
return projectCredentialsFile;
151+
}
96152

97153
public ApplicationSettings Settings { get; set; }
98154
private IConfiguration Configuration { get; }
99155
public IWebHostEnvironment Environment { get; }
156+
157+
private const string EdgeDbConnStrTemplate = "edgedb://{0}:{1}@{2}:{3}/{4}";
100158
}
101159
}

WeatherControl/Wissance.WeatherControl.WebApi.V2/appsettings.Development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"Application": {
1010
"Database": {
11-
"ConnStr": "edgedb://edgedb:VcJjK6blkKAV2MUTdJXzLPvS@localhost:10702/edgedb"
11+
"ProjectName": "Wissance_WeatherControl_EdgeDb"
1212
}
1313
}
1414
}

0 commit comments

Comments
 (0)