|
17 | 17 | using Microsoft.AspNetCore.Http;
|
18 | 18 | using System.Threading.Tasks;
|
19 | 19 |
|
20 |
| -namespace SharpLab.Server { |
21 |
| - public class Startup { |
22 |
| - // Chrome would limit to 10 mins I believe |
23 |
| - private static readonly TimeSpan CorsPreflightMaxAge = TimeSpan.FromHours(1); |
24 |
| - |
25 |
| - public void ConfigureServices(IServiceCollection services) { |
26 |
| - services.AddHttpClient(); |
27 |
| - services.AddCors(); |
28 |
| - services.AddControllers(); |
29 |
| - } |
| 20 | +namespace SharpLab.Server; |
30 | 21 |
|
31 |
| - public void ConfigureContainer(ContainerBuilder builder) { |
32 |
| - var assembly = Assembly.GetExecutingAssembly(); |
| 22 | +public class Startup { |
| 23 | + // Chrome would limit to 10 mins I believe |
| 24 | + private static readonly TimeSpan CorsPreflightMaxAge = TimeSpan.FromHours(1); |
33 | 25 |
|
34 |
| - builder |
35 |
| - .RegisterAssemblyModulesInDirectoryOf(assembly) |
36 |
| - .WhereFileMatches("SharpLab.*"); |
37 |
| - } |
| 26 | + public void ConfigureServices(IServiceCollection services) { |
| 27 | + services.AddHttpClient(); |
| 28 | + services.AddCors(); |
| 29 | + services.AddControllers(); |
| 30 | + } |
| 31 | + |
| 32 | + public void ConfigureContainer(ContainerBuilder builder) { |
| 33 | + var assembly = Assembly.GetExecutingAssembly(); |
| 34 | + |
| 35 | + builder |
| 36 | + .RegisterAssemblyModulesInDirectoryOf(assembly) |
| 37 | + .WhereFileMatches("SharpLab.*"); |
| 38 | + } |
38 | 39 |
|
39 |
| - public static MirrorSharpOptions CreateMirrorSharpOptions(ILifetimeScope container) { |
40 |
| - var options = new MirrorSharpOptions { |
41 |
| - IncludeExceptionDetails = true, |
42 |
| - StatusTestCommands = { |
43 |
| - ('O', "x-optimize=debug,x-target=C#,x-no-cache=true,language=C#"), |
44 |
| - ('R', "0:0:0::using System; public class C { public void M() { } }"), |
45 |
| - ('U', "") |
46 |
| - } |
47 |
| - }; |
48 |
| - var languages = container.Resolve<ILanguageAdapter[]>(); |
49 |
| - foreach (var language in languages) { |
50 |
| - language.SlowSetup(options); |
| 40 | + public static MirrorSharpOptions CreateMirrorSharpOptions(ILifetimeScope container) { |
| 41 | + var options = new MirrorSharpOptions { |
| 42 | + IncludeExceptionDetails = true, |
| 43 | + StatusTestCommands = { |
| 44 | + ('O', "x-optimize=debug,x-target=C#,x-no-cache=true,language=C#"), |
| 45 | + ('R', "0:0:0::using System; public class C { public void M() { } }"), |
| 46 | + ('U', "") |
51 | 47 | }
|
52 |
| - PerformanceLog.Checkpoint("Startup.CreateMirrorSharpOptions.End"); |
53 |
| - return options; |
| 48 | + }; |
| 49 | + var languages = container.Resolve<ILanguageAdapter[]>(); |
| 50 | + foreach (var language in languages) { |
| 51 | + language.SlowSetup(options); |
54 | 52 | }
|
| 53 | + PerformanceLog.Checkpoint("Startup.CreateMirrorSharpOptions.End"); |
| 54 | + return options; |
| 55 | + } |
55 | 56 |
|
56 |
| - public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env) { |
| 57 | + public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env) { |
| 58 | + if (env.IsDevelopment()) |
| 59 | + app.UseDeveloperExceptionPage(); |
| 60 | + |
| 61 | + app.UseRouting(); |
| 62 | + app.UseCors(p => p |
| 63 | + .AllowAnyHeader() |
| 64 | + .AllowAnyOrigin() |
| 65 | + .AllowAnyMethod() |
| 66 | + .SetPreflightMaxAge(CorsPreflightMaxAge) |
| 67 | + ); |
| 68 | + |
| 69 | + app.UseWebSockets(); |
| 70 | + app.MapMirrorSharp("/mirrorsharp", CreateMirrorSharpOptions(app.ApplicationServices.GetAutofacRoot())); |
| 71 | + |
| 72 | + app.UseEndpoints(e => { |
| 73 | + MapStatus(e); |
| 74 | + MapBranchVersion(e, env); |
57 | 75 | if (env.IsDevelopment())
|
58 |
| - app.UseDeveloperExceptionPage(); |
59 |
| - |
60 |
| - app.UseRouting(); |
61 |
| - app.UseCors(p => p |
62 |
| - .AllowAnyHeader() |
63 |
| - .AllowAnyOrigin() |
64 |
| - .AllowAnyMethod() |
65 |
| - .SetPreflightMaxAge(CorsPreflightMaxAge) |
66 |
| - ); |
67 |
| - |
68 |
| - app.UseWebSockets(); |
69 |
| - app.MapMirrorSharp("/mirrorsharp", CreateMirrorSharpOptions(app.ApplicationServices.GetAutofacRoot())); |
70 |
| - |
71 |
| - app.UseEndpoints(e => { |
72 |
| - MapStatus(e); |
73 |
| - MapBranchVersion(e, env); |
74 |
| - if (env.IsDevelopment()) |
75 |
| - MapFeatureFlags(e); |
76 |
| - MapOtherEndpoints(e); |
77 |
| - |
78 |
| - e.MapControllers(); |
79 |
| - }); |
80 |
| - } |
| 76 | + MapFeatureFlags(e); |
| 77 | + MapOtherEndpoints(e); |
81 | 78 |
|
82 |
| - private void MapStatus(IEndpointRouteBuilder e) { |
83 |
| - var okBytes = new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes("OK")); |
84 |
| - e.MapGet("/status", context => { |
85 |
| - context.Response.ContentType = MediaTypeNames.Text.Plain; |
86 |
| - return WriteResponseBodyAsync(context, okBytes); |
87 |
| - }); |
88 |
| - } |
| 79 | + e.MapControllers(); |
| 80 | + }); |
| 81 | + } |
89 | 82 |
|
90 |
| - private void MapFeatureFlags(IEndpointRouteBuilder e) { |
91 |
| - e.MapGet("/featureflags/{key:alpha}", static context => { |
92 |
| - var key = (string)context.GetRouteValue("key")!; |
93 |
| - var featureFlagClient = context.RequestServices.GetRequiredService<IFeatureFlagClient>(); |
| 83 | + private void MapStatus(IEndpointRouteBuilder e) { |
| 84 | + var okBytes = new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes("OK")); |
| 85 | + e.MapGet("/status", context => { |
| 86 | + context.Response.ContentType = MediaTypeNames.Text.Plain; |
| 87 | + return WriteResponseBodyAsync(context, okBytes); |
| 88 | + }); |
| 89 | + } |
94 | 90 |
|
95 |
| - return context.Response.WriteAsync( |
96 |
| - featureFlagClient.GetInt32Flag(key)?.ToString() ?? "<null>", |
97 |
| - context.RequestAborted |
98 |
| - ); |
99 |
| - }); |
100 |
| - } |
| 91 | + private void MapFeatureFlags(IEndpointRouteBuilder e) { |
| 92 | + e.MapGet("/featureflags/{key:alpha}", static context => { |
| 93 | + var key = (string)context.GetRouteValue("key")!; |
| 94 | + var featureFlagClient = context.RequestServices.GetRequiredService<IFeatureFlagClient>(); |
101 | 95 |
|
102 |
| - // Temporary: until build is updated to something better than a json file on site itself |
103 |
| - protected virtual void MapBranchVersion(IEndpointRouteBuilder endpoints, IWebHostEnvironment env) { |
104 |
| - var file = env.WebRootFileProvider.GetFileInfo("branch-version.json"); |
105 |
| - if (!file.Exists) |
106 |
| - return; |
107 |
| - |
108 |
| - using var stream = file.CreateReadStream(); |
109 |
| - var bytes = new byte[stream.Length]; |
110 |
| - stream.Read(bytes, 0, bytes.Length); |
111 |
| - endpoints.MapGet("/branch-version.json", context => { |
112 |
| - context.Response.ContentType = MediaTypeNames.Application.Json; |
113 |
| - return WriteResponseBodyAsync(context, bytes); |
114 |
| - }); |
115 |
| - } |
| 96 | + return context.Response.WriteAsync( |
| 97 | + featureFlagClient.GetInt32Flag(key)?.ToString() ?? "<null>", |
| 98 | + context.RequestAborted |
| 99 | + ); |
| 100 | + }); |
| 101 | + } |
116 | 102 |
|
117 |
| - private Task WriteResponseBodyAsync(HttpContext context, ReadOnlyMemory<byte> body) { |
118 |
| - var writeTask = context.Response.BodyWriter.WriteAsync(body, context.RequestAborted); |
119 |
| - return writeTask.IsCompletedSuccessfully |
120 |
| - ? Task.CompletedTask |
121 |
| - : writeTask.AsTask(); |
122 |
| - } |
| 103 | + // Temporary: until build is updated to something better than a json file on site itself |
| 104 | + protected virtual void MapBranchVersion(IEndpointRouteBuilder endpoints, IWebHostEnvironment env) { |
| 105 | + var file = env.WebRootFileProvider.GetFileInfo("branch-version.json"); |
| 106 | + if (!file.Exists) |
| 107 | + return; |
| 108 | + |
| 109 | + using var stream = file.CreateReadStream(); |
| 110 | + var bytes = new byte[stream.Length]; |
| 111 | + stream.ReadExactly(bytes, 0, bytes.Length); |
| 112 | + endpoints.MapGet("/branch-version.json", context => { |
| 113 | + context.Response.ContentType = MediaTypeNames.Application.Json; |
| 114 | + return WriteResponseBodyAsync(context, bytes); |
| 115 | + }); |
| 116 | + } |
123 | 117 |
|
124 |
| - protected virtual void MapOtherEndpoints(IEndpointRouteBuilder endpoints) { |
125 |
| - } |
| 118 | + private Task WriteResponseBodyAsync(HttpContext context, ReadOnlyMemory<byte> body) { |
| 119 | + var writeTask = context.Response.BodyWriter.WriteAsync(body, context.RequestAborted); |
| 120 | + return writeTask.IsCompletedSuccessfully |
| 121 | + ? Task.CompletedTask |
| 122 | + : writeTask.AsTask(); |
| 123 | + } |
| 124 | + |
| 125 | + protected virtual void MapOtherEndpoints(IEndpointRouteBuilder endpoints) { |
126 | 126 | }
|
127 | 127 | }
|
0 commit comments