55using System . IO . Abstractions ;
66using System . Net ;
77using System . Runtime . InteropServices ;
8+ using System . Text ;
89using Documentation . Builder . Diagnostics . LiveMode ;
910using Elastic . Documentation . Configuration ;
1011using Elastic . Documentation . Configuration . Versions ;
1112using Elastic . Documentation . Site . FileProviders ;
1213using Elastic . Documentation . Tooling ;
13- using Elastic . Markdown . Exporters ;
1414using Elastic . Markdown . IO ;
15- using Elastic . Markdown . Myst . Renderers ;
16- using Markdig . Syntax ;
1715using Microsoft . AspNetCore . Builder ;
1816using Microsoft . AspNetCore . Hosting ;
1917using Microsoft . AspNetCore . Http ;
@@ -92,60 +90,9 @@ public async Task StopAsync(Cancel ctx)
9290 private void SetUpRoutes ( )
9391 {
9492 _ = _webApplication
95- . UseExceptionHandler ( options =>
96- {
97- options . Run ( async context =>
98- {
99- try
100- {
101- var exception = context . Features . Get < Microsoft . AspNetCore . Diagnostics . IExceptionHandlerFeature > ( ) ;
102- if ( exception != null )
103- {
104- var logger = context . RequestServices . GetRequiredService < ILogger < DocumentationWebHost > > ( ) ;
105- logger . LogError (
106- exception . Error ,
107- "Unhandled exception processing request {Path}. Error: {Error}\n Stack Trace: {StackTrace}\n Inner Exception: {InnerException}" ,
108- context . Request . Path ,
109- exception . Error . Message ,
110- exception . Error . StackTrace ,
111- exception . Error . InnerException ? . ToString ( ) ?? "None"
112- ) ;
113- logger . LogError (
114- "Request Details - Method: {Method}, Path: {Path}, QueryString: {QueryString}" ,
115- context . Request . Method ,
116- context . Request . Path ,
117- context . Request . QueryString
118- ) ;
119-
120- context . Response . StatusCode = 500 ;
121- context . Response . ContentType = "text/html" ;
122- await context . Response . WriteAsync ( @"
123- <html>
124- <head><title>Error</title></head>
125- <body>
126- <h1>Internal Server Error</h1>
127- <p>An error occurred while processing your request.</p>
128- <p>Please check the application logs for more details.</p>
129- </body>
130- </html>" ) ;
131- }
132- }
133- catch ( Exception handlerEx )
134- {
135- var logger = context . RequestServices . GetRequiredService < ILogger < DocumentationWebHost > > ( ) ;
136- logger . LogCritical (
137- handlerEx ,
138- "Error handler failed to process exception. Handler Error: {Error}\n Stack Trace: {StackTrace}" ,
139- handlerEx . Message ,
140- handlerEx . StackTrace
141- ) ;
142- context . Response . StatusCode = 500 ;
143- context . Response . ContentType = "text/plain" ;
144- await context . Response . WriteAsync ( "A critical error occurred." ) ;
145- }
146- } ) ;
147- } )
148- . UseLiveReload ( )
93+ . UseLiveReloadWithManualScriptInjection ( _webApplication . Lifetime )
94+ . UseDeveloperExceptionPage ( new DeveloperExceptionPageOptions ( ) )
95+ //.UseMiddleware<NoInjectLiveReloadMiddleware>()
14996 . UseStaticFiles (
15097 new StaticFileOptions
15198 {
@@ -169,37 +116,34 @@ await context.Response.WriteAsync(@"
169116
170117 private async Task < IResult > ServeApiFile ( ReloadableGeneratorState holder , string slug , Cancel ctx )
171118 {
172- #if DEBUG
173- // only reload when actually debugging
174- if ( System . Diagnostics . Debugger . IsAttached )
175- await holder . ReloadApiReferences ( ctx ) ;
176- #endif
119+ var x = LiveReloadConfiguration . Current . LiveReloadScriptUrl ;
120+
177121 var path = Path . Combine ( holder . ApiPath . FullName , slug . Trim ( '/' ) , "index.html" ) ;
178122 var info = _writeFileSystem . FileInfo . New ( path ) ;
179123 if ( info . Exists )
180124 {
181125 //TODO STREAM
182126 var contents = await _writeFileSystem . File . ReadAllTextAsync ( info . FullName , ctx ) ;
183- return Results . Content ( contents , "text/html" ) ;
127+ return LiveReloadHtml ( contents , Encoding . UTF8 , 200 ) ;
184128 }
185129
186130 return Results . NotFound ( ) ;
187131 }
188132
189133 private static async Task < IResult > ServeDocumentationFile ( ReloadableGeneratorState holder , string slug , Cancel ctx )
190134 {
135+ if ( slug == ".well-known/appspecific/com.chrome.devtools.json" )
136+ return Results . NotFound ( ) ;
137+
191138 var generator = holder . Generator ;
192139 const string navPartialSuffix = ".nav.html" ;
193140
194141 // Check if the original request is asking for LLM-rendered markdown
195142 var requestLlmMarkdown = slug . EndsWith ( ".md" ) ;
196- var originalSlug = slug ;
197143
198144 // If requesting .md output, remove the .md extension to find the source file
199145 if ( requestLlmMarkdown )
200- {
201146 slug = slug [ ..^ 3 ] ; // Remove ".md" extension
202- }
203147
204148 if ( slug . EndsWith ( navPartialSuffix ) )
205149 {
@@ -238,12 +182,9 @@ private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorSta
238182 var llmRendered = await generator . RenderLlmMarkdown ( markdown , ctx ) ;
239183 return Results . Content ( llmRendered , "text/markdown; charset=utf-8" ) ;
240184 }
241- else
242- {
243- // Regular HTML rendering
244- var rendered = await generator . RenderLayout ( markdown , ctx ) ;
245- return Results . Content ( rendered . Html , "text/html" ) ;
246- }
185+ // Regular HTML rendering
186+ var rendered = await generator . RenderLayout ( markdown , ctx ) ;
187+ return LiveReloadHtml ( rendered . Html ) ;
247188
248189 case ImageFile image :
249190 return Results . File ( image . SourceFile . FullName , image . MimeType ) ;
@@ -261,4 +202,17 @@ private static async Task<IResult> ServeDocumentationFile(ReloadableGeneratorSta
261202 return Results . Content ( renderedNotFound . Html , "text/html" , null , ( int ) HttpStatusCode . NotFound ) ;
262203 }
263204 }
205+
206+ private static IResult LiveReloadHtml ( string content , Encoding ? encoding = null , int ? statusCode = null )
207+ {
208+ if ( LiveReloadConfiguration . Current . LiveReloadEnabled )
209+ {
210+ //var script = WebsocketScriptInjectionHelper.GetWebSocketClientJavaScript(context, true);
211+ //var html = $"<script>\n{script}\n</script>";
212+ var html = "\n " + $@ "<script src=""{ LiveReloadConfiguration . Current . LiveReloadScriptUrl } "" defer></script>";
213+ content += html ;
214+ }
215+
216+ return Results . Content ( content , "text/html" , encoding , statusCode ) ;
217+ }
264218}
0 commit comments