Skip to content

Commit

Permalink
Added support for internal controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeuchler committed Sep 30, 2021
1 parent b8bacb5 commit 9a3ffdd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 56 deletions.
45 changes: 45 additions & 0 deletions src/Moryx.Runtime.Kestrel/CustomControllerFeatureProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace Moryx.Runtime.Kestrel
{
/// <summary>
/// Custom override to allow internal containers and only controllers which are inherit <see cref="Controller"/>
/// </summary>
internal class CustomControllerFeatureProvider : ControllerFeatureProvider
{
private const string ControllerTypeNameSuffix = "Controller";

protected override bool IsController(TypeInfo typeInfo)
{
// Ignore interfaces
if (!typeInfo.IsClass)
return false;

// Ignore abstracts
if (typeInfo.IsAbstract)
return false;

// Ignore generics
if (typeInfo.ContainsGenericParameters)
return false;

// Ignore none controllers
if (typeInfo.IsDefined(typeof(NonControllerAttribute)))
return false;

// Controller must end with suffix OR must have the controller attribute
if (!typeInfo.Name.EndsWith(ControllerTypeNameSuffix, StringComparison.OrdinalIgnoreCase) &&
!typeInfo.IsDefined(typeof(ControllerAttribute)))
return false;

// Allow only Controllers
if (!typeof(Controller).IsAssignableFrom(typeInfo))
return false;

return true;
}
}
}
4 changes: 3 additions & 1 deletion src/Moryx.Runtime.Kestrel/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void ConfigureServices(IServiceCollection services)
})
.ConfigureApplicationPartManager(manager =>
{
// Find all Controllers and add them to the local container
// Find all assemblies with defined controllers and add them to the application parts
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies.Where(a =>
manager.ApplicationParts.All(p => p.Name != a.GetName().Name)))
Expand All @@ -36,6 +36,8 @@ public void ConfigureServices(IServiceCollection services)
manager.ApplicationParts.Add(new AssemblyPart(assembly));
}
}

manager.FeatureProviders.Add(new CustomControllerFeatureProvider());
})
.AddControllersAsServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Globalization;
using Moryx.Container;

#if USE_WCF
using System.ServiceModel;
#else
Expand All @@ -20,16 +21,15 @@ namespace Moryx.Runtime.Maintenance.Plugins.Common
[Plugin(LifeCycle.Transient, typeof(ICommonMaintenance))]
#if USE_WCF
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
public class CommonMaintenance : ICommonMaintenance
internal class CommonMaintenance : ICommonMaintenance
#else
[ApiController, Route(Endpoint), Produces("application/json")]
[Endpoint(Name = nameof(ICommonMaintenance), Version = "3.0.0")]
public class CommonMaintenance : Controller, ICommonMaintenance
internal class CommonMaintenance : Controller, ICommonMaintenance
#endif
{
internal const string Endpoint = "common";

/// <inheritdoc />
#if !USE_WCF
[HttpGet("time")]
#endif
Expand All @@ -41,7 +41,6 @@ public ServerTimeResponse GetServerTime()
};
}

/// <inheritdoc />
#if !USE_WCF
[HttpGet("info/application")]
#endif
Expand All @@ -57,7 +56,6 @@ public ApplicationInformationResponse GetApplicationInfo()
};
}

/// <inheritdoc />
#if !USE_WCF
[HttpGet("info/system")]
#endif
Expand All @@ -71,7 +69,6 @@ public HostInformationResponse GetHostInfo()
};
}

/// <inheritdoc />
#if !USE_WCF
[HttpGet("info/system/load")]
#endif
Expand All @@ -87,7 +84,6 @@ public SystemLoadResponse GetSystemLoad()
};
}

/// <inheritdoc />
#if !USE_WCF
[HttpGet("info/application/load")]
#endif
Expand Down
7 changes: 4 additions & 3 deletions src/Moryx.Runtime.Maintenance/Plugins/Common/HostHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static ulong PhysicalMemory()
var moc = mc.GetInstances();
return (from ManagementObject item in moc select Convert.ToUInt64(item.Properties["TotalPhysicalMemory"].Value)).FirstOrDefault();
#else
return 0; // TODO: Remove in Core 4
return 42; // TODO: Replace in Core 4
#endif
}

Expand All @@ -40,7 +40,7 @@ public static ulong FreePhysicalMemory()
var moc = mc.GetInstances();
return (from ManagementObject item in moc select Convert.ToUInt64(item.Properties["FreePhysicalMemory"].Value) * 1024).FirstOrDefault();
#else
return 0; // TODO: Remove in Core 4
return 32; // TODO: Replace in Core 4
#endif
}

Expand All @@ -58,7 +58,8 @@ public static ulong ProcessorTimePercentage(string processName = "_Total")

return (ulong)processorTime;
#else
return 0; // TODO: Remove in Core 4

return 0; // TODO: Replace in Core 4
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
using Moryx.Communication.Endpoints;
#endif


