Skip to content

Commit

Permalink
edgedb configurator used
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilLord666 committed Aug 30, 2023
1 parent 7d60ff9 commit daac831
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EdgeDB.Net.Driver" Version="1.0.4" />
<PackageReference Include="EdgeDB.Net.Driver" Version="1.2.3" />
<PackageReference Include="Wissance.WebApiToolkit" Version="1.3.0" />
</ItemGroup>

Expand Down
79 changes: 9 additions & 70 deletions WeatherControl/Wissance.WeatherControl.WebApi.V2/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using EdgeDB;
using Microsoft.AspNetCore.Routing.Internal;
using Newtonsoft.Json;
using Wissance.EdgeDb.Configurator;
using Wissance.WeatherControl.WebApi.V2.Config;
using Wissance.WeatherControl.WebApi.V2.Data;

Expand Down Expand Up @@ -72,84 +73,22 @@ private void ConfigureLogging(IServiceCollection services)

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

services.AddEdgeDB(conn, cfg =>
EdgeDBClientPoolConfig poolCfg = new EdgeDBClientPoolConfig()
{
cfg.ClientType = EdgeDBClientType.Tcp;
cfg.SchemaNamingStrategy = INamingStrategy.CamelCaseNamingStrategy;
cfg.DefaultPoolSize = 256;
cfg.ConnectionTimeout = 5000;
cfg.MessageTimeout = 10000;
});
ClientType = EdgeDBClientType.Tcp,
SchemaNamingStrategy = INamingStrategy.CamelCaseNamingStrategy,
DefaultPoolSize = 256,
ConnectionTimeout = 5000,
MessageTimeout = 10000
};
services.ConfigureEdgeDbDatabase(Settings.Database.ProjectName, poolCfg);
}

private void ConfigureWebApi(IServiceCollection services)
{
services.AddControllers();
}

/*private void ConfigureManagers(IServiceCollection services)
{
// services.AddScoped<StationManager>();
// services.AddScoped<MeasurementsManager>();
}*/

private string GetEdgeDbConnStrByProjectName(string projectName, ILoggerFactory loggerFactory)
{
ILogger<Startup> logger = loggerFactory.CreateLogger<Startup>();
try
{
string projectCredentialsFile = GetEdgeDbProjectCredentialFile(projectName);
string content = File.ReadAllText(projectCredentialsFile);
EdgeDbProjectCredentials credentials = JsonConvert.DeserializeObject<EdgeDbProjectCredentials>(content);
//JsonSerializer.Deserialize<EdgeDbProjectCredentials>(content);
return string.Format(EdgeDbConnStrTemplate, credentials.User, credentials.Password, "localhost",
credentials.Port,
credentials.Database);
}
catch (Exception e)
{
logger.LogError($"An error occurred during attempt to build edgedb connection str: {e.Message}");
return string.Empty;
}
}

private string GetEdgeDbProjectCredentialFile(string projectName)
{
string projectCredentialsFile = string.Empty;
if (OperatingSystem.IsWindows())
{
// should be using ~AppData\Local\EdgeDB\config\credentials\{projectName}.json as a path
string appData = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
projectCredentialsFile = Path.Combine(new string[]
{
appData,
"EdgeDB", "config", "credentials",
projectName + ".json"
});
}

if (OperatingSystem.IsLinux())
{
// linux - /.config/edgedb/credentials/{projectName}.json relative to home dir
string home = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
projectCredentialsFile = Path.Combine(new string[]
{
home,
".config", "edgedb", "credentials",
projectName + ".json"
});
}

return projectCredentialsFile;
}

public ApplicationSettings Settings { get; set; }
private IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EdgeDB.Net.Driver" Version="1.1.2" />
<PackageReference Include="EdgeDB.Net.Driver" Version="1.2.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Wissance.EdgeDb.Configurator" Version="1.0.0" />
<PackageReference Include="Wissance.WebApiToolkit" Version="1.3.0" />
</ItemGroup>

Expand Down

0 comments on commit daac831

Please sign in to comment.