|
| 1 | +using System.Security.Cryptography.X509Certificates; |
| 2 | +using Microsoft.AspNetCore.Server.Kestrel.Core; |
1 | 3 | using ServiceStack; |
2 | 4 | using ServiceStack.Benchmarks; |
3 | 5 |
|
| 6 | +var certPath = Environment.GetEnvironmentVariable("TLS_CERT") ?? "/certs/server.crt"; |
| 7 | +var keyPath = Environment.GetEnvironmentVariable("TLS_KEY") ?? "/certs/server.key"; |
| 8 | +var hasCert = File.Exists(certPath) && File.Exists(keyPath); |
| 9 | + |
4 | 10 | var builder = WebApplication.CreateBuilder(args); |
5 | | -builder.WebHost.UseUrls("http://*:8080"); |
6 | 11 | builder.Services.AddResponseCompression(); |
7 | 12 | builder.Logging.ClearProviders(); |
8 | 13 |
|
9 | | -var app = builder.Build(); |
10 | | - |
11 | | -app.UseResponseCompression(); |
12 | | - |
13 | | -if (Directory.Exists("/data/static")) |
| 14 | +builder.WebHost.ConfigureKestrel(options => |
14 | 15 | { |
15 | | - var mimeTypes = new Dictionary<string, string> |
| 16 | + options.ListenAnyIP(8080, lo => |
16 | 17 | { |
17 | | - [".css"] = "text/css", [".js"] = "application/javascript", [".html"] = "text/html", |
18 | | - [".woff2"] = "font/woff2", [".svg"] = "image/svg+xml", [".webp"] = "image/webp", [".json"] = "application/json", |
19 | | - }; |
20 | | - var staticFiles = new Dictionary<string, (byte[] data, byte[]? br, byte[]? gz, string contentType)>(); |
21 | | - foreach (var file in Directory.GetFiles("/data/static")) |
22 | | - { |
23 | | - var name = Path.GetFileName(file); |
24 | | - if (name.EndsWith(".br") || name.EndsWith(".gz")) continue; |
25 | | - var ext = Path.GetExtension(name); |
26 | | - var ct = mimeTypes.GetValueOrDefault(ext, "application/octet-stream"); |
27 | | - var brPath = file + ".br"; |
28 | | - var gzPath = file + ".gz"; |
29 | | - staticFiles[name] = ( |
30 | | - File.ReadAllBytes(file), |
31 | | - File.Exists(brPath) ? File.ReadAllBytes(brPath) : null, |
32 | | - File.Exists(gzPath) ? File.ReadAllBytes(gzPath) : null, |
33 | | - ct |
34 | | - ); |
35 | | - } |
36 | | - app.MapGet("/static/{filename}", (string filename, HttpContext ctx) => |
| 18 | + lo.Protocols = HttpProtocols.Http1; |
| 19 | + }); |
| 20 | + |
| 21 | + if (hasCert) |
37 | 22 | { |
38 | | - if (!staticFiles.TryGetValue(filename, out var sf)) |
39 | | - return Results.NotFound(); |
40 | | - var ae = ctx.Request.Headers.AcceptEncoding.ToString(); |
41 | | - if (sf.br != null && ae.Contains("br")) |
| 23 | + options.ListenAnyIP(8081, lo => |
42 | 24 | { |
43 | | - ctx.Response.Headers.ContentEncoding = "br"; |
44 | | - return Results.Bytes(sf.br, sf.contentType); |
45 | | - } |
46 | | - if (sf.gz != null && ae.Contains("gzip")) |
47 | | - { |
48 | | - ctx.Response.Headers.ContentEncoding = "gzip"; |
49 | | - return Results.Bytes(sf.gz, sf.contentType); |
50 | | - } |
51 | | - return Results.Bytes(sf.data, sf.contentType); |
52 | | - }); |
53 | | -} |
| 25 | + lo.Protocols = HttpProtocols.Http1; |
| 26 | + lo.UseHttps(X509Certificate2.CreateFromPemFile(certPath, keyPath)); |
| 27 | + }); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +var app = builder.Build(); |
| 32 | + |
| 33 | +app.UseResponseCompression(); |
| 34 | + |
| 35 | +app.MapStaticAssets(); |
54 | 36 |
|
55 | 37 | app.UseServiceStack(new AppHost(), options => { |
56 | 38 | options.MapEndpoints(); |
|
0 commit comments