1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

全てのリクエストに502を返すアプリ

Last updated at Posted at 2022-03-07

はじめに

メンテナンス中にすべての要求に対して 502 を返すアプリが欲しくなった。
普通は NGINX でやらせるだろうけれど、実験的に ASP.NET Core で作ってみた。

プロジェクトの作成

とりあえず空のWebプロジェクトを作って、

❯ dotnet new web -o allways502
❯ cd allways502

元からある app.MapGet をコメントにして、特定のステータスを返す Run デリゲートを追加する。

Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// app.MapGet("/", () => "Hello World!");
app.Run(async context =>
{
   context.Response.StatusCode = StatusCodes.Status502BadGateway;
   await context.Response.WriteAsync("Hello BadGateway");
});

app.Run();

アプリを起動して、

❯ dotnet run
ビルドしています...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7231
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5161
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\LocalRepo\sugiyama\Sandbox\allways502\

リクエストすると、どんな URL でも 502 が返ってくる

❯ curl --dump-header - https://localhost:7231
HTTP/1.1 502 Bad Gateway
Date: Mon, 07 Mar 2022 05:09:08 GMT
Server: Kestrel
Transfer-Encoding: chunked

Hello BadGateway

❯ curl --dump-header - https://localhost:7231/abc
HTTP/1.1 502 Bad Gateway
Date: Mon, 07 Mar 2022 05:09:18 GMT
Server: Kestrel
Transfer-Encoding: chunked

Hello BadGateway

おわりに

ngrok を使って公開してやれば、誰でもアクセスできるサーバーの出来上がり!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?