diff --git a/Dipper.Alioth.Starlets/Dipper.Alioth.Starlets.csproj b/Dipper.Alioth.Starlets/Dipper.Alioth.Starlets.csproj new file mode 100644 index 0000000..6836c68 --- /dev/null +++ b/Dipper.Alioth.Starlets/Dipper.Alioth.Starlets.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/Dipper.Alioth.Starlets/IAsterism.cs b/Dipper.Alioth.Starlets/IAsterism.cs new file mode 100644 index 0000000..4a75f44 --- /dev/null +++ b/Dipper.Alioth.Starlets/IAsterism.cs @@ -0,0 +1,6 @@ +namespace Dipper.Alioth.Starlets; + +public interface IAsterism +{ + void Initialize(); +} \ No newline at end of file diff --git a/Dipper.Alioth.Starlets/IStarlet.cs b/Dipper.Alioth.Starlets/IStarlet.cs new file mode 100644 index 0000000..d5768ac --- /dev/null +++ b/Dipper.Alioth.Starlets/IStarlet.cs @@ -0,0 +1,6 @@ +namespace Dipper.Alioth.Starlets; + +public interface IStarlet +{ + +} \ No newline at end of file diff --git a/Dipper.Alioth/Dipper.Alioth.csproj b/Dipper.Alioth/Dipper.Alioth.csproj index ff57dfd..3af8976 100644 --- a/Dipper.Alioth/Dipper.Alioth.csproj +++ b/Dipper.Alioth/Dipper.Alioth.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0 enable enable @@ -11,4 +11,8 @@ + + + + diff --git a/Dipper.Alioth/DomainAsterism.cs b/Dipper.Alioth/DomainAsterism.cs new file mode 100644 index 0000000..4fca798 --- /dev/null +++ b/Dipper.Alioth/DomainAsterism.cs @@ -0,0 +1,11 @@ +using Dipper.Alioth.Starlets; + +namespace Dipper.Alioth; + +public class DomainAsterism : IAsterism +{ + public void Initialize() + { + Console.WriteLine("已加载主模块"); + } +} \ No newline at end of file diff --git a/Dipper.Alioth/ManagerStarlet.cs b/Dipper.Alioth/ManagerStarlet.cs new file mode 100644 index 0000000..6e4dac8 --- /dev/null +++ b/Dipper.Alioth/ManagerStarlet.cs @@ -0,0 +1,20 @@ +using Dipper.Alioth.Starlets; +using Microsoft.AspNetCore.Mvc.ApplicationParts; + +namespace Dipper.Alioth; + +public class ManagerStarlet : IStarlet +{ + private ApplicationPartManager _partManager; + + + public ManagerStarlet(ApplicationPartManager partManager) + { + _partManager = partManager; + } + + public IEnumerable GetList() + { + return _partManager.ApplicationParts.Select(x => x.Name); + } +} \ No newline at end of file diff --git a/Dipper.Alioth/Modules/IStarModule.cs b/Dipper.Alioth/Modules/IStarModule.cs deleted file mode 100644 index c482258..0000000 --- a/Dipper.Alioth/Modules/IStarModule.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Dipper.Alioth; - -public interface IStarModule -{ - void Initialize(); -} \ No newline at end of file diff --git a/Dipper.Alioth/Options/StarOption.cs b/Dipper.Alioth/Options/StarOption.cs index cfcb46a..6d596ca 100644 --- a/Dipper.Alioth/Options/StarOption.cs +++ b/Dipper.Alioth/Options/StarOption.cs @@ -1,15 +1,16 @@ +using Dipper.Alioth.Starlets; using Microsoft.AspNetCore.Mvc; namespace Dipper.Alioth.Options; public class StarOption { - public IList Modules { get; } + public IList Asterisms { get; } public Action? MvcOption { get; set; } public Action? JsonOption { get; set; } public StarOption() { - Modules = new List(); + Asterisms = new List(); } } \ No newline at end of file diff --git a/Dipper.Alioth/Providers/ApiFeatureProvider.cs b/Dipper.Alioth/Providers/ApiFeatureProvider.cs new file mode 100644 index 0000000..d7d896a --- /dev/null +++ b/Dipper.Alioth/Providers/ApiFeatureProvider.cs @@ -0,0 +1,19 @@ +using System.Reflection; +using Dipper.Alioth.Starlets; +using Microsoft.AspNetCore.Mvc.Controllers; + +namespace Dipper.Alioth.Providers; + +public class ApiFeatureProvider : ControllerFeatureProvider +{ + protected override bool IsController(TypeInfo typeInfo) + { + if (!typeof(IStarlet).IsAssignableFrom(typeInfo) || + !typeInfo.IsPublic || + typeInfo.IsAbstract || + typeInfo.IsGenericType) + return false; + + return true; + } +} \ No newline at end of file diff --git a/Dipper.Alioth/Services/IService.cs b/Dipper.Alioth/Services/IService.cs deleted file mode 100644 index 73a5b81..0000000 --- a/Dipper.Alioth/Services/IService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Dipper.Alioth; - -public interface IService -{ - -} \ No newline at end of file diff --git a/Dipper.Alioth/Web/ServiceConvention.cs b/Dipper.Alioth/Web/ServiceConvention.cs index bfe5871..bad72cd 100644 --- a/Dipper.Alioth/Web/ServiceConvention.cs +++ b/Dipper.Alioth/Web/ServiceConvention.cs @@ -1,4 +1,5 @@ using System.Text; +using Dipper.Alioth.Starlets; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ActionConstraints; using Microsoft.AspNetCore.Mvc.ApplicationModels; @@ -12,7 +13,7 @@ public class ServiceConvention : IApplicationModelConvention foreach (var controller in application.Controllers) { var type = controller.ControllerType; - if (typeof(IService).IsAssignableFrom(type)) + if (typeof(IStarlet).IsAssignableFrom(type)) { ConfigureServiceExplorer(controller); ConfigureSelector(controller); diff --git a/Dipper.Alioth/Web/ServiceExtension.cs b/Dipper.Alioth/Web/ServiceExtension.cs index d153729..24538f7 100644 --- a/Dipper.Alioth/Web/ServiceExtension.cs +++ b/Dipper.Alioth/Web/ServiceExtension.cs @@ -1,4 +1,6 @@ using Dipper.Alioth.Options; +using Dipper.Alioth.Providers; +using Dipper.Alioth.Starlets; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ApplicationParts; @@ -7,9 +9,9 @@ using Microsoft.Extensions.Hosting; namespace Dipper.Alioth.Web; -internal static class ServiceExtension +public static class ServiceExtension { - internal static WebApplicationBuilder AddStarApp(this WebApplicationBuilder builder, Action action) + public static WebApplicationBuilder AddStarApp(this WebApplicationBuilder builder, Action? action = null) { builder.Services.AddSingleton(); @@ -17,7 +19,7 @@ internal static class ServiceExtension action?.Invoke(option); - var mvcBuilder = builder.Services.AddControllers(options => + builder.Services.AddControllers(options => { options.EnableEndpointRouting = false; option.MvcOption?.Invoke(options); @@ -26,38 +28,18 @@ internal static class ServiceExtension { options.JsonSerializerOptions.PropertyNamingPolicy = null; option.JsonOption?.Invoke(options); - }); - - builder.Services.AddSwaggerGen(options => - { - - }); - AddFlexibleApi(mvcBuilder, option); - - builder.Services.AddSession(); + }) + .AddFlexibleApi(option); return builder; } - internal static WebApplication UserStarApp(this WebApplication app) + public static WebApplication UseStarApp(this WebApplication app) { - if (app.Environment.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(); - } - else - { - app.UseExceptionHandler("/?m=Error500"); - } app.UseStaticFiles(); app.UseHttpsRedirection(); app.UseRouting(); - app.UseSession(); - app.UseAuthorization(); - app.MapRazorPages(); app.MapControllers(); return app; @@ -68,20 +50,20 @@ internal static class ServiceExtension /// /// /// - private static void AddFlexibleApi(IMvcBuilder builder, StarOption option) + private static void AddFlexibleApi(this IMvcBuilder builder, StarOption option) { builder.ConfigureApplicationPartManager(manager => { - manager.ApplicationParts.Add(new AssemblyPart(typeof(IService).Assembly)); + manager.ApplicationParts.Add(new AssemblyPart(typeof(IStarlet).Assembly)); - foreach (var module in option.Modules) + foreach (var module in option.Asterisms) { module.Initialize(); var assembly = module.GetType().Assembly; manager.ApplicationParts.Add(new AssemblyPart(assembly)); } - //manager.FeatureProviders.Add(new ApiFeatureProvider()); + manager.FeatureProviders.Add(new ApiFeatureProvider()); }); } } \ No newline at end of file diff --git a/Dipper.Alioth/Web/ServiceFeatureProvider.cs b/Dipper.Alioth/Web/ServiceFeatureProvider.cs index 0646741..cca6595 100644 --- a/Dipper.Alioth/Web/ServiceFeatureProvider.cs +++ b/Dipper.Alioth/Web/ServiceFeatureProvider.cs @@ -1,4 +1,5 @@ using System.Reflection; +using Dipper.Alioth.Starlets; using Microsoft.AspNetCore.Mvc.Controllers; namespace Dipper.Alioth.Web; @@ -7,7 +8,7 @@ public class ServiceFeatureProvider : ControllerFeatureProvider { protected override bool IsController(TypeInfo typeInfo) { - return typeof(IService).IsAssignableFrom(typeInfo) && typeInfo.IsPublic + return typeof(IStarlet).IsAssignableFrom(typeInfo) && typeInfo.IsPublic && !typeInfo.IsAbstract && !typeInfo.IsGenericType; } } \ No newline at end of file diff --git a/Dipper.Alioth/Web/WebStarHost.cs b/Dipper.Alioth/Web/WebStarHost.cs index d5ce0eb..798cf73 100644 --- a/Dipper.Alioth/Web/WebStarHost.cs +++ b/Dipper.Alioth/Web/WebStarHost.cs @@ -4,6 +4,10 @@ using Microsoft.Extensions.DependencyInjection; namespace Dipper.Alioth.Web; +/// +/// 封装统一主机 +/// 暂时不使用 +/// public class WebStarHost : StarHost { private WebStarHost() @@ -36,7 +40,7 @@ public class WebStarHost : StarHost builder.AddStarApp(action); var app = builder.Build(); - app.UserStarApp(); + app.UseStarApp(); return app; } diff --git a/Dipper.Demo/Dipper.Demo.csproj b/Dipper.Demo/Dipper.Demo.csproj index 466f901..8b3ed2c 100644 --- a/Dipper.Demo/Dipper.Demo.csproj +++ b/Dipper.Demo/Dipper.Demo.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0 enable enable diff --git a/Dipper.Demo/Program.cs b/Dipper.Demo/Program.cs index 9f47f12..ec56226 100644 --- a/Dipper.Demo/Program.cs +++ b/Dipper.Demo/Program.cs @@ -1,8 +1,28 @@ using Dipper.Alioth; using Dipper.Alioth.Web; -WebStarHost.CreateWebHost() - .Run(args, options => - { - - }); \ No newline at end of file +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.AddStarApp(option => +{ + option.Asterisms.Add(new DomainAsterism()); +}); +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); +app.UseStarApp(); + +app.Run(); \ No newline at end of file diff --git a/Dipper.sln b/Dipper.sln index 21dbea2..9ebecff 100644 --- a/Dipper.sln +++ b/Dipper.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Alioth", "Dipper.Ali EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Demo", "Dipper.Demo\Dipper.Demo.csproj", "{F075C721-EA36-4B54-B90B-0CC4AA1D641C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Alioth.Starlets", "Dipper.Alioth.Starlets\Dipper.Alioth.Starlets.csproj", "{2282EA3D-7F55-40AE-84F6-33D5099BC078}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Debug|Any CPU.Build.0 = Debug|Any CPU {F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Release|Any CPU.ActiveCfg = Release|Any CPU {F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Release|Any CPU.Build.0 = Release|Any CPU + {2282EA3D-7F55-40AE-84F6-33D5099BC078}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2282EA3D-7F55-40AE-84F6-33D5099BC078}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2282EA3D-7F55-40AE-84F6-33D5099BC078}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2282EA3D-7F55-40AE-84F6-33D5099BC078}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/global.json b/global.json new file mode 100644 index 0000000..7cd6a1f --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file