|
6 | 6 | using System; |
7 | 7 | using System.Collections.Generic; |
8 | 8 | using System.Globalization; |
| 9 | +using System.IO; |
9 | 10 | using System.Linq; |
10 | 11 | using System.Threading.Tasks; |
11 | 12 | using Microsoft.Extensions.Logging; |
12 | 13 | using Serilog; |
13 | 14 | using EdgeDB; |
14 | 15 | using Microsoft.AspNetCore.Routing.Internal; |
| 16 | +using Newtonsoft.Json; |
15 | 17 | using Wissance.WeatherControl.WebApi.V2.Config; |
| 18 | +using Wissance.WeatherControl.WebApi.V2.Data; |
16 | 19 |
|
17 | 20 |
|
18 | 21 | namespace Wissance.WeatherControl.WebApi.V2 |
@@ -69,33 +72,88 @@ private void ConfigureLogging(IServiceCollection services) |
69 | 72 |
|
70 | 73 | private void ConfigureDatabase(IServiceCollection services) |
71 | 74 | { |
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); |
73 | 80 | conn.TLSSecurity = TLSSecurityMode.Insecure; |
74 | 81 |
|
75 | 82 | services.AddEdgeDB(conn, cfg => |
76 | 83 | { |
77 | 84 | cfg.ClientType = EdgeDBClientType.Tcp; |
| 85 | + cfg.SchemaNamingStrategy = INamingStrategy.CamelCaseNamingStrategy; |
78 | 86 | cfg.DefaultPoolSize = 256; |
79 | | - cfg.ConnectionTimeout = 3000; |
80 | | - cfg.MessageTimeout = 5000; |
| 87 | + cfg.ConnectionTimeout = 5000; |
| 88 | + cfg.MessageTimeout = 10000; |
81 | 89 | }); |
82 | 90 | } |
83 | 91 |
|
84 | 92 | private void ConfigureWebApi(IServiceCollection services) |
85 | 93 | { |
86 | 94 | services.AddControllers(); |
87 | | - |
88 | | - // ConfigureManagers(services); |
89 | 95 | } |
90 | 96 |
|
91 | 97 | /*private void ConfigureManagers(IServiceCollection services) |
92 | 98 | { |
93 | 99 | // services.AddScoped<StationManager>(); |
94 | 100 | // services.AddScoped<MeasurementsManager>(); |
95 | 101 | }*/ |
| 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 | + } |
96 | 152 |
|
97 | 153 | public ApplicationSettings Settings { get; set; } |
98 | 154 | private IConfiguration Configuration { get; } |
99 | 155 | public IWebHostEnvironment Environment { get; } |
| 156 | + |
| 157 | + private const string EdgeDbConnStrTemplate = "edgedb://{0}:{1}@{2}:{3}/{4}"; |
100 | 158 | } |
101 | 159 | } |
0 commit comments