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

Play with helper function app #903

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
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
22 changes: 19 additions & 3 deletions .github/workflows/azure-static-web-apps-green-field-0353fee03.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, windows-latest, macOS-latest ]
# os: [ ubuntu-latest, windows-latest, macOS-latest ]
os: [ ubuntu-latest ]
outputs:
deployUrl: ${{ steps.outputJobVariables.outputs.baseUrl }}
steps:
Expand Down Expand Up @@ -123,6 +124,15 @@ jobs:
if-no-files-found: error
retention-days: 1

- name: Upload tools
uses: actions/upload-artifact@v4
if: matrix.os == 'ubuntu-latest'
with:
name: ${{ format('api-{0}', matrix.os) }}
path: api-5
if-no-files-found: error
retention-days: 1

# Enable the following to do something with snapshot result(s)
# - name: Check Verify Results
# if: always()
Expand Down Expand Up @@ -166,6 +176,12 @@ jobs:
name: ${{ format('tools-{0}', 'ubuntu-latest') }}
path: tools

- name: Download api artifact
uses: actions/download-artifact@v4
with:
name: ${{ format('api-{0}', 'ubuntu-latest') }}
path: api

- name: Use Node.js v20
uses: actions/setup-node@v4
with:
Expand Down Expand Up @@ -195,10 +211,10 @@ jobs:
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
action: "upload"
app_location: "dist"
api_location: ""
api_location: "api"
output_location: ""
skip_app_build: true
skip_api_build: true
skip_api_build: false

# Dotnet build is needed so we can use the playwright install
- name: Run E2e
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,7 @@ dist/
LivingDoc.html

# Verify snapshot testing
*.received.*
*.received.*

# Release API (dotnet publish func/func.csproj --output "api")
/api
22 changes: 20 additions & 2 deletions _site/_devtools/staticwebapp.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{
"navigationFallback": {
"rewrite": "/api/fallback",
"exclude": [
"assets/*",
"*.{png,jpg,gif,svg,ico}"
]
},
"platform": {
"apiRuntime": "dotnet-isolated:8.0"
},
"routes": [
{
"route": "/404",
"statusCode": 404
},
{
"route": "/favicon.ico",
"redirect": "/assets/images/favicon.ico"
},
{% for redirect in site.data.redirects %}
{
"route": "{{ redirect.Route }}",
Expand Down Expand Up @@ -42,7 +60,7 @@
},
"responseOverrides": {
"404": {
"rewrite": "/404.html"
}
"rewrite": "/404.html"
}
}
}
28 changes: 28 additions & 0 deletions api-5/Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions api-5/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Project></Project>
1 change: 1 addition & 0 deletions api-5/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Project></Project>
23 changes: 23 additions & 0 deletions api-5/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Kaylumah, 2024. All rights reserved.
// See LICENSE file in the project root for full license information.

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

class Program
{
static void Main()
{
IHost host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.Build();

host.Run();
}
}
9 changes: 9 additions & 0 deletions api-5/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"Api": {
"commandName": "Project",
"commandLineArgs": "--port 7071",
"launchBrowser": false
}
}
}
51 changes: 51 additions & 0 deletions api-5/WeatherForecastFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Kaylumah, 2024. All rights reserved.
// See LICENSE file in the project root for full license information.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Api
{
public class HttpTrigger
{
private readonly ILogger _logger;

public HttpTrigger(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HttpTrigger>();
}

[Function("fallback")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
{
bool hasHeader = req.Headers.TryGetValue("x-ms-original-url", out Microsoft.Extensions.Primitives.StringValues originalUrl);

_logger.LogInformation($"Original Url: {originalUrl}");

Uri? uri = new Uri(originalUrl.ToString());
_logger.LogInformation(uri.AbsolutePath);

/*
string AbsolutePath = "/2024/011/02/as-s.html";
// string AbsolutePath = "blog.html";
string pattern = @"^/(?<year>2024)(?<rest>.*)";
string replacement = "/something/${year}${rest}";

if (Regex.IsMatch(AbsolutePath, pattern))
{
string newUrl = Regex.Replace(AbsolutePath, pattern, replacement);
}*/


RedirectResult result = new RedirectResult($"/404?originalUrl={uri.AbsolutePath}");
result.Permanent = true;
return result;

// Fallback to 404 (does not work)
// NotFoundResult result = new NotFoundResult();
// return result;
}
}
}
12 changes: 12 additions & 0 deletions api-5/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
12 changes: 12 additions & 0 deletions api-5/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*",
"CORSCredentials": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"start": "swa start --app-location dist"
"start": "swa start --app-location dist --api-location api-5"
},
"devDependencies": {
"@azure/static-web-apps-cli": "^2.0.1"
Expand Down