namespace Moryx.Runtime.Maintenance.Plugins.Databases
{

[Plugin(LifeCycle.Transient, typeof(IDatabaseMaintenance))]
#if USE_WCF
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
public class DatabaseMaintenance : IDatabaseMaintenance
internal class DatabaseMaintenance : IDatabaseMaintenance
#else
[ApiController, Route(Endpoint), Produces("application/json")]
[Endpoint(Name = nameof(IDatabaseMaintenance), Version = "3.0.0")]
public class DatabaseMaintenance : Controller, IDatabaseMaintenance
internal class DatabaseMaintenance : Controller, IDatabaseMaintenance
#endif
{
internal const string Endpoint = "databases";
Expand Down Expand Up @@ -59,7 +58,6 @@ public class DatabaseMaintenance : Controller, IDatabaseMaintenance

#endregion

/// <inheritdoc />
#if !USE_WCF
[HttpGet]
#endif
Expand All @@ -68,7 +66,6 @@ public DataModel[] GetAll()
return DbContextManager.Contexts.Select(Convert).ToArray();
}

/// <inheritdoc />
#if !USE_WCF
[HttpGet("model/{targetModel}")]
#endif
Expand All @@ -77,7 +74,6 @@ public DataModel GetModel(string targetModel)
return Convert(DbContextManager.Contexts.FirstOrDefault(context => TargetModelName(context) == targetModel));
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/config")]
#endif
Expand All @@ -93,7 +89,6 @@ public void SetDatabaseConfig(string targetModel, DatabaseConfigModel config)

}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/config/test")]
#endif
Expand All @@ -110,7 +105,6 @@ public TestConnectionResponse TestDatabaseConfig(string targetModel, DatabaseCon
return new TestConnectionResponse { Result = result };
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("createall")]
#endif
Expand All @@ -120,7 +114,6 @@ public InvocationResponse CreateAll()
return string.IsNullOrEmpty(bulkResult) ? new InvocationResponse() : new InvocationResponse(bulkResult);
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/create")]
#endif
Expand All @@ -146,7 +139,6 @@ public InvocationResponse CreateDatabase(string targetModel, DatabaseConfigModel
}
}

/// <inheritdoc />
#if !USE_WCF
[HttpDelete("/")]
#endif
Expand All @@ -156,7 +148,6 @@ public InvocationResponse EraseAll()
return string.IsNullOrEmpty(bulkResult) ? new InvocationResponse() : new InvocationResponse(bulkResult);
}

/// <inheritdoc />
#if !USE_WCF
[HttpDelete("model/{targetModel}")]
#endif
Expand All @@ -180,7 +171,6 @@ public InvocationResponse EraseDatabase(string targetModel, DatabaseConfigModel
}
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/dump")]
#endif
Expand All @@ -201,7 +191,6 @@ public InvocationResponse DumpDatabase(string targetModel, DatabaseConfigModel c
return new InvocationResponse();
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/restore")]
#endif
Expand All @@ -218,7 +207,6 @@ public InvocationResponse RestoreDatabase(string targetModel, RestoreDatabaseReq
return new InvocationResponse();
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/{migrationName}/migrate")]
#endif
Expand All @@ -232,7 +220,6 @@ public DatabaseUpdateSummary MigrateDatabaseModel(string targetModel, string mig
return targetConfigurator.MigrateDatabase(config, migrationName);
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/rollback")]
#endif
Expand All @@ -248,7 +235,6 @@ public InvocationResponse RollbackDatabase(string targetModel, DatabaseConfigMod
return rollbackResult ? new InvocationResponse() : new InvocationResponse("Error while rollback!");
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("model/{targetModel}/setup")]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace Moryx.Runtime.Maintenance.Plugins.Logging
[Plugin(LifeCycle.Transient, typeof(ILogMaintenance))]
#if USE_WCF
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
public class LogMaintenance : ILogMaintenance, ILoggingComponent
internal class LogMaintenance : ILogMaintenance, ILoggingComponent
#else
[ApiController, Route(Endpoint), Produces("application/json")]
[Endpoint(Name = nameof(ILogMaintenance), Version = "3.0.0")]
public class LogMaintenance : Controller, ILogMaintenance, ILoggingComponent
internal class LogMaintenance : Controller, ILogMaintenance, ILoggingComponent
#endif
{
internal const string Endpoint = "loggers";
Expand All @@ -34,15 +34,11 @@ public class LogMaintenance : Controller, ILogMaintenance, ILoggingComponent

public ILoggingAppender LoggingAppender { get; set; }

/// <summary>
/// Logger of this component
/// </summary>
[UseChild("WcfService")]
[UseChild("LogMaintenance")]
public IModuleLogger Logger { get; set; }

#endregion

/// <inheritdoc />
#if !USE_WCF
[HttpGet]
#endif
Expand All @@ -51,7 +47,6 @@ public LoggerModel[] GetAllLoggers()
return LoggerManagement.Select(Convert).ToArray();
}

/// <inheritdoc />
#if !USE_WCF
[HttpPut("logger/{loggerName}/loglevel")]
#endif
Expand All @@ -61,7 +56,6 @@ public InvocationResponse SetLogLevel(string loggerName, SetLogLevelRequest requ
return new InvocationResponse();
}

/// <inheritdoc />
#if !USE_WCF
[HttpPost("appender")]
#endif
Expand All @@ -73,7 +67,6 @@ public AddAppenderResponse AddAppender(AddAppenderRequest request)
};
}

/// <inheritdoc />
#if !USE_WCF
[HttpGet("appender/{appenderId}")]
#endif
Expand All @@ -87,7 +80,7 @@ public LogMessageModel[] GetMessages(string appenderId)
// ReSharper disable once PossibleNullReferenceException
ctx.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
#else
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
#endif
return new LogMessageModel[0];
}
Expand All @@ -96,7 +89,6 @@ public LogMessageModel[] GetMessages(string appenderId)
return messages.ToArray();
}

/// <inheritdoc />
#if !USE_WCF
[HttpDelete("appender/{appenderId}")]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Moryx.Runtime.Maintenance.Plugins.Logging
{
public interface ILoggingAppender : IMaintenancePlugin
internal interface ILoggingAppender : IMaintenancePlugin
{
/// <summary>
/// Add a remote appender to the logging stream
Expand Down
Loading

0 comments on commit 9a3ffdd

Please sign in to comment.