This commit is contained in:
Tommy Parnell
2022-11-09 11:43:42 -05:00
parent a5931f48c6
commit 0871e037d8
10 changed files with 72 additions and 61 deletions

2
.vscode/launch.json vendored
View File

@@ -9,7 +9,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/TerribleDev.Blog.Web/bin/Debug/netcoreapp3.1/TerribleDev.Blog.Web.dll",
"program": "${workspaceFolder}/src/TerribleDev.Blog.Web/bin/Debug/net7.0/TerribleDev.Blog.Web.dll",
"args": [],
"cwd": "${workspaceFolder}/src/TerribleDev.Blog.Web",
"stopAtEntry": false,

View File

@@ -11,7 +11,7 @@ COPY ./src/TerribleDev.Blog.Web/ .
RUN dotnet publish -c release -o /app -r linux-musl-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRunComposite=true /p:PublishSingleFile=true
RUN date +%s > /app/buildtime.txt
# final stage/image
FROM mcr.microsoft.com/dotnet/runtime-deps:6.0-alpine-amd64
FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine-amd64
WORKDIR /app
COPY --from=build /app ./

View File

@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
WORKDIR /app
# Copy everything else and build

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp6.0</TargetFramework>
<TargetFramework>netcoreapp7.0</TargetFramework>
<IsPackable>true</IsPackable>
<PackAsTool>true</PackAsTool>
<ToolCommandName>tempo</ToolCommandName>

View File

