Skip to content

Commit

Permalink
Migrate Database-Maintenance endpoint to kestrel
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxantron committed Sep 27, 2021
1 parent f7d7800 commit 5db9a17
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Runtime.Serialization;
using Moryx.Configuration;
using Moryx.Modules;
#if USE_WCF
using Moryx.Tools.Wcf;
#endif

namespace Moryx.Runtime.Maintenance
{
Expand All @@ -18,11 +20,29 @@ public abstract class MaintenancePluginConfig : UpdatableEntry, IPluginConfig
/// Name of the plugin.
/// </summary>
public abstract string PluginName { get; }

/// <summary>
/// Endpoint which the plugin provides.
/// </summary>
[DataMember]
public HostConfig ProvidedEndpoint { get; set; }
}

#if !USE_WCF
/// <summary>
/// Config clone for compatibility
/// </summary>
public class HostConfig
{
public string Endpoint { get; set; }

public string BindingType { get; set; }

public bool RequiresAuthentification { get; set; }

public bool MetadataEnabled { get; set; }

public bool HelpEnabled { get; set; }
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0

using Moryx.Communication.Endpoints;
#if USE_WCF
using Moryx.Tools.Wcf;
#endif

namespace Moryx.Runtime.Maintenance
{
Expand Down
19 changes: 15 additions & 4 deletions src/Moryx.Runtime.Maintenance/Moryx.Runtime.Maintenance.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45</TargetFrameworks>
<TargetFrameworks>net45;net5.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Description>Core module to configure and control MORYX based applications</Description>
<CreatePackage>true</CreatePackage>
<PackageTags>MORYX;Runtime;Server;Configuration;UI</PackageTags>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
<DefineConstants>USE_WCF</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Moryx\Moryx.csproj" />
<ProjectReference Include="..\Moryx.Model\Moryx.Model.csproj" />
<ProjectReference Include="..\Moryx.Tools.Wcf\Moryx.Tools.Wcf.csproj" />
<ProjectReference Include="..\Moryx.Runtime\Moryx.Runtime.csproj" />
<ProjectReference Include="..\Moryx.Runtime.Wcf\Moryx.Runtime.Wcf.csproj" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net45'">
<ProjectReference Include="..\Moryx.Tools.Wcf\Moryx.Tools.Wcf.csproj" />
<ProjectReference Include="..\Moryx.Runtime.Wcf\Moryx.Runtime.Wcf.csproj" />

<Reference Include="System.Management" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net5.0'">
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0

using System.Runtime.Serialization;
using Moryx.Tools.Wcf;

namespace Moryx.Runtime.Maintenance.Plugins.Common
{
Expand All @@ -24,8 +23,7 @@ public CommonMaintenanceConfig()
{
ProvidedEndpoint = new HostConfig
{
Endpoint = "common",
BindingType = ServiceBindingType.WebHttp,
Endpoint = CommonMaintenance.Endpoint
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace Moryx.Runtime.Maintenance.Plugins.Common
/// </summary>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
[Plugin(LifeCycle.Transient, typeof(ICommonMaintenance))]
internal class CommonMaintenance : ICommonMaintenance
public class CommonMaintenance : ICommonMaintenance
{
public const string Endpoint = "common";

/// <summary>
/// Get the current server time.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,80 @@
// Licensed under the Apache License, Version 2.0

using System.Net;
#if USE_WCF
using System.ServiceModel;
using System.ServiceModel.Web;
#endif
using Moryx.Communication.Endpoints;

namespace Moryx.Runtime.Maintenance.Plugins.Common
{
/// <summary>
/// Service contract for the common maintenance.
/// </summary>
#if USE_WCF
[ServiceContract]
[Endpoint(Name = nameof(ICommonMaintenance), Version = "3.0.0.0")]
#endif
internal interface ICommonMaintenance
{
/// <summary>
/// Get the server time.
/// </summary>
/// <returns>The current server time.</returns>
#if USE_WCF
[OperationContract]
[WebInvoke(UriTemplate = "time", Method = WebRequestMethods.Http.Get,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
#endif
ServerTimeResponse GetServerTime();

/// <summary>
/// Get information about the application
/// </summary>
/// <returns></returns>
#if USE_WCF
[OperationContract]
[WebInvoke(UriTemplate = "info/application", Method = WebRequestMethods.Http.Get,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
#endif
ApplicationInformationResponse GetApplicationInfo();

/// <summary>
/// Get information about the host
/// </summary>
/// <returns></returns>
#if USE_WCF
[OperationContract]
[WebInvoke(UriTemplate = "info/system", Method = WebRequestMethods.Http.Get,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
#endif
HostInformationResponse GetHostInfo();

/// <summary>
/// Get information about the system load
/// </summary>
/// <returns></returns>
#if USE_WCF
[OperationContract]
[WebInvoke(UriTemplate = "info/system/load", Method = WebRequestMethods.Http.Get,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
#endif
SystemLoadResponse GetSystemLoad();

/// <summary>
/// Get information about the application load
/// </summary>
/// <returns></returns>
#if USE_WCF
[OperationContract]
[WebInvoke(UriTemplate = "info/application/load", Method = WebRequestMethods.Http.Get,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
#endif
ApplicationLoadResponse GetApplicationLoad();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
using System.ComponentModel;
using System.Runtime.Serialization;
using Moryx.Serialization;
using Moryx.Tools.Wcf;

namespace Moryx.Runtime.Maintenance.Plugins.Databases
{
/// <summary>
/// Configuration for the database maintenance plugin.
/// </summary>
[DataContract]
internal class DatabaseConfig : MaintenancePluginConfig
public class DatabaseConfig : MaintenancePluginConfig
{
/// <summary>
/// Name of the plugin.
Expand All @@ -26,8 +25,7 @@ public DatabaseConfig()
{
ProvidedEndpoint = new HostConfig
{
BindingType = ServiceBindingType.WebHttp,
Endpoint = "databases",
Endpoint = DatabaseMaintenance.Endpoint,
MetadataEnabled = true
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@
using System;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text.RegularExpressions;
using Moryx.Communication.Endpoints;
using Moryx.Container;
using Moryx.Logging;
using Moryx.Model;
using Moryx.Model.Configuration;
#if USE_WCF
using System.ServiceModel;
#else
using Microsoft.AspNetCore.Mvc;
#endif


namespace Moryx.Runtime.Maintenance.Plugins.Databases
{
#if USE_WCF
[Plugin(LifeCycle.Singleton, typeof(IDatabaseMaintenance))]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
internal class DatabaseMaintenance : IDatabaseMaintenance
#else
[ApiController, Route(Endpoint)]
[Produces("application/json")]
[Endpoint(Name = nameof(IDatabaseMaintenance), Version = "3.0.0")]
public class DatabaseMaintenance : Controller, IDatabaseMaintenance
#endif
{
#region Dependencies
public const string Endpoint = "databases";

#region Dependencies

/// <summary>
/// Global component for database contexts
Expand All @@ -35,18 +50,27 @@ internal class DatabaseMaintenance : IDatabaseMaintenance
/// </summary>
public DatabaseConfig Config { get; set; }

#endregion
#endregion

#if !USE_WCF
[HttpGet]
#endif
public DataModel[] GetAll()
{
return DbContextManager.Contexts.Select(Convert).ToArray();
}

#if !USE_WCF
[HttpGet("model/{targetModel}")]
#endif
public DataModel GetModel(string targetModel)
{
return Convert(DbContextManager.Contexts.FirstOrDefault(context => TargetModelName(context) == targetModel));
}

#if !USE_WCF
[HttpPost("model/{targetModel}/config")]
#endif
public void SetDatabaseConfig(string targetModel, DatabaseConfigModel config)
{
var match = GetTargetConfigurator(targetModel);
Expand All @@ -59,6 +83,9 @@ public void SetDatabaseConfig(string targetModel, DatabaseConfigModel config)

}

#if !USE_WCF
[HttpPost("model/{targetModel}/config/test")]
#endif
public TestConnectionResponse TestDatabaseConfig(string targetModel, DatabaseConfigModel config)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -72,12 +99,18 @@ public TestConnectionResponse TestDatabaseConfig(string targetModel, DatabaseCon
return new TestConnectionResponse { Result = result };
}

#if !USE_WCF
[HttpPost("createall")]
#endif
public InvocationResponse CreateAll()
{
var bulkResult = BulkOperation(mc => mc.CreateDatabase(mc.Config), "Creation");
return string.IsNullOrEmpty(bulkResult) ? new InvocationResponse() : new InvocationResponse(bulkResult);
}

#if !USE_WCF
[HttpPost("model/{targetModel}/create")]
#endif
public InvocationResponse CreateDatabase(string targetModel, DatabaseConfigModel config)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -100,12 +133,18 @@ public InvocationResponse CreateDatabase(string targetModel, DatabaseConfigModel
}
}

#if !USE_WCF
[HttpDelete("/")]
#endif
public InvocationResponse EraseAll()
{
var bulkResult = BulkOperation(mc => mc.DeleteDatabase(mc.Config), "Deletion");
return string.IsNullOrEmpty(bulkResult) ? new InvocationResponse() : new InvocationResponse(bulkResult);
}

#if !USE_WCF
[HttpDelete("model/{targetModel}")]
#endif
public InvocationResponse EraseDatabase(string targetModel, DatabaseConfigModel config)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -126,6 +165,9 @@ public InvocationResponse EraseDatabase(string targetModel, DatabaseConfigModel
}
}

#if !USE_WCF
[HttpPost("model/{targetModel}/dump")]
#endif
public InvocationResponse DumpDatabase(string targetModel, DatabaseConfigModel config)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -143,6 +185,9 @@ public InvocationResponse DumpDatabase(string targetModel, DatabaseConfigModel c
return new InvocationResponse();
}

#if !USE_WCF
[HttpPost("model/{targetModel}/restore")]
#endif
public InvocationResponse RestoreDatabase(string targetModel, RestoreDatabaseRequest request)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -156,6 +201,9 @@ public InvocationResponse RestoreDatabase(string targetModel, RestoreDatabaseReq
return new InvocationResponse();
}

#if !USE_WCF
[HttpPost("model/{targetModel}/{migrationName}/migrate")]
#endif
public DatabaseUpdateSummary MigrateDatabaseModel(string targetModel, string migrationName, DatabaseConfigModel configModel)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -166,6 +214,9 @@ public DatabaseUpdateSummary MigrateDatabaseModel(string targetModel, string mig
return targetConfigurator.MigrateDatabase(config, migrationName);
}

#if !USE_WCF
[HttpPost("model/{targetModel}/rollback")]
#endif
public InvocationResponse RollbackDatabase(string targetModel, DatabaseConfigModel config)
{
var targetConfigurator = GetTargetConfigurator(targetModel);
Expand All @@ -178,6 +229,9 @@ public InvocationResponse RollbackDatabase(string targetModel, DatabaseConfigMod
return rollbackResult ? new InvocationResponse() : new InvocationResponse("Error while rollback!");
}

#if !USE_WCF
[HttpPost("model/{targetModel}/setup")]
#endif
public InvocationResponse ExecuteSetup(string targetModel, ExecuteSetupRequest request)
{
var contextType = DbContextManager.Contexts.First(c => TargetModelName(c) == targetModel);
Expand Down
Loading

0 comments on commit 5db9a17

Please sign in to comment.