internal static IApplicationBuilder UseHisar <TStartup>(this IApplicationBuilder app)
        {
            ThrowIfServiceNotRegistered(app.ApplicationServices);
            app.UseStaticFiles();

            var  componentHelper     = GetComponentHelper(app.ApplicationServices);
            bool isExternalComponent = componentHelper.IsExternalComponent;

            if (isExternalComponent)
            {
                app.UseStaticFiles(new StaticFileOptions()
                {
                    FileProvider = new StaticCliFileProvider()
                });
            }
            else
            {
                // for registered components
                var assemblyLoader = app.ApplicationServices.GetRequiredService <HisarAssemblyComponentsLoader>();
                var assemblyFileProviderFactory = app.ApplicationServices.GetRequiredService <IAssemblyFileProviderFactory>();
                foreach (var lookup in assemblyLoader.ComponentAssemblyLookup)
                {
                    var assemblyName = lookup.Value.GetName().Name;
                    app.UseStaticFiles(new StaticFileOptions()
                    {
                        FileProvider = assemblyFileProviderFactory.CreateFileProvider(lookup.Value),
                        RequestPath  = $"/{lookup.Key}"
                    });
                }

                var componentsRoutesDictionary = new Dictionary <string, IList <IRouter> >();
                foreach (KeyValuePair <string, HisarConventionBasedStartup> entry in assemblyLoader.StartupLookup)
                {
                    var componentId = entry.Key;
                    var startup     = entry.Value;
                    startup.Configure(app);

                    // unique builder - route name handler
                    var componentRoutesBuilder = new RouteBuilder(app)
                    {
                        DefaultHandler = app.ApplicationServices.GetRequiredService <MvcRouteHandler>(),
                    };

                    startup.ConfigureRoutes(componentRoutesBuilder);
                    componentsRoutesDictionary.Add(entry.Key, componentRoutesBuilder.Routes);
                }

                app.UseMvc(routes =>
                {
                    ConfigureRoutes(routes, componentsRoutesDictionary);

                    // add hosting component default routes
                    var startupType     = typeof(TStartup);
                    var configureRoutes = StartupTypeLoader.GetConfigureRoutesMethod(startupType);
                    configureRoutes?.Invoke(null, new object[] { routes });
                });
            }

            return(app);
        }
Exemple #2
0
 public HisarConventionBasedStartup(StartupMethods methods, Type startupType, IServiceProvider sp)
     : base(methods)
 {
     _sp                     = sp;
     _startupType            = startupType;
     _configureRoutes        = StartupTypeLoader.GetConfigureRoutesMethod(_startupType);
     ConfigureRoutesDelegate = routes => ConfigureRoutes(routes);
 }