@@ -9,16 +9,19 @@ using System.IO;
using Microsoft.AspNetCore.Html;
using TerribleDev.Blog.Web.Filters;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.OutputCaching;
namespace TerribleDev.Blog.Web.Controllers
{
[Http2PushFilter]
public class HomeController : Controller
{
private readonly ILogger<HomeController> logger;
private readonly PostCache postCache;
public HomeController(PostCache postCache)
public HomeController(PostCache postCache, ILogger<HomeController> logger)
{
this.logger = logger;
this.postCache = postCache;
}
@@ -27,18 +30,26 @@ namespace TerribleDev.Blog.Web.Controllers
[Route("/index.html", Order = 2)]
[Route("/")]
[Route("/page/{pageNumber:required:int:min(1)}")]
[OutputCache(Duration = 31536000, VaryByParam = "pageNumber")]
[OutputCache(Duration = 31536000, VaryByRouteValueNames = new string[] { "pageNumber" })]
[ResponseCache(Duration = 900)]
public IActionResult Index(int pageNumber = 1)
{
if(!postCache.PostsByPage.TryGetValue(pageNumber, out var result))
this.logger.LogWarning("Viewing page", pageNumber);
if (!postCache.PostsByPage.TryGetValue(pageNumber, out var result))
{
return Redirect($"/404/?from=/page/{pageNumber}/");
}
return View(new HomeViewModel() { Posts = result, Page = pageNumber, HasNext = postCache.PostsByPage.ContainsKey(pageNumber + 1), HasPrevious = postCache.PostsByPage.ContainsKey(pageNumber - 1),
BlogLD = postCache.BlogLD,
SiteLD = postCache.SiteLD,
BlogLDString = postCache.BlogLDString, SiteLDString = postCache.SiteLDString });
return View(new HomeViewModel()
{
Posts = result,
Page = pageNumber,
HasNext = postCache.PostsByPage.ContainsKey(pageNumber + 1),
HasPrevious = postCache.PostsByPage.ContainsKey(pageNumber - 1),
BlogLD = postCache.BlogLD,
SiteLD = postCache.SiteLD,
BlogLDString = postCache.BlogLDString,
SiteLDString = postCache.SiteLDString
});
}
[Route("/theme/{postName?}")]
public IActionResult Theme(string postName)
@@ -60,34 +71,34 @@ namespace TerribleDev.Blog.Web.Controllers
}
[Route("{postUrl}/{amp?}")]
[OutputCache(Duration = 31536000, VaryByParam = "postUrl,amp")]
[OutputCache(Duration = 31536000, VaryByRouteValueNames = new string[] { "postUrl", "amp" })]
[ResponseCache(Duration = 900)]
public IActionResult Post(string postUrl, string amp = "")
{
if(!String.IsNullOrEmpty(amp) && amp != "amp")
if (!String.IsNullOrEmpty(amp) && amp != "amp")
{
return Redirect($"/404/?from=/{postUrl}/{amp}/");
}
var isAmp = amp == "amp";
if(isAmp)
if (isAmp)
{
return this.RedirectPermanent($"/{postUrl}");
}
// case sensitive lookup
if(postCache.UrlToPost.TryGetValue(postUrl, out var currentPost))
if (postCache.UrlToPost.TryGetValue(postUrl, out var currentPost))
{
return View("Post", model: new PostViewModel() { Post = currentPost });
return View("Post", model: new PostViewModel() { Post = currentPost });
}
// case insensitive lookup on post
if(postCache.CaseInsensitiveUrlToPost.TryGetValue(postUrl, out var caseInsensitivePost))
if (postCache.CaseInsensitiveUrlToPost.TryGetValue(postUrl, out var caseInsensitivePost))
{
return View("Post", model: new PostViewModel() { Post = caseInsensitivePost });
return View("Post", model: new PostViewModel() { Post = caseInsensitivePost });
}
if(postCache.LandingPagesUrl.TryGetValue(postUrl, out var landingPage))
if (postCache.LandingPagesUrl.TryGetValue(postUrl, out var landingPage))
{
return View("Post", model: new PostViewModel() { Post = landingPage });
return View("Post", model: new PostViewModel() { Post = landingPage });
}
this.StatusCode(404);
return View(nameof(FourOhFour));
}

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.SyndicationFeed;
using Microsoft.SyndicationFeed.Rss;
using TerribleDev.Blog.Web.Models;

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using TerribleDev.Blog.Web.Filters;
using TerribleDev.Blog.Web.Models;
@@ -24,7 +25,7 @@ namespace TerribleDev.Blog.Web.Controllers
return View(postCache.TagsToPosts);
}
[Route("/tags/{tagName}")]
[OutputCache(Duration = 31536000, VaryByParam = "tagName")]
[OutputCache(Duration = 31536000, VaryByRouteValueNames = new string[]{"tagName"})]
public IActionResult TagPluralRedirect(string tagName)
{
if(string.IsNullOrEmpty(tagName))
@@ -34,7 +35,7 @@ namespace TerribleDev.Blog.Web.Controllers
return Redirect($"/tag/{tagName}/");
}
[Route("/tag/{tagName}")]
[OutputCache(Duration = 31536000, VaryByParam = "tagName")]
[OutputCache(Duration = 31536000, VaryByRouteValueNames = new string[] {"tagName"})]
public IActionResult GetTag(string tagName)
{
if(!postCache.TagsToPosts.TryGetValue(tagName.ToLower(), out var models))

View File

@@ -1,5 +1,5 @@
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
WORKDIR /source
# copy csproj and restore as distinct layers
@@ -11,7 +11,7 @@ COPY . .
RUN dotnet publish -c release -o /app -r linux-musl-x64 --self-contained true --no-restore /p:PublishTrimmed=true /p:PublishReadyToRunComposite=true /p:PublishSingleFile=true
# final stage/image
FROM mcr.microsoft.com/dotnet/runtime-deps:6.0-alpine-amd64
FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine-amd64
WORKDIR /app
COPY --from=build /app ./

View File

@@ -12,7 +12,7 @@ using HardHat;
using TerribleDev.Blog.Web.Models;
using TerribleDev.Blog.Web.Factories;
using Microsoft.Extensions.Hosting;
using WebMarkupMin.AspNetCore6;
using WebMarkupMin.AspNetCore7;
using Microsoft.Extensions.Logging;
using TerribleDev.Blog.Web.Filters;
@@ -68,16 +68,23 @@ namespace TerribleDev.Blog.Web
controllerBuilder.AddRazorRuntimeCompilation();
}
#endif
services.AddResponseCompression(a =>
services
.AddResponseCompression(a =>
{
a.EnableForHttps = true;
})
.AddResponseCaching()
.AddMemoryCache();
if(Env.IsProduction())
{
services.AddOutputCaching();
}
// if(Env.IsProduction())
// {
// }
services.AddOutputCache(a =>{
a.AddBasePolicy(b => {
b.Cache();
});
});
services.AddWebMarkupMin(a => {
a.AllowMinificationInDevelopmentEnvironment = true;
a.DisablePoweredByHttpHeaders = true;
@@ -90,19 +97,15 @@ namespace TerribleDev.Blog.Web
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Console.WriteLine("ETag Detected As: " + StaticETag.staticEtag);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseHttpsRedirection();
if (env.IsProduction())
{
app.UseOutputCache();
app.UseResponseCaching();
}
app.UseResponseCompression();
var cacheTime = env.IsDevelopment() ? 0 : 31536000;
var cacheTime = env.IsDevelopment() ? 1 : 31536000;
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
@@ -120,6 +123,16 @@ namespace TerribleDev.Blog.Web
"public,max-age=" + cacheTime;
}
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseRewriter(new Microsoft.AspNetCore.Rewrite.RewriteOptions().AddRedirect("(.*[^/|.xml|.html])$", "$1/", 301));
app.UseIENoOpen();
app.UseNoMimeSniff();
@@ -142,20 +155,6 @@ namespace TerribleDev.Blog.Web
// },
UpgradeInsecureRequests = true
});
app.Use(async (context, next) => {
var etag = context.Request.Headers.IfNoneMatch.ToString();
if(etag != null && string.Equals(etag, StaticETag.staticEtag, StringComparison.Ordinal))
{
context.Response.StatusCode = 304;
await context.Response.CompleteAsync();
return;
}
await next();
});
if(Env.IsProduction())
{
app.UseOutputCaching();
}
app.UseWebMarkupMin();
app.UseRouting();
app.UseEndpoints(endpoints =>

View File

@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<RuntimeIdentifiers>linux-musl-x64</RuntimeIdentifiers>
<!-- <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<RuntimeIdentifiers>linux-musl-x64</RuntimeIdentifiers> -->
</PropertyGroup>
<ItemGroup>
@@ -23,12 +23,11 @@
<PackageReference Include="Markdig" Version="0.15.7" />
<PackageReference Include="Schema.NET" Version="11.0.1" />
<PackageReference Include="UriBuilder.Fluent" Version="1.5.2" />
<PackageReference Include="WebMarkupMin.AspNetCore6" Version="2.11.0" />
<PackageReference Include="WebMarkupMin.AspNetCore7" Version="2.13.0-rc1" />
<PackageReference Include="YamlDotNet" Version="5.3.0" />
<PackageReference Include="HardHat" Version="2.1.1" />
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
<PackageReference Include="WebEssentials.AspNetCore.OutputCaching" Version="1.0.16" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.1" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.0" Condition="'$(Configuration)' == 'Debug'" />
</ItemGroup>