Skip to content

Commit

Permalink
First working draft, yet without meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 20, 2023
1 parent 935f7ab commit 3d1a9e3
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Modules/AutoLayout/GenHTTP.Modules.AutoLayout.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
<ProjectReference Include="..\Basics\GenHTTP.Modules.Basics.csproj" />
<ProjectReference Include="..\IO\GenHTTP.Modules.IO.csproj" />

<ProjectReference Include="..\Layouting\GenHTTP.Modules.Layouting.csproj" />
<ProjectReference Include="..\Layouting\GenHTTP.Modules.Layouting.csproj" />

<ProjectReference Include="..\Placeholders\GenHTTP.Modules.Placeholders.csproj" />
<ProjectReference Include="..\Markdown\GenHTTP.Modules.Markdown.csproj" />
<ProjectReference Include="..\Scriban\GenHTTP.Modules.Scriban.csproj" />
<ProjectReference Include="..\Scriban\GenHTTP.Modules.Scriban.csproj" />

</ItemGroup>

Expand Down
8 changes: 6 additions & 2 deletions Modules/AutoLayout/Provider/AutoLayoutHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ public class AutoLayoutHandler : IHandler

private IHandler Content { get; set; }

private HandlerRegistry HandlerRegistry { get; }

#endregion

#region Initialization

public AutoLayoutHandler(IHandler parent, IResourceTree tree, string[] indexNames)
public AutoLayoutHandler(IHandler parent, IResourceTree tree, HandlerRegistry registry, string[] indexNames)
{
Parent = parent;

Tree = tree;
IndexNames = indexNames;

HandlerRegistry = registry;

Content = Layout.Create()
.Build(this);
}

public async ValueTask ReloadAsync()
{
Content = (await TreeScanner.ScanAsync(Tree, IndexNames)).Build(this);
Content = (await TreeScanner.ScanAsync(Tree, HandlerRegistry, IndexNames)).Build(this);
}

#endregion
Expand Down
61 changes: 61 additions & 0 deletions Modules/AutoLayout/Provider/AutoLayoutHandlerBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Modules.AutoLayout.Scanning;
using System.Collections.Generic;

namespace GenHTTP.Modules.AutoLayout.Provider
{

public class AutoLayoutHandlerBuilder : IHandlerBuilder<AutoLayoutHandlerBuilder>
{
private readonly List<IConcernBuilder> _Concerns = new();

private IResourceTree? _Tree;

private HandlerRegistryBuilder? _Registry;

private string[]? _Index;

#region Functionality

public AutoLayoutHandlerBuilder Tree(IResourceTree tree)
{
_Tree = tree;
return this;
}

public AutoLayoutHandlerBuilder Registry(HandlerRegistryBuilder registry)
{
_Registry = registry;
return this;
}

public AutoLayoutHandlerBuilder Index(params string[] index)
{
_Index = index;
return this;
}

public AutoLayoutHandlerBuilder Add(IConcernBuilder concern)
{
_Concerns.Add(concern);
return this;
}

public IHandler Build(IHandler parent)
{
var tree = _Tree ?? throw new BuilderMissingPropertyException("tree");

var registry = _Registry ?? Resolvers.Default();

var index = _Index ?? new[] { "Index" };

return Concerns.Chain(parent, _Concerns, (p) => new AutoLayoutHandler(p, tree, registry.Build(), index));
}

#endregion

}

}
30 changes: 30 additions & 0 deletions Modules/AutoLayout/Provider/PlainProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading.Tasks;

using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;
using GenHTTP.Api.Protocol;

using GenHTTP.Modules.Basics;
using GenHTTP.Modules.Placeholders;

