Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain content instead of replacing it #189

Merged
merged 4 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions API/Content/ContentElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ public ContentElement(string path, ContentInfo details, FlexibleContentType cont

#endregion

#region Functionality

public ulong CalculateChecksum() => CalculateChecksum(this);

private static ulong CalculateChecksum(ContentElement element)
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)element.Path.GetHashCode();
hash = hash * 23 + (uint)element.Details.GetHashCode();
hash = hash * 23 + (uint)element.ContentType.GetHashCode();

if (element.Children != null)
{
foreach (var child in element.Children)
{
hash = hash * 23 + CalculateChecksum(child);
}
}

return hash;
}
}

#endregion

}

}
13 changes: 1 addition & 12 deletions API/Content/IErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
using GenHTTP.Api.Content.Templating;

using System.Threading.Tasks;

namespace GenHTTP.Api.Content
{

/// <summary>
/// Handlers implementing this interface will be queried to render
/// errors that occur when handling requests.
/// </summary>
public interface IErrorHandler
public interface IErrorHandler : IRenderer<ErrorModel>
{

/// <summary>
/// Renders the given error into a web page that can be rendered
/// using a template.
/// </summary>
/// <param name="error">The error to be handled</param>
/// <param name="details">Additional details about the error</param>
/// <returns>The page to be rendered into the template</returns>
ValueTask<TemplateModel> RenderAsync(ErrorModel error, ContentInfo details);

}

}
13 changes: 2 additions & 11 deletions API/Content/IPageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Threading.Tasks;

using GenHTTP.Api.Content.Templating;
using GenHTTP.Api.Protocol;
using GenHTTP.Api.Content.Templating;

namespace GenHTTP.Api.Content
{
Expand All @@ -10,15 +7,9 @@ namespace GenHTTP.Api.Content
/// Handlers implementing this interface will be queried to render
/// pages into a template.
/// </summary>
public interface IPageRenderer
public interface IPageRenderer : IRenderer<TemplateModel>
{

/// <summary>
/// Renders the given model into a web page to be served to the client.
/// </summary>
/// <param name="model">The model to be rendered</param>
/// <returns>The response to be returned to the client</returns>
ValueTask<IResponseBuilder> RenderAsync(TemplateModel model);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -7,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// Model for pages which can be served by an application
/// to the clients.
/// </summary>
public class PageModel : IBaseModel
public abstract class AbstractModel : IModel
{

#region Get-/Setters
Expand All @@ -24,21 +26,27 @@ public class PageModel : IBaseModel

#endregion

#region Functionality
#region Initialization

/// <summary>
/// Creates a new page model for the given request.
/// </summary>
/// <param name="request">The request which caused this rendering call</param>
/// <param name="handler">The handler responsible to render the response</param>
public PageModel(IRequest request, IHandler handler)
public AbstractModel(IRequest request, IHandler handler)
{
Request = request;
Handler = handler;
}

#endregion

#region Functionality

public abstract ValueTask<ulong> CalculateChecksumAsync();

#endregion

}

}
32 changes: 32 additions & 0 deletions API/Content/Templating/BasicModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{

/// <summary>
/// Simple page model with no further information than the mandatory request
/// and handler values.
/// </summary>
public class BasicModel : AbstractModel
{

#region Initialization

public BasicModel(IRequest request, IHandler handler) : base(request, handler)
{

}

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync() => new(17);

#endregion

}

}
24 changes: 23 additions & 1 deletion API/Content/Templating/ErrorModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

Expand All @@ -8,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// <summary>
/// Model for errors that occur when handling requests.
/// </summary>
public sealed class ErrorModel : PageModel
public sealed class ErrorModel : AbstractModel
{

#region Get-/Setters
Expand Down Expand Up @@ -48,6 +49,27 @@ public ErrorModel(IRequest request, IHandler handler, ResponseStatus status, str

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync()
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)Status;

hash = hash * 23 + (uint)Message.GetHashCode();

hash = hash * 23 + (uint)(DevelopmentMode ? 1 : 0);
hash = hash * 23 + (uint)(Cause?.GetHashCode() ?? 0);

return new(hash);
}
}

#endregion

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -7,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// Interface which needs to be implemented by all models
/// used to render pages or templates.
/// </summary>
public interface IBaseModel
public interface IModel
{

/// <summary>
Expand All @@ -20,6 +22,16 @@ public interface IBaseModel
/// </summary>
IHandler Handler { get; }

/// <summary>
/// Calculates the checksum of this model.
/// </summary>
/// <returns>The checksum of this model</returns>
/// <remarks>
/// Required to determine, whether an already cached page
/// needs to be refreshed or not.
/// </remarks>
ValueTask<ulong> CalculateChecksumAsync();

}

}
8 changes: 7 additions & 1 deletion API/Content/Templating/IRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GenHTTP.Api.Content.Templating
/// Allows to render models of the given type.
/// </summary>
/// <typeparam name="T">The type of the model to be rendered</typeparam>
public interface IRenderer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] in T> where T : class, IBaseModel
public interface IRenderer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] in T> where T : class, IModel
{

/// <summary>
Expand All @@ -18,6 +18,12 @@ public interface IRenderer<[DynamicallyAccessedMembers(DynamicallyAccessedMember
/// </summary>
ValueTask PrepareAsync();

/// <summary>
/// Returns a checksum for the template used by this renderer instance.
/// </summary>
/// <returns>The checksum of the template used by this renderer</returns>
ValueTask<ulong> CalculateChecksumAsync();

/// <summary>
/// Renders the given model.
/// </summary>
Expand Down
36 changes: 21 additions & 15 deletions API/Content/Templating/TemplateModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -7,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// Model used by templates to render the actual HTML returned
/// to the client.
/// </summary>
public sealed class TemplateModel : IBaseModel
public sealed class TemplateModel : AbstractModel
{

#region Get-/Setters
Expand All @@ -22,16 +24,6 @@ public sealed class TemplateModel : IBaseModel
/// </summary>
public string Content { get; }

/// <summary>
/// The request which caused this call.
/// </summary>
public IRequest Request { get; }

/// <summary>
/// The handler responsible to render the response.
/// </summary>
public IHandler Handler { get; }

#endregion

#region Initialization
Expand All @@ -43,7 +35,7 @@ public sealed class TemplateModel : IBaseModel
/// <param name="handler">The handler responsible to render the response</param>
/// <param name="pageInfo">Information about the page to be rendered</param>
/// <param name="content">The content to be rendered within the template</param>
public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, string content)
public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, string content) : base(request, handler)
{
Content = content;

Expand All @@ -53,9 +45,23 @@ public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, s
{
Meta = Meta with { Title = "Untitled Page" };
}
}

Request = request;
Handler = handler;
#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync()
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)Content.GetHashCode();
hash = hash * 23 + (uint)Meta.GetHashCode();

return new(hash);
}
}

#endregion
Expand Down
9 changes: 8 additions & 1 deletion API/Content/Templating/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -22,7 +23,7 @@ public ViewModel(IRequest request, IHandler handler) : base(request, handler, nu

}

public class ViewModel<T> : PageModel where T : class
public class ViewModel<T> : AbstractModel where T : class
{

#region Get-/Setters
Expand All @@ -40,6 +41,12 @@ public ViewModel(IRequest request, IHandler handler, T? data) : base(request, ha

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync() => new((ulong)(Data?.GetHashCode() ?? 17));

#endregion

}

}
Loading