Exemple #1
0
        private static IContainer BuildContainer(Options options)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var builder = new ContainerBuilder();

            var settings = new SettingsRepository(options.MplayerExecutablePath, options.LibraryPath);

            builder.RegisterInstance<ISettingsRepository>(settings);
            builder.RegisterType<SongRepository>().As<ISongRepository>().SingleInstance();

            builder.RegisterType<SongRunner>().SingleInstance();

            builder.RegisterWebApiModelBinderProvider();

            builder.RegisterApiControllers(assembly);

            return builder.Build();
        }
Exemple #2
0
        private static void RunOwin(Options options)
        {
            var owinOptions = new StartOptions("http://*:8888")
            {
                ServerFactory = "Microsoft.Owin.Host.HttpListener"
            };

            using (WebApp.Start(owinOptions, appBuilder =>
            {
                // WA
                var config = new HttpConfiguration();
                config.MapHttpAttributeRoutes();
                config.EnableSwagger(c => c.SingleApiVersion("v1", "K")).EnableSwaggerUi();
                config.EnableCors(new EnableCorsAttribute("*", null, "*", "Content-Type,Link"));

                // Autofac
                var container = BuildContainer(options);
                config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

                appBuilder.UseCors(CorsOptions.AllowAll);

                appBuilder.UseAutofacMiddleware(container);
                appBuilder.UseAutofacWebApi(config);
                appBuilder.UseWebApi(config);
                appBuilder.UseFileServer(new FileServerOptions
                {
                    FileSystem = new PhysicalFileSystem(options.WebAppPath),
                    RequestPath = new PathString("/app"),
                    DefaultFilesOptions = { DefaultFileNames = new[] { "index.html" } }
                });

                appBuilder.Use(async (cx, next) =>
                {
                    var appPath = Host + "app";

                    cx.Response.ContentType = "text/html";

                    await cx.Response.WriteAsync(string.Format(
                        Resources.BlankPageTemplate,
                        BarcodeGenerator.GeneratePngDataUri(appPath, 800, 800),
                        appPath));
                });

                var settings = container.Resolve<ISettingsRepository>();
                var songs = container.Resolve<ISongRepository>();

                songs.Reindex(settings.ListPaths().ToArray());

                var runner = container.Resolve<SongRunner>();
                runner.Start();

            }))
            {
                PrintFlair();

                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write(Resources.ListeningOnText);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(Host);

                try
                {
                    Process.Start(Host);
                }
                catch (Exception)
                {
                    // ignored
                }

                Console.ReadKey();
            }
        }