Example #1
0
        public static int Main(string[] args)
        {
            try
            {
                Configuration = new ConfigurationBuilder()
                                .AddEnvironmentVariables()
                                .AddJsonFile("appsettings.json")
                                .AddCommandLine(
                    args,
                    // We provide explicit aliases for those command line
                    // options that specify client information, matching
                    // the placeholder options that we define below.
                    new Dictionary <string, string>()
                {
                    ["--user-agent"]  = "IQSHARP_USER_AGENT",
                    ["--hosting-env"] = "IQSHARP_HOSTING_ENV"
                }
                    )
                                .Build();

                var app = new KernelApplication(
                    Jupyter.Constants.IQSharpKernelProperties, new Startup().ConfigureServices
                    )
                          .WithKernelSpecResources <Jupyter.IQSharpEngine>(
                    new Dictionary <string, string>
                {
                    ["logo-64x64.png"] = "Microsoft.Quantum.IQSharp.Jupyter.res.logo-64x64.png",
                    ["kernel.js"]      = "Microsoft.Quantum.IQSharp.Jupyter.res.kernel.js"
                }
                    );
                app.Command(
                    "server",
                    cmd =>
                {
                    cmd.HelpOption();
                    cmd.Description = $"Runs IQSharp as an HTTP server.";
                    cmd.OnExecute(() =>
                    {
                        WebHost.CreateDefaultBuilder(args)
                        .UseUrls("http://localhost:8888")
                        .UseStartup <Startup>()
                        .Build()
                        .Run();
                        return(0);
                    });
                }
                    );

                AddWorkspaceOption(
                    app
                    .AddInstallCommand()
                    .AddKernelCommand(
                        // These command options will be consumed by the Program.Configuration
                        // object above, rather than by the kernel application object itself.
                        // Thus, we only need placeholders to prevent the kernel application
                        // from raising an exception when unrecognized options are passed.
                        kernelCmd => {
                    kernelCmd.Option <string>(
                        "--user-agent <AGENT>",
                        "Specifies which user agent has initiated this kernel instance.",
                        CommandOptionType.SingleValue
                        );
                    kernelCmd.Option <string>(
                        "--hosting-env <ENV>",
                        "Specifies the hosting environment that this kernel is being run in.",
                        CommandOptionType.SingleValue
                        );
                }
                        )
                    );

#if TELEMETRY
                Telemetry.Start(app, Configuration);
#endif

                return(app.Execute(args));
            }
            catch (CommandParsingException)
            {
                return(1);
            }
        }
Example #2
0
        public static int Main(string[] args)
        {
            try
            {
                Configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .AddCommandLine(
                    args,
                    // We provide explicit aliases for those command line
                    // options that specify client information, matching
                    // the placeholder options that we define below.
                    new Dictionary <string, string>()
                {
                    ["--user-agent"]  = "UserAgent",
                    ["--hosting-env"] = "HostingEnvironment"
                }
                    )
                                .Add(new NormalizedEnvironmentVariableConfigurationSource
                {
                    Prefix  = "IQSHARP_",
                    Aliases = new Dictionary <string, string>
                    {
                        ["USER_AGENT"]  = "UserAgent",
                        ["HOSTING_ENV"] = "HostingEnvironment",
                        ["LOG_PATH"]    = "LogPath"
                    }
                })
                                .Build();

                var app = new KernelApplication(
                    Jupyter.Constants.IQSharpKernelProperties, new Startup().ConfigureServices
                    )
                          .ConfigureLogging(
                    loggingBuilder => {
                    // As per https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#access-configuration-during-startup,
                    // we need to use an IConfiguration instance directly during
                    // startup, rather than using the nice binding methods
                    // like serviceCollection.Configure<TOptions>(configuration).
                    var options = Configuration.Get <LoggingOptions>();
                    if (options?.LogPath != null && options.LogPath.Length != 0)
                    {
                        loggingBuilder.AddFile(
                            options.LogPath,
                            minimumLevel: LogLevel.Debug
                            );
                    }
                }
                    )
                          .WithKernelSpecResources <Jupyter.IQSharpEngine>(
                    new Dictionary <string, string>
                {
                    ["logo-64x64.png"] = "Microsoft.Quantum.IQSharp.Jupyter.res.logo-64x64.png",
                    ["kernel.js"]      = "Microsoft.Quantum.IQSharp.Jupyter.res.kernel.js"
                }
                    );
                app.Command(
                    "server",
                    cmd =>
                {
                    cmd.HelpOption();
                    cmd.Description = $"Runs IQSharp as an HTTP server.";
                    cmd.OnExecute(() =>
                    {
                        GetHttpServer(args).Run();
                        return(0);
                    });
                }
                    );

                AddWorkspaceOption(
                    app
                    .AddInstallCommand()
                    .AddKernelCommand(
                        // These command options will be consumed by the Program.Configuration
                        // object above, rather than by the kernel application object itself.
                        // Thus, we only need placeholders to prevent the kernel application
                        // from raising an exception when unrecognized options are passed.
                        kernelCmd => {
                    kernelCmd.Option <string>(
                        "--user-agent <AGENT>",
                        "Specifies which user agent has initiated this kernel instance.",
                        CommandOptionType.SingleValue
                        );
                    kernelCmd.Option <string>(
                        "--hosting-env <ENV>",
                        "Specifies the hosting environment that this kernel is being run in.",
                        CommandOptionType.SingleValue
                        );
                }
                        )
                    );

#if TELEMETRY
                Telemetry.Start(app, Configuration);
#endif

                return(app.Execute(args));
            }
            catch (CommandParsingException)
            {
                return(1);
            }
        }