示例#1
0
        private async Task handleRequest(HttpContext context)
        {
            var syncIoFeature = context.Features.Get <IHttpBodyControlFeature>();

            if (syncIoFeature != null)
            {
                syncIoFeature.AllowSynchronousIO = true;
            }

            if (context.Request.Path.Value.Equals("/script"))
            {
                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = "application/javascript";
                using var stream             = GetType().Assembly.GetManifestResourceStream($"{typeof(ConfigurationPage).Namespace}.script.js");
                await stream.CopyToAsync(context.Response.Body);

                return;
            }

            if (context.Request.Path.Value.Equals("/styles"))
            {
                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = "text/css";
                using var stream             = GetType().Assembly.GetManifestResourceStream($"{typeof(ConfigurationPage).Namespace}.styles.css");
                await stream.CopyToAsync(context.Response.Body);

                return;
            }

            var authorized = _options.Authorization?.Authorize(context);

            if (authorized.HasValue && !authorized.Value)
            {
                context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }

            if (context.Request.Path.Value.Equals("/saveWorkerGoalCount"))
            {
                await processRequest(context, async() => await saveWorkerGoalCount(context));

                return;
            }

            if (context.Request.Path.Value.Equals("/saveMaxWorkersPerServer"))
            {
                await processRequest(context, async() => await saveMaxWorkersPerServer(context));

                return;
            }

            if (context.Request.Path.Value.Equals("/createNewServerConfiguration"))
            {
                await processRequest(context, async() => await createNewServerConfiguration(context));

                return;
            }

            if (context.Request.Path.Value.Equals("/activateServer"))
            {
                await processRequest(context, async() => { _configurationApi.ActivateServer(await parseConfigurationId(context)); });

                return;
            }

            if (context.Request.Path.Value.Equals("/inactivateServer"))
            {
                await processRequest(context, async() => { _configurationApi.InactivateServer(await parseConfigurationId(context)); });

                return;
            }

            if (!string.IsNullOrEmpty(context.Request.Path.Value))
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            await processRequest(context, async() =>
            {
                var page = new ConfigurationPage(_configuration, context.Request.PathBase.Value, _options);
                var html = page.ToString();
                await context.Response.WriteAsync(html);
            });
        }