Ejemplo n.º 1
0
        private static async Task Invoke(FunctionHandler function, HttpContext context)
        {
            var funcHandler = function.GetType().GetMethod("Handle");

            HttpMethodAttribute attr = (HttpMethodAttribute)funcHandler.GetCustomAttributes(
                typeof(HttpMethodAttribute), true).FirstOrDefault();

            string method = attr?.HttpMethods.FirstOrDefault() ?? "Post";

            if (string.Compare(method, context.Request.Method, true) != 0)
            {
                context.Response.StatusCode = 405;
                await context.Response.WriteAsync($"405 - {context.Request.Method} method allowed");

                return;
            }

            try
            {
                var(status, text) = await function.Handle(context.Request);

                context.Response.StatusCode = status;

                if (!string.IsNullOrEmpty(text))
                {
                    if (context.Request.ContentType.Contains("application/json"))
                    {
                        context.Response.Headers["Content-Type"] = "application/json";
                    }
                    else
                    {
                        context.Response.Headers["Content-Type"] = "plain/text";
                    }

                    await context.Response.WriteAsync(text);
                }
            }
            catch (NotImplementedException nie)
            {
                context.Response.StatusCode = 501;
                await context.Response.WriteAsync(nie.ToString());
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, FunctionHandler function)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.Map("/", async context =>
                {
                    await Invoke(function, context);
                });
            });
        }