From 5e02abc95cc8419b378917d7053d63092046b2cf Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 14 Jun 2020 16:34:42 +0800 Subject: Many many bugs fix. 1. Add a way to test front end with mock page. 2. Unknown api returns 400 but not frontend page. 3. Fix a big bug that cause all data loss in database migration. --- Timeline/Configs/ApplicationConfiguration.cs | 1 + .../20200614061237_AddTimelineUniqueId.cs | 47 +++++++++++++++++++++- Timeline/MockClientApp/index.html | 10 +++++ Timeline/Models/Http/ErrorResponse.cs | 10 +++++ Timeline/Properties/launchSettings.json | 8 ++++ Timeline/Resources/Messages.Designer.cs | 9 +++++ Timeline/Resources/Messages.resx | 3 ++ Timeline/Routes/UnknownEndpointMiddleware.cs | 39 ++++++++++++++++++ Timeline/Startup.cs | 29 +++++++++---- Timeline/Timeline.csproj | 7 ++++ 10 files changed, 155 insertions(+), 8 deletions(-) create mode 100644 Timeline/MockClientApp/index.html create mode 100644 Timeline/Routes/UnknownEndpointMiddleware.cs (limited to 'Timeline') diff --git a/Timeline/Configs/ApplicationConfiguration.cs b/Timeline/Configs/ApplicationConfiguration.cs index fec7f06c..c84327d7 100644 --- a/Timeline/Configs/ApplicationConfiguration.cs +++ b/Timeline/Configs/ApplicationConfiguration.cs @@ -7,5 +7,6 @@ public const string DatabaseFileName = "timeline.db"; public const string DisableFrontEndKey = "DisableFrontEnd"; public const string FrontEndProxyOnlyKey = "FrontEndProxyOnly"; + public const string UseMockFrontEndKey = "UseMockFrontEnd"; } } diff --git a/Timeline/Migrations/20200614061237_AddTimelineUniqueId.cs b/Timeline/Migrations/20200614061237_AddTimelineUniqueId.cs index 80e90dbf..1fc3de18 100644 --- a/Timeline/Migrations/20200614061237_AddTimelineUniqueId.cs +++ b/Timeline/Migrations/20200614061237_AddTimelineUniqueId.cs @@ -9,6 +9,8 @@ namespace Timeline.Migrations migrationBuilder.Sql( @" ALTER TABLE timelines RENAME TO timelines_backup; +ALTER TABLE timeline_members RENAME TO timeline_members_backup; +ALTER TABLE timeline_posts RENAME TO timeline_posts_backup; CREATE TABLE timelines ( id INTEGER NOT NULL CONSTRAINT PK_timelines PRIMARY KEY AUTOINCREMENT, @@ -21,10 +23,53 @@ CREATE TABLE timelines ( CONSTRAINT FK_timelines_users_owner FOREIGN KEY (owner) REFERENCES users (id) ON DELETE CASCADE ); +CREATE TABLE timeline_members ( + id INTEGER NOT NULL + CONSTRAINT PK_timeline_members PRIMARY KEY AUTOINCREMENT, + user INTEGER NOT NULL, + timeline INTEGER NOT NULL, + CONSTRAINT FK_timeline_members_timelines_timeline FOREIGN KEY ( + timeline + ) + REFERENCES timelines (id) ON DELETE CASCADE, + CONSTRAINT FK_timeline_members_users_user FOREIGN KEY ( + user + ) + REFERENCES users (id) ON DELETE CASCADE +); + +CREATE TABLE timeline_posts ( + id INTEGER NOT NULL + CONSTRAINT PK_timeline_posts PRIMARY KEY AUTOINCREMENT, + timeline INTEGER NOT NULL, + author INTEGER NOT NULL, + content TEXT, + time TEXT NOT NULL, + last_updated TEXT NOT NULL, + local_id INTEGER NOT NULL + DEFAULT 0, + content_type TEXT NOT NULL + DEFAULT '', + extra_content TEXT, + CONSTRAINT FK_timeline_posts_users_author FOREIGN KEY ( + author + ) + REFERENCES users (id) ON DELETE CASCADE, + CONSTRAINT FK_timeline_posts_timelines_timeline FOREIGN KEY ( + timeline + ) + REFERENCES timelines (id) ON DELETE CASCADE +); + + INSERT INTO timelines (id, name, description, owner, visibility, create_time) SELECT id, name, description, owner, visibility, create_time FROM timelines_backup; - +INSERT INTO timeline_members SELECT * FROM timeline_members_backup; +INSERT INTO timeline_posts SELECT * FROM timeline_posts_backup; + DROP TABLE timelines_backup; +DROP TABLE timeline_members_backup; +DROP TABLE timeline_posts_backup; " ); } diff --git a/Timeline/MockClientApp/index.html b/Timeline/MockClientApp/index.html new file mode 100644 index 00000000..03cf371e --- /dev/null +++ b/Timeline/MockClientApp/index.html @@ -0,0 +1,10 @@ + + + + + Mock Client App + + + This is a mock client app for testing. + + diff --git a/Timeline/Models/Http/ErrorResponse.cs b/Timeline/Models/Http/ErrorResponse.cs index bb9c44df..9a4d190a 100644 --- a/Timeline/Models/Http/ErrorResponse.cs +++ b/Timeline/Models/Http/ErrorResponse.cs @@ -30,6 +30,16 @@ namespace Timeline.Models.Http return new CommonResponse(ErrorCodes.Common.Forbid, string.Format(message, formatArgs)); } + public static CommonResponse UnknownEndpoint(params object?[] formatArgs) + { + return new CommonResponse(ErrorCodes.Common.UnknownEndpoint, string.Format(Common_UnknownEndpoint, formatArgs)); + } + + public static CommonResponse CustomMessage_UnknownEndpoint(string message, params object?[] formatArgs) + { + return new CommonResponse(ErrorCodes.Common.UnknownEndpoint, string.Format(message, formatArgs)); + } + public static class Header { diff --git a/Timeline/Properties/launchSettings.json b/Timeline/Properties/launchSettings.json index 4baafa62..d23d132f 100644 --- a/Timeline/Properties/launchSettings.json +++ b/Timeline/Properties/launchSettings.json @@ -8,6 +8,14 @@ "ASPNETCORE_WORKDIR": "D:\\timeline-development" } }, + "Timeline-MockFrontEnd": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_USEMOCKFRONTEND": "true", + "ASPNETCORE_WORKDIR": "D:\\timeline-development" + } + }, "Timeline-Staging": { "commandName": "Project", "environmentVariables": { diff --git a/Timeline/Resources/Messages.Designer.cs b/Timeline/Resources/Messages.Designer.cs index 40c4a1ce..bb654ce6 100644 --- a/Timeline/Resources/Messages.Designer.cs +++ b/Timeline/Resources/Messages.Designer.cs @@ -150,6 +150,15 @@ namespace Timeline.Resources { } } + /// + /// Looks up a localized string similar to The api endpoint you request is unknown. You might get the wrong api entry.. + /// + internal static string Common_UnknownEndpoint { + get { + return ResourceManager.GetString("Common_UnknownEndpoint", resourceCulture); + } + } + /// /// Looks up a localized string similar to Unknown type of post content.. /// diff --git a/Timeline/Resources/Messages.resx b/Timeline/Resources/Messages.resx index 8d5543fe..2bbf494e 100644 --- a/Timeline/Resources/Messages.resx +++ b/Timeline/Resources/Messages.resx @@ -147,6 +147,9 @@ Model is of bad format. + + The api endpoint you request is unknown. You might get the wrong api entry. + Unknown type of post content. diff --git a/Timeline/Routes/UnknownEndpointMiddleware.cs b/Timeline/Routes/UnknownEndpointMiddleware.cs new file mode 100644 index 00000000..25ec563c --- /dev/null +++ b/Timeline/Routes/UnknownEndpointMiddleware.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using System; +using System.Net.Mime; +using System.Text.Json; +using Timeline.Models.Http; + +namespace Timeline.Routes +{ + public static class UnknownEndpointMiddleware + { + public static void Attach(IApplicationBuilder app) + { + app.Use(async (context, next) => + { + if (context.GetEndpoint() != null) + { + await next(); + return; + } + + if (context.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase)) + { + context.Response.StatusCode = StatusCodes.Status400BadRequest; + context.Response.ContentType = MediaTypeNames.Application.Json; + + var body = JsonSerializer.SerializeToUtf8Bytes(ErrorResponse.Common.UnknownEndpoint(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); + + context.Response.ContentLength = body.Length; + await context.Response.Body.WriteAsync(body); + await context.Response.CompleteAsync(); + return; + } + + await next(); + }); + } + } +} diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 35c47712..918f025a 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -24,6 +24,7 @@ namespace Timeline public class Startup { private readonly bool disableFrontEnd; + private readonly bool useMockFrontEnd; public Startup(IConfiguration configuration, IWebHostEnvironment environment) { @@ -31,6 +32,7 @@ namespace Timeline Configuration = configuration; disableFrontEnd = Configuration.GetValue(ApplicationConfiguration.DisableFrontEndKey) ?? false; + useMockFrontEnd = Configuration.GetValue(ApplicationConfiguration.UseMockFrontEndKey) ?? false; } public IWebHostEnvironment Environment { get; } @@ -90,12 +92,23 @@ namespace Timeline options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); }); - if (!disableFrontEnd && !Environment.IsDevelopment()) + if (!disableFrontEnd) { - services.AddSpaStaticFiles(config => + if (useMockFrontEnd) { - config.RootPath = "ClientApp/dist"; - }); + services.AddSpaStaticFiles(config => + { + config.RootPath = "MockClientApp"; + }); + + } + else if (!Environment.IsDevelopment()) // In development, we don't want to serve dist. Or it will take precedence than front end dev server. + { + services.AddSpaStaticFiles(config => + { + config.RootPath = "ClientApp/dist"; + }); + } } } @@ -120,7 +133,7 @@ namespace Timeline app.UseRouting(); - if (!disableFrontEnd && !Environment.IsDevelopment()) + if (!disableFrontEnd && (useMockFrontEnd || !Environment.IsDevelopment())) { app.UseSpaStaticFiles(new StaticFileOptions { @@ -136,13 +149,15 @@ namespace Timeline endpoints.MapControllers(); }); + UnknownEndpointMiddleware.Attach(app); + if (!disableFrontEnd) { app.UseSpa(spa => { - spa.Options.SourcePath = "ClientApp"; + spa.Options.SourcePath = useMockFrontEnd ? "MockClientApp" : "ClientApp"; - if (Environment.IsDevelopment()) + if (!useMockFrontEnd && Environment.IsDevelopment()) { if (Configuration.GetValue(ApplicationConfiguration.FrontEndProxyOnlyKey) ?? false) { diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj index 53fd3b71..dfaecf02 100644 --- a/Timeline/Timeline.csproj +++ b/Timeline/Timeline.csproj @@ -20,6 +20,9 @@ PreserveNewest + + PreserveNewest + @@ -68,6 +71,10 @@ + + + + True -- cgit v1.2.3