IIS設定

This commit is contained in:
armonica committed 2026-09-05 02:56:01 +09:00
1 parent 422a9d8ccc
commit 544f145f4f
2 files changed
+45 -25

No files matched your search

+26 -23
View File
@@ -6,24 +6,27 @@ using WebImageViewer.Class.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Docker/Caddyリバースプロキシからの転送(host.docker.internal:5000)を受け付けるため全インターフェースでリッスン // =========================================================================
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ASPNETCORE_URLS"))) // 【重要】IIS ホスト時のポート設定
// IIS (AspNetCoreModuleV2) がポートを動的に割り当てて制御するため、
// builder.WebHost.UseUrls(...) は呼び出さず、IIS のバインディング (8080) に委ねます。
// ※ローカルで IIS を介さず exe 単体でデバッグ起動したい場合のみ、環境変数がない時に UseUrls を有効にします。
if (!builder.Environment.IsProduction() && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ASPNETCORE_PORT")))
{ {
builder.WebHost.UseUrls("http://0.0.0.0:5000"); builder.WebHost.UseUrls("http://0.0.0.0:5000");
} }
// =========================================================================
// ========================================== // ==========================================
// サービスの登録 (DIコンテナ) // サービスの登録 (DIコンテナ)
// ========================================== // ==========================================
// メモリキャッシュ機能の追加
// メモリキャッシュ機能の追加(VPSのトラフィック削減・高速化)
builder.Services.AddMemoryCache(); builder.Services.AddMemoryCache();
// HTTP通信用ヘルパーの登録 // HTTP通信用ヘルパーの登録
builder.Services.AddHttpClient<HttpHelper>(); builder.Services.AddHttpClient<HttpHelper>();
// 各種スクレイパークラスの登録(Class/Scrapers フォルダより) // 各種スクレイパークラスの登録
builder.Services.AddScoped<IScraper, MomongaScraper>(); builder.Services.AddScoped<IScraper, MomongaScraper>();
builder.Services.AddScoped<MomongaScraper>(); builder.Services.AddScoped<MomongaScraper>();
builder.Services.AddScoped<IScraper, AuraHentaiScraper>(); builder.Services.AddScoped<IScraper, AuraHentaiScraper>();
@@ -33,17 +36,18 @@ builder.Services.AddScoped<HitomiScraper>();
builder.Services.AddScoped<IScraper, EHentaiScraper>(); builder.Services.AddScoped<IScraper, EHentaiScraper>();
builder.Services.AddScoped<EHentaiScraper>(); builder.Services.AddScoped<EHentaiScraper>();
// ギャラリー統括管理サービスの登録
// ギャラリー統括管理サービスの登録(Class/Services フォルダより)
builder.Services.AddScoped<GalleryService>(); builder.Services.AddScoped<GalleryService>();
// VPS(リバースプロキシ環境: https://udukipc-web.armonica-vps.work/)対応のフォワードヘッダー設定 // VPS / Caddy リバースプロキシ環境対応のフォワードヘッダー設定
builder.Services.Configure<ForwardedHeadersOptions>(options => builder.Services.Configure<ForwardedHeadersOptions>(options =>
{ {
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost; options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
// リバースプロキシ背後での動作を許可
// 多段プロキシ(Caddy -> IIS -> Kestrel)経由でもヘッダーを最後まで反映させる
options.ForwardLimit = null;
// 非推奨となった KnownNetworks は使わず、KnownIPNetworks をクリア
options.KnownIPNetworks.Clear(); options.KnownIPNetworks.Clear();
options.KnownProxies.Clear(); options.KnownProxies.Clear();
}); });
@@ -54,16 +58,17 @@ var app = builder.Build();
// ミドルウェアパイプラインの設定 // ミドルウェアパイプラインの設定
// ========================================== // ==========================================
// プロキシヘッダーの反映(ルーティング等の前で必ず実行)
app.UseForwardedHeaders(); app.UseForwardedHeaders();
// ========================================== // ==========================================
// ルーティング・エンドポイントの定義 (統一アクセス方式) // ルーティング・エンドポイントの定義 (統一アクセス方式)
// ========================================== // ==========================================
// トップページ(URL入力・ギャラリー検索UI) // トップページ
app.MapGet("/", () => Results.Content(HtmlRenderer.RenderHomePage(), "text/html; charset=utf-8")); app.MapGet("/", () => Results.Content(HtmlRenderer.RenderHomePage(), "text/html; charset=utf-8"));
// 汎用ビューアー画面(URLクエリパラメータ経由: 例 /view?url=https://aurahentai.com/gallery/013597 または /view?site=aurahentai&id=013597) // 汎用ビューアー画面
app.MapGet("/view", async (string? url, string? site, string? id, string? category, GalleryService galleryService, CancellationToken ct) => app.MapGet("/view", async (string? url, string? site, string? id, string? category, GalleryService galleryService, CancellationToken ct) =>
{ {
var target = url ?? id; var target = url ?? id;
@@ -75,13 +80,13 @@ app.MapGet("/view", async (string? url, string? site, string? id, string? catego
return await RenderGalleryResponseAsync(site, target, category, galleryService, ct); return await RenderGalleryResponseAsync(site, target, category, galleryService, ct);
}); });
// 統一ビューアー画面(3階層: /{site}/{category}/{id} 例: /momonga/fanzine/mo3469109) // 統一ビューアー画面(3階層)
app.MapGet("/{site}/{category}/{id}", async (string site, string category, string id, GalleryService galleryService, CancellationToken ct) => app.MapGet("/{site}/{category}/{id}", async (string site, string category, string id, GalleryService galleryService, CancellationToken ct) =>
{ {
return await RenderGalleryResponseAsync(site, id, category, galleryService, ct); return await RenderGalleryResponseAsync(site, id, category, galleryService, ct);
}); });
// 統一ビューアー画面(2階層: /{site}/{id} 例: /momonga/mo3469109, /aurahentai/013597, /fanzine/mo3469109, /gallery/013597) // 統一ビューアー画面(2階層)
app.MapGet("/{site}/{id}", async (string site, string id, GalleryService galleryService, CancellationToken ct) => app.MapGet("/{site}/{id}", async (string site, string id, GalleryService galleryService, CancellationToken ct) =>
{ {
return await RenderGalleryResponseAsync(site, id, null, galleryService, ct); return await RenderGalleryResponseAsync(site, id, null, galleryService, ct);
@@ -100,19 +105,19 @@ app.MapGet("/api/sites", (GalleryService galleryService) =>
return Results.Ok(sites); return Results.Ok(sites);
}); });
// 統一 JSON API(3階層: /api/{site}/{category}/{id} 例: /api/momonga/fanzine/mo3469109) // 統一 JSON API(3階層)
app.MapGet("/api/{site}/{category}/{id}", async (string site, string category, string id, GalleryService galleryService, CancellationToken ct) => app.MapGet("/api/{site}/{category}/{id}", async (string site, string category, string id, GalleryService galleryService, CancellationToken ct) =>
{ {
return await RenderJsonResponseAsync(site, id, category, galleryService, ct); return await RenderJsonResponseAsync(site, id, category, galleryService, ct);
}); });
// 統一 JSON API(2階層: /api/{site}/{id} 例: /api/momonga/mo3469109, /api/aurahentai/013597, /api/fanzine/mo3469109) // 統一 JSON API(2階層)
app.MapGet("/api/{site}/{id}", async (string site, string id, GalleryService galleryService, CancellationToken ct) => app.MapGet("/api/{site}/{id}", async (string site, string id, GalleryService galleryService, CancellationToken ct) =>
{ {
return await RenderJsonResponseAsync(site, id, null, galleryService, ct); return await RenderJsonResponseAsync(site, id, null, galleryService, ct);
}); });
// 汎用スクレイピング JSON API(URLまたはパラメータ直接指定) // 汎用スクレイピング JSON API
app.MapGet("/api/scrape", async (string? url, string? site, string? id, string? category, GalleryService galleryService, CancellationToken ct) => app.MapGet("/api/scrape", async (string? url, string? site, string? id, string? category, GalleryService galleryService, CancellationToken ct) =>
{ {
var target = url ?? id; var target = url ?? id;
@@ -124,7 +129,7 @@ app.MapGet("/api/scrape", async (string? url, string? site, string? id, string?
return await RenderJsonResponseAsync(site, target, category, galleryService, ct); return await RenderJsonResponseAsync(site, target, category, galleryService, ct);
}); });
// 画像プロキシエンドポイント(CORSや外部Referer制約を回避する必要がある場合に使用) // 画像プロキシエンドポイント
app.MapGet("/api/proxy-image", async (string url, string? referer, HttpHelper httpHelper, CancellationToken ct) => app.MapGet("/api/proxy-image", async (string url, string? referer, HttpHelper httpHelper, CancellationToken ct) =>
{ {
if (string.IsNullOrWhiteSpace(url)) if (string.IsNullOrWhiteSpace(url))
@@ -146,7 +151,6 @@ app.MapGet("/api/proxy-image", async (string url, string? referer, HttpHelper ht
// ========================================== // ==========================================
// 共通レスポンス生成ヘルパー関数 // 共通レスポンス生成ヘルパー関数
// ========================================== // ==========================================
static async Task<IResult> RenderGalleryResponseAsync( static async Task<IResult> RenderGalleryResponseAsync(
string? site, string? site,
string idOrUrl, string idOrUrl,
@@ -189,5 +193,4 @@ static async Task<IResult> RenderJsonResponseAsync(
return Results.Ok(gallery); return Results.Ok(gallery);
} }
app.Run(); app.Run();
@@ -1,15 +1,32 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<DeleteExistingFiles>false</DeleteExistingFiles> <DeleteExistingFiles>false</DeleteExistingFiles>
<ExcludeApp_Data>false</ExcludeApp_Data> <ExcludeApp_Data>false</ExcludeApp_Data>
<LaunchSiteAfterPublish>true</LaunchSiteAfterPublish> <LaunchSiteAfterPublish>false</LaunchSiteAfterPublish>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform> <LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider> <PublishProvider>FileSystem</PublishProvider>
<PublishUrl>C:\docker\web\</PublishUrl> <PublishUrl>C:\docker\web\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod> <WebPublishMethod>FileSystem</WebPublishMethod>
<_TargetId>Folder</_TargetId> <_TargetId>Folder</_TargetId>
<TargetFramework>net10.0</TargetFramework>
<SelfContained>false</SelfContained>
</PropertyGroup> </PropertyGroup>
<!-- ① 発行開始前: app_offline.htm を配置して IIS のプロセスを停止し DLL ロックを解除 -->
<Target Name="TakeAppOffline" BeforeTargets="BeforePublish">
<Message Text="[Deploy] app_offline.htm を配置して IIS プロセスを停止します..." Importance="high" />
<WriteLinesToFile File="$(PublishUrl)app_offline.htm"
Lines="&lt;html&gt;&lt;body&gt;Publishing updates...&lt;/body&gt;&lt;/html&gt;"
Overwrite="true" />
<!-- プロセスが完全に終了してファイルロックが解放されるまで 2 秒待機 -->
<Exec Command="powershell -Command Start-Sleep -Seconds 2" />
</Target>
<!-- ② 発行完了後: app_offline.htm を削除して IIS プロセスを自動起動 -->
<Target Name="BringAppOnline" AfterTargets="AfterPublish">
<Message Text="[Deploy] app_offline.htm を削除して IIS アプリケーションを復帰します..." Importance="high" />
<Delete Files="$(PublishUrl)app_offline.htm" />
</Target>
</Project> </Project>