-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from argon-chat/feature/fluid_and_email_forms
Feature/fluid and email forms
- Loading branch information
Showing
9 changed files
with
260 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace Argon.Api.Features.Template; | ||
|
||
using System.Collections.Concurrent; | ||
using Fluid; | ||
|
||
public static class TemplateFeature | ||
{ | ||
public static IServiceCollection AddTemplateEngine(this WebApplicationBuilder builder) | ||
{ | ||
builder.Services.AddSingleton<FluidParser>(); | ||
builder.Services.AddHostedService<EMailFormLoader>(); | ||
builder.Services.AddSingleton<EMailFormStorage>(); | ||
return builder.Services; | ||
} | ||
} | ||
|
||
public class EMailFormStorage | ||
{ | ||
private readonly ConcurrentDictionary<string, IFluidTemplate> htmlForms = new(); | ||
|
||
public void Load(string name, IFluidTemplate template) => htmlForms.TryAdd(name, template); | ||
|
||
public IFluidTemplate GetContentFor(string formKey) | ||
{ | ||
if (htmlForms.TryGetValue(formKey, out var form)) | ||
return form; | ||
throw new InvalidOperationException($"No '{formKey}' form found"); | ||
} | ||
|
||
public string Render(string formKey, Dictionary<string, string> values) | ||
{ | ||
var template = GetContentFor(formKey); | ||
|
||
var context = new TemplateContext(); | ||
|
||
foreach (var (key, value) in values) | ||
context.SetValue(key, value); | ||
|
||
return template.Render(context); | ||
} | ||
} | ||
|
||
public class EMailFormLoader(EMailFormStorage storage, ILogger<EMailFormLoader> logger, FluidParser engine) : BackgroundService | ||
{ | ||
protected async override Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
var formFiles = Directory.EnumerateFiles("./Resources", "*.html").ToList(); | ||
|
||
logger.LogInformation("Found '{count}' email forms", formFiles.Count); | ||
|
||
foreach (var file in formFiles) | ||
{ | ||
var content = await File.ReadAllTextAsync(file, stoppingToken); | ||
var name = Path.GetFileNameWithoutExtension(file); | ||
|
||
if (engine.TryParse(content, out var result, out var error)) | ||
{ | ||
storage.Load(name, result); | ||
logger.LogInformation("Loaded '{name}' email form", name); | ||
} | ||
else | ||
logger.LogError("Failed load '{name}' email form, error: {error}", name, error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta content="width=device-width, initial-scale=1.0" name="viewport"> | ||
<title>Login to Your Account</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #1a1a1a; | ||
margin: 0; | ||
padding: 0; | ||
color: #e0e0e0; | ||
} | ||
|
||
.container { | ||
max-width: 600px; | ||
margin: 0 auto; | ||
background-color: #2a2a2a; | ||
padding: 20px; | ||
border-radius: 8px; | ||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5); | ||
} | ||
|
||
.header { | ||
font-size: 24px; | ||
font-weight: bold; | ||
text-align: center; | ||
margin-bottom: 20px; | ||
color: #bb86fc; | ||
} | ||
|
||
.message { | ||
font-size: 16px; | ||
color: #cccccc; | ||
margin-bottom: 20px; | ||
line-height: 1.5; | ||
} | ||
|
||
.button { | ||
display: inline-block; | ||
font-size: 18px; | ||
font-weight: bold; | ||
padding: 10px 20px; | ||
color: #ffffff; | ||
background-color: #6200ea; | ||
border-radius: 4px; | ||
text-decoration: none; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.footer { | ||
font-size: 14px; | ||
color: #888888; | ||
text-align: center; | ||
margin-top: 20px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="header">Login to Argon</div> | ||
<div class="message"> | ||
Hello,<br><br> | ||
You have successfully logged in to your account. Your session details are as follows: | ||
</div> | ||
<div style="text-align: center;"> | ||
<div class="message"> | ||
IP: <strong>{{ user_ip }}</strong><br> | ||
Region: <strong>{{ user_region }}</strong><br> | ||
Host: <strong>{{ user_host }}</strong><br> | ||
Login Time: <strong>{{ login_time }}</strong> | ||
</div> | ||
</div> | ||
<div class="message"> | ||
If this login was not made by you, please contact our support team immediately. | ||
</div> | ||
<div class="footer"> | ||
Thank you,<br> | ||
The Argon Team | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.