namespace GenHTTP.Modules.AutoLayout.Provider
{

public class PlainProvider : IResourceHandlerProvider
{

public bool Supports(IResource resource)
{
var type = (resource.ContentType?.KnownType ?? resource.Name?.GuessContentType());

return (type == ContentType.TextPlain) || (type == ContentType.TextHtml);
}

public ValueTask<IHandlerBuilder> GetHandlerAsync(IResource resource)
{
return new(Page.From(resource));
}

}

}
1 change: 1 addition & 0 deletions Modules/AutoLayout/Resolvers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Resolvers
public static HandlerRegistryBuilder Default()
{
return new HandlerRegistryBuilder().Fallback(new DownloadProvider())
.Add(new PlainProvider())
.Add(new MarkdownProvider())
.Add(new ScribanProvider());
}
Expand Down
10 changes: 5 additions & 5 deletions Modules/AutoLayout/Scanning/TreeScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ namespace GenHTTP.Modules.AutoLayout.Scanning
public static class TreeScanner
{

public static ValueTask<LayoutBuilder> ScanAsync(IResourceTree tree, HandlerRegistry registry, params string[] indexNames)
public static ValueTask<LayoutBuilder> ScanAsync(IResourceTree tree, HandlerRegistry registry, string[] indexNames)
{
return ScanContainerAsync(tree, registry, indexNames);
}

private static async ValueTask<LayoutBuilder> ScanContainerAsync(IResourceContainer container, HandlerRegistry registry, params string[] indexNames)
private static async ValueTask<LayoutBuilder> ScanContainerAsync(IResourceContainer container, HandlerRegistry registry, string[] indexNames)
{
var layout = Layout.Create();

await foreach (var node in container.GetNodes())
{
layout.Add(node.Name, await ScanContainerAsync(node, registry));
layout.Add(node.Name.ToLowerInvariant(), await ScanContainerAsync(node, registry, indexNames));
}

await foreach (var resource in container.GetResources())
Expand All @@ -34,15 +34,15 @@ private static async ValueTask<LayoutBuilder> ScanContainerAsync(IResourceContai

var fileName = Path.GetFileNameWithoutExtension(resource.Name).ToLowerInvariant();

var isIndex = indexNames.Any(n => n == fileName);
var isIndex = indexNames.Any(n => n.ToLowerInvariant() == fileName);

if (isIndex)
{
layout.Index(handler);
}
else
{
layout.Add(resource.Name, handler);
layout.Add(fileName, handler);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions Modules/AutoLayout/TreeLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using GenHTTP.Api.Content.IO;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Modules.AutoLayout.Provider;

namespace GenHTTP.Modules.AutoLayout
{
public static class TreeLayout
{

public static AutoLayoutHandlerBuilder From(IResourceTree tree) => new AutoLayoutHandlerBuilder().Tree(tree);

public static AutoLayoutHandlerBuilder From(IBuilder<IResourceTree> tree) => new AutoLayoutHandlerBuilder().Tree(tree.Build());

}

}
13 changes: 13 additions & 0 deletions Playground/GenHTTP.Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@

</PropertyGroup>

<ItemGroup>
<None Remove="Header.jpg" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Header.jpg" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="GenHTTP.Themes.Lorahost" Version="8.0.0" />
</ItemGroup>

<ItemGroup>

<ProjectReference Include="..\API\GenHTTP.Api.csproj" />
Expand Down Expand Up @@ -47,6 +59,7 @@
<ProjectReference Include="..\Modules\AutoReload\GenHTTP.Modules.AutoReload.csproj" />
<ProjectReference Include="..\Modules\Functional\GenHTTP.Modules.Functional.csproj" />
<ProjectReference Include="..\Modules\StaticWebsites\GenHTTP.Modules.StaticWebsites.csproj" />
<ProjectReference Include="..\Modules\AutoLayout\GenHTTP.Modules.AutoLayout.csproj" />

</ItemGroup>

Expand Down
Binary file added Playground/Header.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 29 additions & 2 deletions Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
using GenHTTP.Engine;

using GenHTTP.Modules.AutoLayout;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.Websites;
using GenHTTP.Themes.Lorahost;
using System.Collections.Generic;

var layout = TreeLayout.From(ResourceTree.FromDirectory(@"C:\Work\GenHTTP\GenHTTP.Website\Project\Pages"))
.Index("Home", "Index", "Intro");

var theme = Theme.Create()
.Header(Resource.FromAssembly("Header.jpg"))
.Title("GenHTTP Webserver")
.Subtitle("Simple and lightweight, embeddable HTTP webserver written in pure C# with few dependencies to 3rd-party libraries. Compatible with .NET 6/7/8.")
.Action("documentation/", "Get started");

var menu = Menu.Empty()
.Add("{website}", "Home")
.Add("features", "Features")
.Add("documentation/", "Documentation", new List<(string, string)> { ("content/", "Providing Content"), ("testing/", "Testing Apps"), ("server/", "Server Setup"), ("hosting/", "Hosting Apps"), ("asp-net-comparison", "Comparison with ASP.NET") })
.Add("links", "Links")
.Add("https://discord.gg/GwtDyUpkpV", "Discord")
.Add("https://github.com/Kaliumhexacyanoferrat/GenHTTP", "GitHub")
.Add("legal", "Legal");


var website = Website.Create()
.Content(layout)
.Theme(theme)
.Menu(menu);

Host.Create()
.Handler(Content.From(Resource.FromString("Hello World")))
.Handler(website)
.Defaults()
.Development()
.Console()
Expand Down

0 comments on commit 3d1a9e3

Please sign in to comment.