Skip to content

Commit

Permalink
Upgrade to use Endpoints, remove use of Funq, show Swagger/OpenAPI v3…
Browse files Browse the repository at this point in the history
… when developing locally.
  • Loading branch information
Layoric committed Feb 5, 2024
1 parent ad47876 commit 8724a3d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 36 deletions.
31 changes: 11 additions & 20 deletions MyApp.ServiceInterface/EmailServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,24 @@ public class SmtpConfig
/// <summary>
/// Uses a configured SMTP client to send emails
/// </summary>
public class EmailServices : Service
public class EmailServices(SmtpConfig config, ILogger<EmailServices> log)
// TODO: Uncomment to enable sending emails with SMTP
// : Service
{
public EmailServices(SmtpConfig config, ILogger<EmailServices> log)
{
Config = config;
Log = log;
}

public SmtpConfig Config { get; }
public ILogger<EmailServices> Log { get; }

/* Uncomment to enable sending emails with SMTP
public object Any(SendEmail request)
{
Log.LogInformation("Sending email to {Email} with subject {Subject}", request.To, request.Subject);
log.LogInformation("Sending email to {Email} with subject {Subject}", request.To, request.Subject);

using var client = new SmtpClient(Config.Host, Config.Port);
client.Credentials = new System.Net.NetworkCredential(Config.Username, Config.Password);
using var client = new SmtpClient(config.Host, config.Port);
client.Credentials = new System.Net.NetworkCredential(config.Username, config.Password);
client.EnableSsl = true;

// If DevToEmail is set, send all emails to that address instead
var emailTo = Config.DevToEmail != null
? new MailAddress(Config.DevToEmail)
var emailTo = config.DevToEmail != null
? new MailAddress(config.DevToEmail)
: new MailAddress(request.To, request.ToName);

var emailFrom = new MailAddress(Config.FromEmail, Config.FromName);
var emailFrom = new MailAddress(config.FromEmail, config.FromName);

var msg = new MailMessage(emailFrom, emailTo)
{
Expand All @@ -82,14 +74,13 @@ public object Any(SendEmail request)
IsBodyHtml = request.BodyHtml != null,
};

if (Config.Bcc != null)
if (config.Bcc != null)
{
msg.Bcc.Add(new MailAddress(Config.Bcc));
msg.Bcc.Add(new MailAddress(config.Bcc));
}

client.Send(msg);

return new EmptyResponse();
}
*/
}
2 changes: 1 addition & 1 deletion MyApp.ServiceModel/Hello.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MyApp.ServiceModel;

[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
public class Hello : IGet, IReturn<HelloResponse>
{
public string? Name { get; set; }
}
Expand Down
4 changes: 2 additions & 2 deletions MyApp/Configure.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace MyApp;
public class ConfigureAuth : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureAppHost(appHost =>
.ConfigureServices((context, services) =>
{
appHost.Plugins.Add(new AuthFeature(IdentityAuth.For<ApplicationUser>(options => {
services.AddPlugin(new AuthFeature(IdentityAuth.For<ApplicationUser>(options => {
options.EnableCredentialsAuth = true;
options.SessionFactory = () => new CustomUserSession();
})));
Expand Down
13 changes: 5 additions & 8 deletions MyApp/Configure.AutoQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services => {
// Enable Audit History
services.AddSingleton<ICrudEvents>(c =>
new OrmLiteCrudEvents(c.Resolve<IDbConnectionFactory>()));
})
.ConfigureAppHost(appHost => {

new OrmLiteCrudEvents(c.GetRequiredService<IDbConnectionFactory>()));
// For TodosService
appHost.Plugins.Add(new AutoQueryDataFeature());

services.AddPlugin(new AutoQueryDataFeature());
// For Bookings https://docs.servicestack.net/autoquery-crud-bookings
appHost.Plugins.Add(new AutoQueryFeature
services.AddPlugin(new AutoQueryFeature
{
MaxLimit = 1000,
//IncludeTotal = true,
});

})
.ConfigureAppHost(appHost => {
appHost.Resolve<ICrudEvents>().InitSchema();
});
}
5 changes: 2 additions & 3 deletions MyApp/Configure.Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public void Configure(IWebHostBuilder builder) => builder
context.Configuration.GetConnectionString("DefaultConnection")
?? ":memory:",
SqliteDialect.Provider));
})
.ConfigureAppHost(appHost => {

// Enable built-in Database Admin UI at /admin-ui/database
appHost.Plugins.Add(new AdminDatabaseFeature());
services.AddPlugin(new AdminDatabaseFeature());
});
}
4 changes: 3 additions & 1 deletion MyApp/Configure.Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public void Configure(IWebHostBuilder builder) => builder
services.AddSingleton<MarkdownPages>();
services.AddSingleton<MarkdownVideos>();
services.AddSingleton<MarkdownBlog>();

// Plugins
services.AddPlugin(new CleanUrlsFeature());
})
.ConfigureAppHost(
appHost => appHost.Plugins.Add(new CleanUrlsFeature()),
afterPluginsLoaded: appHost =>
{
var pages = appHost.Resolve<MarkdownPages>();
Expand Down
1 change: 1 addition & 0 deletions MyApp/MyApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<PackageReference Include="ServiceStack" Version="8.*" />
<PackageReference Include="ServiceStack.AspNetCore.OpenApi" Version="8.*" />
<PackageReference Include="ServiceStack.Blazor" Version="8.*" />
<PackageReference Include="ServiceStack.Mvc" Version="8.*" />
<PackageReference Include="ServiceStack.OrmLite.Sqlite.Data" Version="8.*" />
Expand Down
19 changes: 18 additions & 1 deletion MyApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using MyApp.Components;
using MyApp.Data;
using MyApp.Components.Account;
using MyApp.ServiceInterface;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -57,12 +58,25 @@
services.AddBlazorServerIdentityApiClient(baseUrl);
services.AddLocalStorage();

services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

// Register all services
services.AddServiceStack(typeof(MyServices).Assembly, c => {
c.AddSwagger(o => {
//o.AddJwtBearer();
o.AddBasicAuth();
});
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
Expand All @@ -81,7 +95,10 @@
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();

app.UseServiceStack(new AppHost());
app.UseServiceStack(new AppHost(), options =>
{
options.MapEndpoints();
});

BlazorConfig.Set(new()
{
Expand Down
7 changes: 7 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="ServiceStack Pre-Release" value="https://f.feedz.io/servicestack/pre-release/nuget/index.json" />
</packageSources>
</configuration>

0 comments on commit 8724a3d

Please sign in to comment.