| | 1 | |
|
| | 2 | | using Kestrun.Middleware; |
| | 3 | | using Serilog.Events; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Hosting; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Provides extension methods for configuring static file, default file, favicon, and file server middleware in Kestrun |
| | 9 | | /// </summary> |
| | 10 | | public static class KestrunHostStaticFilesExtensions |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Adds default files middleware to the application. |
| | 14 | | /// This middleware serves default files like index.html when a directory is requested. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 17 | | /// <param name="cfg">Configuration options for the default files middleware.</param> |
| | 18 | | /// <returns>The current KestrunHost instance.</returns> |
| | 19 | | public static KestrunHost AddDefaultFiles(this KestrunHost host, DefaultFilesOptions? cfg) |
| | 20 | | { |
| 2 | 21 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 22 | | { |
| 0 | 23 | | host.HostLogger.Debug("Adding Default Files with configuration: {@Config}", cfg); |
| | 24 | | } |
| | 25 | |
|
| 2 | 26 | | if (cfg == null) |
| | 27 | | { |
| 1 | 28 | | return host.AddDefaultFiles(); // no config, use defaults |
| | 29 | | } |
| | 30 | |
|
| | 31 | | // Convert DefaultFilesOptions to an Action<DefaultFilesOptions> |
| 1 | 32 | | return host.AddDefaultFiles(options => |
| 1 | 33 | | { |
| 0 | 34 | | CopyDefaultFilesOptions(cfg, options); |
| 1 | 35 | | }); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Adds default files middleware to the application. |
| | 40 | | /// This middleware serves default files like index.html when a directory is requested. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 43 | | /// <param name="cfg">Configuration options for the default files middleware.</param> |
| | 44 | | /// <returns>The current KestrunHost instance.</returns> |
| | 45 | | public static KestrunHost AddDefaultFiles(this KestrunHost host, Action<DefaultFilesOptions>? cfg = null) |
| | 46 | | { |
| 5 | 47 | | return host.Use(app => |
| 5 | 48 | | { |
| 1 | 49 | | var options = new DefaultFilesOptions(); |
| 1 | 50 | | cfg?.Invoke(options); |
| 1 | 51 | | _ = app.UseDefaultFiles(options); |
| 6 | 52 | | }); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Adds a directory browser middleware to the application. |
| | 57 | | /// This middleware enables directory browsing for a specified request path. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 60 | | /// <param name="requestPath">The request path to enable directory browsing for.</param> |
| | 61 | | /// <returns>The current KestrunHost instance.</returns> |
| | 62 | | public static KestrunHost AddDirectoryBrowser(this KestrunHost host, string requestPath) |
| | 63 | | { |
| 0 | 64 | | return host.Use(app => |
| 0 | 65 | | { |
| 0 | 66 | | _ = app.UseDirectoryBrowser(requestPath); |
| 0 | 67 | | }); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Adds a favicon middleware to the application. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 74 | | /// <param name="iconPath">The path to the favicon file. If null, uses the default favicon.</param> |
| | 75 | | /// <returns>The current KestrunHost instance.</returns> |
| | 76 | | public static KestrunHost AddFavicon(this KestrunHost host, string? iconPath = null) |
| | 77 | | { |
| 4 | 78 | | return host.Use(app => |
| 4 | 79 | | { |
| 2 | 80 | | _ = app.UseFavicon(iconPath); |
| 6 | 81 | | }); |
| | 82 | | } |
| | 83 | |
|
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Copies static file options from one object to another. |
| | 87 | | /// </summary> |
| | 88 | | /// <param name="src">The source static file options.</param> |
| | 89 | | /// <param name="dest">The destination static file options.</param> |
| | 90 | | /// <remarks> |
| | 91 | | /// This method copies properties from the source static file options to the destination static file options. |
| | 92 | | /// </remarks> |
| | 93 | | private static void CopyStaticFileOptions(StaticFileOptions? src, StaticFileOptions dest) |
| | 94 | | { |
| | 95 | | // If no source, return a new empty options object |
| 0 | 96 | | if (src == null || dest == null) |
| | 97 | | { |
| 0 | 98 | | return; |
| | 99 | | } |
| | 100 | | // Copy properties from source to destination |
| 0 | 101 | | dest.ContentTypeProvider = src.ContentTypeProvider; |
| 0 | 102 | | dest.OnPrepareResponse = src.OnPrepareResponse; |
| 0 | 103 | | dest.ServeUnknownFileTypes = src.ServeUnknownFileTypes; |
| 0 | 104 | | dest.DefaultContentType = src.DefaultContentType; |
| 0 | 105 | | dest.FileProvider = src.FileProvider; |
| 0 | 106 | | dest.RequestPath = src.RequestPath; |
| 0 | 107 | | dest.RedirectToAppendTrailingSlash = src.RedirectToAppendTrailingSlash; |
| 0 | 108 | | dest.HttpsCompression = src.HttpsCompression; |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Copies default files options from one object to another. |
| | 113 | | /// This method is used to ensure that the default files options are correctly configured. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="src">The source default files options.</param> |
| | 116 | | /// <param name="dest">The destination default files options.</param> |
| | 117 | | /// <remarks> |
| | 118 | | /// This method copies properties from the source default files options to the destination default files options. |
| | 119 | | /// </remarks> |
| | 120 | | private static void CopyDefaultFilesOptions(DefaultFilesOptions? src, DefaultFilesOptions dest) |
| | 121 | | { |
| | 122 | | // If no source, return a new empty options object |
| 0 | 123 | | if (src == null || dest == null) |
| | 124 | | { |
| 0 | 125 | | return; |
| | 126 | | } |
| | 127 | | // Copy properties from source to destination |
| 0 | 128 | | dest.DefaultFileNames.Clear(); |
| 0 | 129 | | foreach (var name in src.DefaultFileNames) |
| | 130 | | { |
| 0 | 131 | | dest.DefaultFileNames.Add(name); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | dest.FileProvider = src.FileProvider; |
| 0 | 135 | | dest.RequestPath = src.RequestPath; |
| 0 | 136 | | dest.RedirectToAppendTrailingSlash = src.RedirectToAppendTrailingSlash; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Adds a file server middleware to the application. |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 143 | | /// <param name="cfg">Configuration options for the file server middleware.</param> |
| | 144 | | /// <returns>The current KestrunHost instance.</returns> |
| | 145 | | /// <remarks> |
| | 146 | | /// This middleware serves static files and default files from a specified file provider. |
| | 147 | | /// If no configuration is provided, it uses default settings. |
| | 148 | | /// </remarks> |
| | 149 | | public static KestrunHost AddFileServer(this KestrunHost host, FileServerOptions? cfg) |
| | 150 | | { |
| 2 | 151 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 152 | | { |
| 0 | 153 | | host.HostLogger.Debug("Adding File Server with configuration: {@Config}", cfg); |
| | 154 | | } |
| | 155 | |
|
| 2 | 156 | | if (cfg == null) |
| | 157 | | { |
| 1 | 158 | | return host.AddFileServer(); // no config, use defaults |
| | 159 | | } |
| | 160 | |
|
| | 161 | | // Convert FileServerOptions to an Action<FileServerOptions> |
| 1 | 162 | | return host.AddFileServer(options => |
| 1 | 163 | | { |
| 0 | 164 | | options.EnableDefaultFiles = cfg.EnableDefaultFiles; |
| 0 | 165 | | options.EnableDirectoryBrowsing = cfg.EnableDirectoryBrowsing; |
| 0 | 166 | | options.FileProvider = cfg.FileProvider; |
| 0 | 167 | | options.RequestPath = cfg.RequestPath; |
| 0 | 168 | | options.RedirectToAppendTrailingSlash = cfg.RedirectToAppendTrailingSlash; |
| 0 | 169 | | CopyDefaultFilesOptions(cfg.DefaultFilesOptions, options.DefaultFilesOptions); |
| 0 | 170 | | if (cfg.DirectoryBrowserOptions != null) |
| 1 | 171 | | { |
| 0 | 172 | | options.DirectoryBrowserOptions.FileProvider = cfg.DirectoryBrowserOptions.FileProvider; |
| 0 | 173 | | options.DirectoryBrowserOptions.RequestPath = cfg.DirectoryBrowserOptions.RequestPath; |
| 0 | 174 | | options.DirectoryBrowserOptions.RedirectToAppendTrailingSlash = cfg.DirectoryBrowserOptions.RedirectToAp |
| 1 | 175 | | } |
| 1 | 176 | |
|
| 0 | 177 | | CopyStaticFileOptions(cfg.StaticFileOptions, options.StaticFileOptions); |
| 1 | 178 | | }); |
| | 179 | | } |
| | 180 | |
|
| | 181 | | /// <summary> |
| | 182 | | /// Adds a file server middleware to the application. |
| | 183 | | /// This middleware serves static files and default files from a specified file provider. |
| | 184 | | /// </summary> |
| | 185 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 186 | | /// <param name="cfg">Configuration options for the file server middleware.</param> |
| | 187 | | /// <returns>The current KestrunHost instance.</returns> |
| | 188 | | public static KestrunHost AddFileServer(this KestrunHost host, Action<FileServerOptions>? cfg = null) |
| | 189 | | { |
| 7 | 190 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 191 | | { |
| 0 | 192 | | host.HostLogger.Debug("Adding File Server with configuration: {@Config}", cfg); |
| | 193 | | } |
| | 194 | |
|
| 7 | 195 | | return host.Use(app => |
| 7 | 196 | | { |
| 2 | 197 | | var options = new FileServerOptions(); |
| 2 | 198 | | cfg?.Invoke(options); |
| 2 | 199 | | _ = app.UseFileServer(options); |
| 9 | 200 | | }); |
| | 201 | | } |
| | 202 | |
|
| | 203 | |
|
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Adds static files to the application. |
| | 207 | | /// This overload allows you to specify configuration options. |
| | 208 | | /// </summary> |
| | 209 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 210 | | /// <param name="cfg">The static file options to configure.</param> |
| | 211 | | /// <returns>The current KestrunHost instance.</returns> |
| | 212 | | public static KestrunHost AddStaticFiles(this KestrunHost host, Action<StaticFileOptions>? cfg = null) |
| | 213 | | { |
| 8 | 214 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 215 | | { |
| 0 | 216 | | host.HostLogger.Debug("Adding static files with configuration: {Config}", cfg); |
| | 217 | | } |
| | 218 | |
|
| 8 | 219 | | return host.Use(app => |
| 8 | 220 | | { |
| 3 | 221 | | if (cfg == null) |
| 8 | 222 | | { |
| 0 | 223 | | _ = app.UseStaticFiles(); |
| 8 | 224 | | } |
| 8 | 225 | | else |
| 8 | 226 | | { |
| 3 | 227 | | var options = new StaticFileOptions(); |
| 3 | 228 | | cfg(options); |
| 8 | 229 | |
|
| 3 | 230 | | _ = app.UseStaticFiles(options); |
| 8 | 231 | | } |
| 11 | 232 | | }); |
| | 233 | | } |
| | 234 | |
|
| | 235 | | /// <summary> |
| | 236 | | /// Adds static files to the application. |
| | 237 | | /// This overload allows you to specify configuration options. |
| | 238 | | /// </summary> |
| | 239 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | 240 | | /// <param name="options">The static file options to configure.</param> |
| | 241 | | /// <returns>The current KestrunHost instance.</returns> |
| | 242 | | public static KestrunHost AddStaticFiles(this KestrunHost host, StaticFileOptions options) |
| | 243 | | { |
| 2 | 244 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 245 | | { |
| 0 | 246 | | host.HostLogger.Debug("Adding static files with options: {@Options}", options); |
| | 247 | | } |
| | 248 | |
|
| 2 | 249 | | if (options == null) |
| | 250 | | { |
| 1 | 251 | | return host.AddStaticFiles(); // no options, use defaults |
| | 252 | | } |
| | 253 | |
|
| | 254 | | // reuse the delegate overload so the pipeline logic stays in one place |
| 1 | 255 | | return host.AddStaticFiles(o => |
| 1 | 256 | | { |
| 1 | 257 | | // copy only the properties callers are likely to set |
| 0 | 258 | | CopyStaticFileOptions(options, o); |
| 1 | 259 | | }); |
| | 260 | | } |
| | 261 | | } |