Skip to content

Commit 7f3ac48

Browse files
committed
Add web api project
1 parent e4515bb commit 7f3ac48

File tree

7 files changed

+178
-1
lines changed

7 files changed

+178
-1
lines changed

.github/workflows/publish.yml

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11-
publish:
11+
publish_example:
1212
runs-on: ${{ matrix.config.os }}
1313

1414
strategy:
@@ -61,3 +61,57 @@ jobs:
6161
./AssetRipper.NativeDialogs.Example/bin/Release/net9.0/${{ matrix.config.runtime }}/publish/*.so
6262
./AssetRipper.NativeDialogs.Example/bin/Release/net9.0/${{ matrix.config.runtime }}/publish/*.dylib
6363
if-no-files-found: error
64+
65+
publish_web_api:
66+
runs-on: ${{ matrix.config.os }}
67+
68+
strategy:
69+
matrix:
70+
config:
71+
- { name: win_x64, os: windows-latest, runtime: win-x64, executable: AssetRipper.NativeDialogs.WebApi.exe }
72+
- { name: win_arm64, os: windows-latest, runtime: win-arm64, executable: AssetRipper.NativeDialogs.WebApi.exe }
73+
- { name: linux_x64, os: ubuntu-22.04, runtime: linux-x64, executable: AssetRipper.NativeDialogs.WebApi }
74+
- { name: linux_arm64, os: ubuntu-22.04, runtime: linux-arm64, executable: AssetRipper.NativeDialogs.WebApi }
75+
- { name: mac_x64, os: macos-latest, runtime: osx-x64, executable: AssetRipper.NativeDialogs.WebApi }
76+
- { name: mac_arm64, os: macos-latest, runtime: osx-arm64, executable: AssetRipper.NativeDialogs.WebApi }
77+
78+
steps:
79+
- uses: actions/checkout@v4
80+
- name: Setup .NET
81+
uses: actions/setup-dotnet@v4
82+
with:
83+
dotnet-version: 9.0.x
84+
85+
# https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/cross-compile#linux
86+
- name: Install Arm64 Dependencies
87+
if: matrix.config.runtime == 'linux-arm64'
88+
run: |
89+
sudo dpkg --add-architecture arm64
90+
sudo bash -c 'cat > /etc/apt/sources.list.d/arm64.list <<EOF
91+
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy main restricted
92+
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy-updates main restricted
93+
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy-backports main restricted universe multiverse
94+
EOF'
95+
sudo sed -i -e 's/deb http/deb [arch=amd64] http/g' /etc/apt/sources.list
96+
sudo sed -i -e 's/deb mirror/deb [arch=amd64] mirror/g' /etc/apt/sources.list
97+
sudo apt update
98+
sudo apt install -y clang llvm binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu zlib1g-dev:arm64
99+
100+
- name: Publish
101+
run: dotnet publish -c Release -r ${{ matrix.config.runtime }}
102+
working-directory: ./AssetRipper.NativeDialogs.WebApi/
103+
104+
- name: List Files
105+
shell: bash
106+
run: ls -R ./AssetRipper.NativeDialogs.WebApi/bin/Release/
107+
108+
- name: Upload
109+
uses: actions/upload-artifact@v4
110+
with:
111+
name: WebApi_${{ matrix.config.name }}
112+
path: |
113+
./AssetRipper.NativeDialogs.WebApi/bin/Release/net9.0/${{ matrix.config.runtime }}/publish/${{ matrix.config.executable }}
114+
./AssetRipper.NativeDialogs.WebApi/bin/Release/net9.0/${{ matrix.config.runtime }}/publish/*.dll
115+
./AssetRipper.NativeDialogs.WebApi/bin/Release/net9.0/${{ matrix.config.runtime }}/publish/*.so
116+
./AssetRipper.NativeDialogs.WebApi/bin/Release/net9.0/${{ matrix.config.runtime }}/publish/*.dylib
117+
if-no-files-found: error
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace AssetRipper.NativeDialogs.WebApi;
4+
5+
[JsonSerializable(typeof(string[]))]
6+
internal partial class AppJsonSerializerContext : JsonSerializerContext
7+
{
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InvariantGlobalization>true</InvariantGlobalization>
8+
<PublishAot>true</PublishAot>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\AssetRipper.NativeDialogs\AssetRipper.NativeDialogs.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace AssetRipper.NativeDialogs.WebApi;
4+
5+
public static class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
10+
11+
builder.Services.ConfigureHttpJsonOptions(options =>
12+
{
13+
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
14+
});
15+
16+
WebApplication app = builder.Build();
17+
18+
app.MapGet("/", () => Task.FromResult<string?>("Welcome to the AssetRipper Native Dialogs Web API!"));
19+
app.MapGet("/open-file", OpenFileDialog.OpenFile);
20+
app.MapGet("/open-files", OpenFileDialog.OpenFiles);
21+
app.MapGet("/open-folder", OpenFolderDialog.OpenFolder);
22+
app.MapGet("/open-folders", OpenFolderDialog.OpenFolders);
23+
app.MapGet("/save-file", SaveFileDialog.SaveFile);
24+
app.MapGet("/confirm", static async Task<string?> () =>
25+
{
26+
bool? result = await ConfirmationDialog.Confirm(new() { Message = "Do you acknowledge?", Type = ConfirmationDialog.Type.YesNo });
27+
return result switch
28+
{
29+
true => "User acknowledged.",
30+
false => "User did not acknowledge.",
31+
null => "User canceled the dialog."
32+
};
33+
});
34+
app.MapGet("/message", static async Task<string?> () =>
35+
{
36+
await MessageDialog.Message("Hello, World!");
37+
return "Message dialog shown.";
38+
});
39+
40+
app.Run();
41+
}
42+
43+
private static void DisableCaching(this HttpResponse response)
44+
{
45+
response.Headers.CacheControl = "no-store, max-age=0";
46+
}
47+
48+
private static void MapGet(this WebApplication app, [StringSyntax("Route")] string path, Func<Task<string?>> func)
49+
{
50+
app.MapGet(path, async (context) =>
51+
{
52+
context.Response.DisableCaching();
53+
await JsonResult(await func.Invoke()).ExecuteAsync(context);
54+
});
55+
}
56+
57+
private static void MapGet(this WebApplication app, [StringSyntax("Route")] string path, Func<Task<string[]?>> func)
58+
{
59+
app.MapGet(path, async (context) =>
60+
{
61+
context.Response.DisableCaching();
62+
await JsonResult(await func.Invoke()).ExecuteAsync(context);
63+
});
64+
}
65+
66+
private static IResult JsonResult<T>(T? value) where T : class
67+
{
68+
if (value is null)
69+
{
70+
return Results.Text("null", "text/json");
71+
}
72+
else
73+
{
74+
return Results.Json(value, AppJsonSerializerContext.Default);
75+
}
76+
}
77+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

AssetRipper.NativeDialogs.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1818
README.md = README.md
1919
EndProjectSection
2020
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetRipper.NativeDialogs.WebApi", "AssetRipper.NativeDialogs.WebApi\AssetRipper.NativeDialogs.WebApi.csproj", "{18204EF5-F8B0-4314-BF6C-3FBB2AD6FB12}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -32,6 +34,10 @@ Global
3234
{B666C286-5649-456B-A356-FB91DC0DE0C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
3335
{B666C286-5649-456B-A356-FB91DC0DE0C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
3436
{B666C286-5649-456B-A356-FB91DC0DE0C3}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{18204EF5-F8B0-4314-BF6C-3FBB2AD6FB12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{18204EF5-F8B0-4314-BF6C-3FBB2AD6FB12}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{18204EF5-F8B0-4314-BF6C-3FBB2AD6FB12}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{18204EF5-F8B0-4314-BF6C-3FBB2AD6FB12}.Release|Any CPU.Build.0 = Release|Any CPU
3541
EndGlobalSection
3642
GlobalSection(SolutionProperties) = preSolution
3743
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)