Skip to content

Commit

Permalink
Merge pull request #622 from bcgov/2.0.5
Browse files Browse the repository at this point in the history
Add date to timestamp for logging, update appsettings in configmaps
  • Loading branch information
bcgov-brwang authored May 18, 2023
2 parents 1623039 + 0e1dba2 commit 02c17c1
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 26 deletions.
5 changes: 0 additions & 5 deletions .pipeline/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { OpenShiftClientX } = require("@bcgov/pipeline-cli");
const path = require("path");

const util = require("./utils");
const KeyCloakClient = require("./keycloak");

module.exports = (settings) => {
const phases = settings.phases;
Expand All @@ -18,16 +17,12 @@ module.exports = (settings) => {
path.resolve(__dirname, "../../openshift")
);
var objects = [];
const kc = new KeyCloakClient(settings, oc);

const dbSecret = util.getSecret(
oc,
phases[phase].namespace,
`${phases[phase].name}-db-${phases[phase].phase}`
);

kc.addUris();

if (!dbSecret) {
console.log("Adding Db postgresql secret");

Expand Down
14 changes: 10 additions & 4 deletions Server/SchoolBusAPI/DbAppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using SchoolBusAPI.Extensions;
using SchoolBusAPI.ViewModels;
using SchoolBusCommon;
Expand All @@ -32,16 +33,18 @@ public class DbAppContextFactory : IDbAppContextFactory
{
DbContextOptions<DbAppContext> _options;
IHttpContextAccessor _httpContextAccessor;
ILogger<DbAppContext> _logger;

public DbAppContextFactory(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options)
public DbAppContextFactory(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options, ILogger<DbAppContext> logger)
{
_options = options;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}

public IDbAppContext Create()
{
return new DbAppContext(_httpContextAccessor, _options);
return new DbAppContext(_httpContextAccessor, _options, _logger);
}
}

Expand Down Expand Up @@ -88,14 +91,16 @@ public interface IDbAppContext
public class DbAppContext : DbContext, IDbAppContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<DbAppContext> _logger;

/// <summary>
/// Constructor for Class used for Entity Framework access.
/// </summary>
public DbAppContext(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options)
public DbAppContext(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options, ILogger<DbAppContext> logger)
: base(options)
{
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -335,7 +340,8 @@ public override int SaveChanges()
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
string exceptionMessage = e.ToString();
_logger.LogError($"DbAppContext exception: {exceptionMessage}");
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Server/SchoolBusAPI/SchoolBusApi.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions Server/SchoolBusAPI/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Http;
using AutoMapper;
using Microsoft.Extensions.Logging;

namespace SchoolBusAPI.Services
{
Expand All @@ -29,13 +30,16 @@ public interface IEmailService
public class EmailService : ServiceBase, IEmailService
{
private readonly IConfiguration Configuration;
private readonly ILogger<EmailService> _logger;

/// <summary>
/// Create a email service
/// </summary>
public EmailService(DbAppContext context, IConfiguration configuration, IHttpContextAccessor httpContextAccessor, IMapper mapper) : base(httpContextAccessor, context, mapper)
public EmailService(DbAppContext context, IConfiguration configuration, IHttpContextAccessor httpContextAccessor, IMapper mapper, ILogger<EmailService> logger)
: base(httpContextAccessor, context, mapper)
{
Configuration = configuration;
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -97,16 +101,16 @@ public virtual IActionResult EmailSend(Email email)
{
client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
Console.WriteLine("Starting certificate validation.");
_logger.LogInformation("Starting certificate validation.");
if (sslPolicyErrors == SslPolicyErrors.None)
{
Console.WriteLine("Cerficiate valid");
_logger.LogInformation("Certificate valid");
return true;
}

if (chain.ChainElements == null || chain.ChainElements.Count == 0)
{
Console.WriteLine("No certificates found in chain.");
_logger.LogError("No certificates found in chain.");
return false;
}

Expand All @@ -120,19 +124,19 @@ public virtual IActionResult EmailSend(Email email)
{
email.mailSent = false;
email.errorInfo = "Unable to process the SSL Certificate. Certificate may be untrusted, or the server does not accept SSL.";
Console.WriteLine($"Unable to process the SSL Certificate. Certificate may be untrusted, or the server does not accept SSL.");
_logger.LogError($"Unable to process the SSL Certificate. Certificate may be untrusted, or the server does not accept SSL.");
return new ObjectResult(email);
}
catch (Exception ex)
{
email.mailSent = false;
email.errorInfo = $"Unknown error occurred: ({ex.GetType().ToString()}) {ex.Message}.";
Console.WriteLine($"Unknown error occurred: ({ex.GetType().ToString()}) {ex.Message}");
_logger.LogError($"Unknown error occurred: ({ex.GetType().ToString()}) {ex.Message}");
return new ObjectResult(email);
}

client.Send(emailMessage);
Console.WriteLine("Email sent.");
_logger.LogInformation("Email sent.");
client.Disconnect(true);

email.mailSent = true;
Expand Down
12 changes: 9 additions & 3 deletions Server/SchoolBusAPI/Services/SchoolBusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Net.Http;
using System.Text;
using AutoMapper;
using Microsoft.Extensions.Logging;

namespace SchoolBusAPI.Services
{
Expand Down Expand Up @@ -170,14 +171,17 @@ public class SchoolBusService : ServiceBase, ISchoolBusService
{
private readonly DbAppContext _context;
private readonly IConfiguration Configuration;
private readonly ILogger<SchoolBusService> _logger;

/// <summary>
/// Create a service and set the database context
/// </summary>
public SchoolBusService(IHttpContextAccessor httpContextAccessor, IConfiguration configuration, DbAppContext context, IMapper mapper) : base(httpContextAccessor, context, mapper)
public SchoolBusService(IHttpContextAccessor httpContextAccessor, IConfiguration configuration, DbAppContext context, IMapper mapper, ILogger<SchoolBusService> logger)
: base(httpContextAccessor, context, mapper)
{
_context = context;
Configuration = configuration;
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -666,7 +670,8 @@ public virtual IActionResult SchoolbusesIdPdfpermitGetAsync(int id)
catch (Exception e)
{
result = null;
Console.WriteLine(e.ToString());
string exceptionMessage = e.ToString();
_logger.LogError($"SchoolbusesIdPdfpermitGetAsync exception: {exceptionMessage}");
}

finally
Expand All @@ -679,7 +684,8 @@ public virtual IActionResult SchoolbusesIdPdfpermitGetAsync(int id)
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
string exceptionMessage = e.ToString();
_logger.LogError($"SchoolbusesIdPdfpermitGetAsync exception: {exceptionMessage}");
}
}

Expand Down
3 changes: 2 additions & 1 deletion Server/SchoolBusAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ private DbAppContextFactory CreateDbAppContextFactory(IServiceProvider servicePr
{
DbContextOptionsBuilder<DbAppContext> options = new DbContextOptionsBuilder<DbAppContext>();
options.UseNpgsql(GetConnectionString());
DbAppContextFactory dbAppContextFactory = new DbAppContextFactory(null, options.Options);
var logger = serviceProvider.GetRequiredService<ILogger<DbAppContext>>();
DbAppContextFactory dbAppContextFactory = new DbAppContextFactory(null, options.Options, logger);
return dbAppContextFactory;
}

Expand Down
9 changes: 8 additions & 1 deletion Server/SchoolBusAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
{
"Name": "Async",
"Args": {
"configure": [{ "Name": "Console" }]
"configure": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
}
}
]
}
},
{
Expand Down
9 changes: 7 additions & 2 deletions openshift/configmaps/api-appsettings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ objects:
"Name": "Async",
"Args": {
"configure": [
{ "Name": "Console" }
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
}
}
]
}
},
Expand All @@ -46,7 +51,7 @@ objects:
"Schoolbus": "Host=localhost;Username=test1;Password=test1;Database=test1"
},
"JWT": {
"Authority": "https://dev.oidc.gov.bc.ca/auth/realms/<realmid>",
"Authority": "https://dev.loginproxy.gov.bc.ca/auth/realms/<realmid>",
"Audience": "<app-id>"
},
"CCW_USER_ID": "user",
Expand Down

0 comments on commit 02c17c1

Please sign in to comment.