Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from goncalo-oliveira/develop
Browse files Browse the repository at this point in the history
added support for controller functions.
  • Loading branch information
goncalo-oliveira authored Feb 3, 2021
2 parents 18b0494 + c11c7f9 commit c3e7d4c
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 87 deletions.
64 changes: 64 additions & 0 deletions src/Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace OpenFaaS.Api
{
public class Startup : IPluginStartup
{
public Startup( IConfiguration configuration )
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices( IServiceCollection services )
{
var mvcBuilder = services.AddControllers( options =>
{
} );

mvcBuilder.AddNewtonsoftJson( options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
options.SerializerSettings.Converters.Add( new StringEnumConverter( new CamelCaseNamingStrategy(), false ) );
});
// Replaced with Newtonsoft because Microsoft's serializer doesn't do polymorphic serialization

mvcBuilder.AddPluginControllers();
}

public void Configure( IApplicationBuilder app, IWebHostEnvironment env )
{
if ( env.IsDevelopment() )
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

if ( !Configuration.GetValue<bool>( "Args:SkipAuth" ) )
{
app.UseAuthentication();
app.UseAuthorization();
}

// allow api implementation to customize the pipeline
app.ConfigurePlugin( env );

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
32 changes: 0 additions & 32 deletions src/FunctionServiceExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using OpenFaaS;
using System.Linq;

namespace OpenFaaS
namespace OpenFaaS.Functions
{
internal static class HttpFunctionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using OpenFaaS.Functions.Middleware;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OpenFaaS
namespace OpenFaaS.Functions
{
internal static class HttpMethodAttributeExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace OpenFaaS
namespace OpenFaaS.Functions
{
internal class HttpRequestHandlerOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
internal class HttpAuthenticationHandler
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
/// <summary>
/// Deals with authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
/// <summary>
/// Deals with authorization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Routing;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
/// <summary>
/// Deals with function execution and response handling
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Routing;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
internal interface IRouteMatcher
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
internal class RouteMatcher : IRouteMatcher
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace OpenFaaS
namespace OpenFaaS.Functions.Middleware
{
/// <summary>
/// Deals with routing and routing templates
Expand Down
62 changes: 62 additions & 0 deletions src/Functions/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Newtonsoft.Json.Serialization;

namespace OpenFaaS.Functions
{
public class Startup : IPluginStartup
{
public Startup( IConfiguration configuration )
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices( IServiceCollection services )
{
services.AddMvc()
.AddNewtonsoftJson( options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
} );
// Replaced with Newtonsoft because Microsoft's serializer doesn't do polymorphic serialization

services.AddSingleton<ISystemClock, SystemClock>();

// add root request handler to the container
services.AddSingleton<Middleware.IRouteMatcher, Middleware.RouteMatcher>();
services.TryAddScoped<Middleware.HttpAuthenticationHandler>();
services.Configure<HttpRequestHandlerOptions>( options =>
{
options.SkipAuth = Configuration.GetValue<bool>( "Args:SkipAuth" );

if ( options.SkipAuth )
{
Console.WriteLine( "Skipping authentication and authorization" );
}
} );
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure( IApplicationBuilder app, IWebHostEnvironment env )
{
app.UseDeveloperExceptionPage();

Console.WriteLine();
Console.WriteLine( "Running..." );

app.UseMiddleware<Middleware.RoutingMiddleware>()
.UseMiddleware<Middleware.AuthenticationMiddleware>()
.UseMiddleware<Middleware.AuthorizationMiddleware>()
.UseMiddleware<Middleware.FunctionMiddleware>();
}
}
}
13 changes: 13 additions & 0 deletions src/IPluginStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace OpenFaaS
{
public interface IPluginStartup
{
void ConfigureServices( IServiceCollection services );
void Configure( IApplicationBuilder app, IWebHostEnvironment env );
}
}
56 changes: 56 additions & 0 deletions src/PluginServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace OpenFaaS
{
internal static class PluginServiceExtensions
{
private static readonly string typeName = "OpenFaaS.Startup";

private static object startup;

public static void AddPluginControllers( this IMvcBuilder builder )
{
builder.AddApplicationPart( AssemblyResolver.Default.Assembly );
}

public static IServiceCollection ConfigurePluginServices( this IServiceCollection services, IConfiguration configuration )
{
startup = AssemblyResolver.Default.Assembly.CreateInstance( typeName, false, BindingFlags.CreateInstance, null, new object[]
{
configuration
}, null, null );

startup.GetType().InvokeMember( "ConfigureServices", BindingFlags.InvokeMethod, null, startup, new object[]
{
services
} );

return ( services );
}

public static IApplicationBuilder ConfigurePlugin( this IApplicationBuilder app, IWebHostEnvironment env )
{
var type = startup.GetType();

var configureMethod = type.GetMethod( "Configure", BindingFlags.Public | BindingFlags.Instance, null, new Type[]
{
typeof( IApplicationBuilder ), typeof( bool )
}, null );

// the Configure( IApplicationBuilder, bool ) method is optional

configureMethod?.Invoke( startup, new object[]
{
app, env.IsDevelopment()
} );

return ( app );
}
}
}
7 changes: 2 additions & 5 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ static void Main(string[] args)
Console.WriteLine("OpenFaaS ASPNET Function Loader");
Console.WriteLine();

//AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;

parsed.WithParsed( options =>
{
if ( options.Detach )
Expand Down Expand Up @@ -84,9 +82,8 @@ public static IHostBuilder CreateHostBuilder( string[] args, Options options ) =
} );
} );

webBuilder.UseKestrel()
.UseStartup<Startup>();

webBuilder.UseKestrel();
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls( $"http://*:{options.Port}" );
} );

Expand Down
Loading

0 comments on commit c3e7d4c

Please sign in to comment.