-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStartup.cs
107 lines (94 loc) · 4.18 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// NOTE: This example use template code prior Piranha version 10.0.3 (old .NET startup / hosting model)
// For template code Piranha version 10.0.3 and up (.NET 6 compact startup / minimal hosting model): see the other example: Program.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Piranha;
using Piranha.AttributeBuilder;
using Piranha.AspNetCore.Identity.SQLite;
using Piranha.Data.EF.SQLite;
using Piranha.Manager.Editor;
using Piranha.Models;
using Piranha.Runtime;
using Zon3.SpamDetector; // <-- 1. Reference to SpamDetector (add source using project reference or add nuget package)
namespace RazorWeb
{
public class Startup
{
private readonly IConfiguration _config;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="configuration">The current configuration</param>
public Startup(IConfiguration configuration)
{
_config = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// Service setup
services.AddPiranha(options =>
{
options.AddRazorRuntimeCompilation = true;
options.UseFileStorage(naming: Piranha.Local.FileStorageNaming.UniqueFolderNames);
options.UseImageSharp();
options.UseManager();
options.UseTinyMCE();
options.UseMemoryCache();
options.UseEF<SQLiteDb>(db =>
db.UseSqlite(_config.GetConnectionString("piranha")));
options.UseIdentityWithSeed<IdentitySQLiteDb>(db =>
db.UseSqlite(_config.GetConnectionString("piranha")));
options.UseSpamDetector(); // <-- 2. SpamDetector service registered
/***
* Here you can configure the different permissions
* that you want to use for securing content in the
* application.
options.UseSecurity(o =>
{
o.UsePermission("WebUser", "Web User");
});
*/
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApi api)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Initialize Piranha
App.Init(api);
// Build content types
new ContentTypeBuilder(api)
.AddAssembly(typeof(Startup).Assembly)
.Build()
.DeleteOrphans();
// Configure Tiny MCE
EditorConfig.FromFile("editorconfig.json");
// Middleware setup
app.UsePiranha(options => {
options.UseManager();
options.UseTinyMCE();
options.UseIdentity();
options.UseSpamDetector(); // <-- 3. SpamDetector service configured
});
// 4. Add hook. SpamDetector use OnValidate hook to moderate comments
App.Hooks.Comments.RegisterOnValidate(c =>
{
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
var spamDetector = serviceScope.ServiceProvider.GetRequiredService<SpamDetectorService>();
// SpamDetector will call Akismet API to review the comment and then return the result
var reviewResult = spamDetector.ReviewAsync(c).Result;
// This updates the comment submitted with the result of the antispam review
c.IsApproved = reviewResult.Approved;
});
}
}
}