using System; using System.Collections.Concurrent; using System.Linq; using System.Threading; using System.Threading.Tasks; using EmbedIO.Internal; using EmbedIO.Utilities; namespace EmbedIO { /// /// Groups modules under a common base URL path. /// The BaseRoute property /// of modules contained in a ModuleGroup is relative to the /// ModuleGroup's BaseRoute property. /// For example, given the following code: /// new ModuleGroup("/download") /// .WithStaticFilesAt("/docs", "/var/my/documents"); /// files contained in the /var/my/documents folder will be /// available to clients under the /download/docs/ URL. /// /// /// /// public class ModuleGroup : WebModuleBase, IDisposable, IWebModuleContainer, IMimeTypeCustomizer { private readonly WebModuleCollection _modules; private readonly MimeTypeCustomizer _mimeTypeCustomizer = new MimeTypeCustomizer(); /// /// Initializes a new instance of the class. /// /// The base route served by this module. /// The value to set the property to. /// See the help for the property for more information. /// /// public ModuleGroup(string baseRoute, bool isFinalHandler) : base(baseRoute) { IsFinalHandler = isFinalHandler; _modules = new WebModuleCollection(nameof(ModuleGroup)); } /// /// Finalizes an instance of the class. /// ~ModuleGroup() { Dispose(false); } /// public sealed override bool IsFinalHandler { get; } /// public IComponentCollection Modules => _modules; /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } string IMimeTypeProvider.GetMimeType(string extension) => _mimeTypeCustomizer.GetMimeType(extension); bool IMimeTypeProvider.TryDetermineCompression(string mimeType, out bool preferCompression) => _mimeTypeCustomizer.TryDetermineCompression(mimeType, out preferCompression); /// public void AddCustomMimeType(string extension, string mimeType) => _mimeTypeCustomizer.AddCustomMimeType(extension, mimeType); /// public void PreferCompression(string mimeType, bool preferCompression) => _mimeTypeCustomizer.PreferCompression(mimeType, preferCompression); /// protected override Task OnRequestAsync(IHttpContext context) => _modules.DispatchRequestAsync(context); /// /// Releases unmanaged and - optionally - managed resources. /// /// to release both managed and unmanaged resources; /// to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (!disposing) return; _modules.Dispose(); } /// protected override void OnBeforeLockConfiguration() { base.OnBeforeLockConfiguration(); _mimeTypeCustomizer.Lock(); } /// protected override void OnStart(CancellationToken cancellationToken) { _modules.StartAll(cancellationToken); } } }