Skip to content

Commit 6e7a0fa

Browse files
author
Mathis Burger
committed
[TASK] Added file server
1 parent cd2b1c5 commit 6e7a0fa

16 files changed

+16369
-3
lines changed

App.razor

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>

Components/Navbar.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<nav class="navbar bg-light">
2+
<div class="container-fluid">
3+
<a class="navbar-brand" href="">
4+
ModularSmartHome
5+
</a>
6+
</div>
7+
</nav>

Controllers/AssetController.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Kernel.Controllers;
4+
5+
[ApiController]
6+
public class AssetController : ControllerBase
7+
{
8+
9+
[HttpGet("/css/{name}")]
10+
public IActionResult CssServer([FromRoute] string name)
11+
{
12+
return StreamFile("css", name);
13+
}
14+
15+
private IActionResult StreamFile(string dir, string name)
16+
{
17+
string path = Path.Combine(Path.Combine("wwwroot", dir), name);
18+
//return Ok(path);
19+
20+
//Read the File data into Byte Array.
21+
byte[] bytes = System.IO.File.ReadAllBytes(path);
22+
23+
//Send the File to Download.
24+
return File(bytes, "application/octet-stream", name);
25+
}
26+
27+
}

Kernel.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<ItemGroup>
1111
<PackageReference Include="JWT" Version="9.0.0" />
1212
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.2.1" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.8" />
14+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
1315
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
1416
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
1517
<PrivateAssets>all</PrivateAssets>
@@ -23,13 +25,17 @@
2325
</ItemGroup>
2426

2527
<ItemGroup>
26-
<Folder Include="Components" />
2728
<Folder Include="Migrations" />
28-
<Folder Include="Pages" />
29+
<Folder Include="wwwroot" />
2930
</ItemGroup>
3031

3132
<ItemGroup>
3233
<_ContentIncludedByDefault Remove="Pages\Shared\_Layout.cshtml" />
34+
<_ContentIncludedByDefault Remove="Pages\Shared\_Layout.razor" />
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<Content Remove="Pages\_Imports.razor" />
3339
</ItemGroup>
3440

3541
</Project>

Pages/Error.cshtml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@page
2+
@model Kernel.Pages.ErrorModel
3+
4+
<!DOCTYPE html>
5+
<html lang="en">
6+
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
10+
<title>Error</title>
11+
<link href="~/css/bootstrap.min.css" rel="stylesheet" />
12+
</head>
13+
14+
<body>
15+
<div class="main">
16+
<div class="content px-4">
17+
<h1 class="text-danger">Error.</h1>
18+
<h2 class="text-danger">An error occurred while processing your request.</h2>
19+
20+
@if (Model.ShowRequestId)
21+
{
22+
<p>
23+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
24+
</p>
25+
}
26+
27+
<h3>Development Mode</h3>
28+
<p>
29+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
30+
</p>
31+
<p>
32+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
33+
It can result in displaying sensitive information from exceptions to end users.
34+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
35+
and restarting the app.
36+
</p>
37+
</div>
38+
</div>
39+
</body>
40+
41+
</html>

Pages/Error.cshtml.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace Kernel.Pages;
6+
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string? RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
private readonly ILogger<ErrorModel> _logger;
16+
17+
public ErrorModel(ILogger<ErrorModel> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25+
}
26+
}

Pages/Index.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@page "/"
2+
3+
<PageTitle>Index</PageTitle>
4+
5+
<h1>Hello, world!</h1>

Pages/_Host.cshtml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/"
2+
@namespace Kernel.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@{
5+
Layout = "_Layout";
6+
}
7+
8+
<component type="typeof(App)" render-mode="ServerPrerendered" />

Pages/_Layout.cshtml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@using Microsoft.AspNetCore.Components.Web
2+
@namespace Kernel.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
10+
<base href="~/" />
11+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
12+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
13+
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
14+
</head>
15+
<body>
16+
@RenderBody()
17+
<script src="_framework/blazor.server.js"></script>
18+
</body>
19+
</html>

Shared/MainLayout.razor

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@inherits LayoutComponentBase
2+
3+
<PageTitle>ModularSmartHome</PageTitle>
4+
5+
<Navbar />
6+
<div class="container">
7+
@Body
8+
</div>

Startup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public void ConfigureServices(IServiceCollection services)
3434
{
3535
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Watchify API endpoints", Version = "v1" });
3636
});
37+
services.AddRazorPages();
38+
services.AddServerSideBlazor();
3739

3840
}
3941

@@ -53,7 +55,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5355
});
5456
}
5557

56-
app.UseStaticFiles();
5758
app.UseRouting();
5859

5960
app.UseAuthorization();
@@ -68,6 +69,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6869
app.UseEndpoints(endpoints =>
6970
{
7071
endpoints.MapControllers();
72+
endpoints.MapBlazorHub();
73+
endpoints.MapFallbackToPage("/_Host");
7174
});
75+
app.UseStaticFiles();
7276
}
7377
}

_Imports.razor

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@using System.Net.Http
2+
@using Microsoft.AspNetCore.Authorization
3+
@using Microsoft.AspNetCore.Components.Authorization
4+
@using Microsoft.AspNetCore.Components.Forms
5+
@using Microsoft.AspNetCore.Components.Routing
6+
@using Microsoft.AspNetCore.Components.Web
7+
@using Microsoft.AspNetCore.Components.Web.Virtualization
8+
@using Microsoft.JSInterop
9+
@using Kernel
10+
@using Kernel.Shared
11+
@using Kernel.Components

0 commit comments

Comments
 (0)