Ejemplo n.º 1
0
        /// <summary>
        /// test-proxy
        /// </summary>
        /// <param name="insecure">Allow untrusted SSL certs from upstream server</param>
        /// <param name="storageLocation">The path to the target local git repo. If not provided as an argument, Environment variable TEST_PROXY_FOLDER will be consumed. Lacking both, the current working directory will be utilized.</param>
        /// <param name="storagePlugin">Does the user have a preference as to a default storage plugin? Defaults to "No plugin" currently.</param>
        /// <param name="command">A specific test-proxy action to be carried out. Supported options: ["Save", "Restore", "Reset"]</param>
        /// <param name="assetsJsonPath">Only required if a "command" value is present. This should be a path to a valid assets.json within a language repository.</param>
        /// <param name="dump">Flag. Pass to dump configuration values before starting the application.</param>
        /// <param name="version">Flag. Pass to get the version of the tool.</param>
        /// <param name="args">Unmapped arguments un-used by the test-proxy are sent directly to the ASPNET configuration provider.</param>
        public static void Main(bool insecure = false, string storageLocation = null, string storagePlugin = null, string command = null, string assetsJsonPath = null, bool dump = false, bool version = false, string[] args = null)
        {
            if (version)
            {
                var assembly        = System.Reflection.Assembly.GetExecutingAssembly();
                var semanticVersion = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion;
                var assemblyVersion = assembly.GetName().Version;

                Console.WriteLine($"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}-dev.{semanticVersion}");

                Environment.Exit(0);
            }

            TargetLocation = resolveRepoLocation(storageLocation);
            Resolver       = new StoreResolver();
            DefaultStore   = Resolver.ResolveStore(storagePlugin ?? "NullStore");

            if (!String.IsNullOrWhiteSpace(command))
            {
                switch (command.ToLowerInvariant())
                {
                case "save":
                    DefaultStore.Push(assetsJsonPath, TargetLocation);
                    break;

                case "restore":
                    DefaultStore.Restore(assetsJsonPath, TargetLocation);
                    break;

                case "reset":
                    DefaultStore.Reset(assetsJsonPath, TargetLocation);
                    break;

                default:
                    throw new Exception($"One must provide a valid value for argument \"command\". \"{command}\" is not a valid option.");
                }
            }

            _insecure       = insecure;
            Regex.CacheSize = 0;

            var statusThreadCts = new CancellationTokenSource();

            var statusThread = PrintStatus(
                () => $"[{DateTime.UtcNow.ToString("HH:mm:ss")}] Recorded: {RequestsRecorded}\tPlayed Back: {RequestsPlayedBack}",
                newLine: true, statusThreadCts.Token);

            var host = Host.CreateDefaultBuilder(args);

            host.ConfigureWebHostDefaults(
                builder =>
                builder.UseStartup <Startup>()
                // ripped directly from implementation of ConfigureWebDefaults@https://github.dev/dotnet/aspnetcore/blob/a779227cc2694a50b074a097889ed9e80d15cd77/src/DefaultBuilder/src/WebHost.cs#L176
                .ConfigureLogging((hostBuilder, loggingBuilder) =>
            {
                loggingBuilder.ClearProviders();
                loggingBuilder.AddConfiguration(hostBuilder.Configuration.GetSection("Logging"));
                loggingBuilder.AddSimpleConsole(options =>
                {
                    options.TimestampFormat = "[HH:mm:ss] ";
                });
                loggingBuilder.AddDebug();
                loggingBuilder.AddEventSourceLogger();
            })
                .ConfigureKestrel(options =>
            {
                options.ConfigureEndpointDefaults(lo => lo.Protocols = HttpProtocols.Http1);
            })
                );

            var app = host.Build();

            if (dump)
            {
                var config = app.Services?.GetService <IConfiguration>();
                Console.WriteLine("Dumping Resolved Configuration Values:");
                if (config != null)
                {
                    foreach (var c in config.AsEnumerable())
                    {
                        Console.WriteLine(c.Key + " = " + c.Value);
                    }
                }
            }

            app.Run();

            statusThreadCts.Cancel();
            statusThread.Join();
        }