using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; using System.Linq; using Newtonsoft.Json; using ReZero.Configuration; namespace ReZero.SuperAPI { /// /// Represents configuration options for the ReZero SuperAPI. /// public class SuperAPIOptions { public static SuperAPIOptions GetOptions( string fileName = "appsettings.json") { string key = "ReZero"; ReZeroJson configuration=ApiConfiguration.GetJsonValue(key,fileName); SuperAPIOptions superAPIOptions = new SuperAPIOptions(); superAPIOptions.IsEnableSuperAPI = true; superAPIOptions.DatabaseOptions = new DatabaseOptions() { ConnectionConfig = new SuperAPIConnectionConfig() { ConnectionString = configuration.BasicDatabase?.ConnectionString, DbType = configuration?.BasicDatabase?.DbType ?? SqlSugar.DbType.Sqlite } }; superAPIOptions.UiOptions = new UiOptions() { ShowNativeApiDocument = configuration?.Ui?.ShowNativeApiDocument ?? true }; superAPIOptions.InterfaceOptions = new InterfaceOptions() { Jwt=configuration?.Jwt, CorsOptions = configuration?.Cors?? new ReZeroCors() }; if (!string.IsNullOrEmpty(configuration?.Ui?.DefaultIndexSource)) { superAPIOptions.UiOptions.DefaultIndexSource = configuration.Ui.DefaultIndexSource; } return superAPIOptions; } public void EnableSuperApi() { SuperAPIOptions options = new SuperAPIOptions(); IsEnableSuperAPI = true; this.DatabaseOptions = options.DatabaseOptions; this.InterfaceOptions = options.InterfaceOptions; this.DependencyInjectionOptions = options.DependencyInjectionOptions; this.UiOptions = options.UiOptions; } public void EnableSuperApi(SuperAPIOptions options) { IsEnableSuperAPI = true; this.DatabaseOptions = options.DatabaseOptions; this.InterfaceOptions = options.InterfaceOptions; this.DependencyInjectionOptions = options.DependencyInjectionOptions; this.UiOptions = options.UiOptions; } /// /// Enable super api /// internal bool IsEnableSuperAPI = false; /// /// Gets or sets the database configuration options. /// public DatabaseOptions? DatabaseOptions { get; set; } public InterfaceOptions InterfaceOptions { get; set; } = new InterfaceOptions(); /// /// Gets or sets the options for the DependencyInjection. /// public DependencyInjectionOptions DependencyInjectionOptions { get; set; } = new DependencyInjectionOptions(); /// /// Gets or sets the UI configuration options. /// public UiOptions UiOptions { get; set; } = new UiOptions(); } public class DependencyInjectionOptions { public Assembly[]? Assemblies { get; set; } public bool InitDependencyInjection => Assemblies?.Any() ?? false; public DependencyInjectionOptions(params Assembly[] assemblies) { if (!InitDependencyInjection) { this.Assemblies = assemblies; } } } public class InterfaceOptions { public string? AuthorizationLocalStorageName { get; set; } = "RezeroLocalStorage"; public string PageNumberPropName { set; get; } = "PageNumber"; public string PageSizePropName { set; get; } = "PageSize"; public DefaultSuperApiAop SuperApiAop { get; set; } = new DefaultSuperApiAop(); public Func? NoAuthorizationFunc { get; set; } public Func? MergeDataToStandardDtoFunc { get; set; } public ReZeroJwt? Jwt { get; set; } public ReZeroCors CorsOptions { get; set; } = new ReZeroCors(); public JsonSerializerSettings? JsonSerializerSettings { get; set; } } /// /// Represents configuration options for the database settings in ReZero. /// public class DatabaseOptions { /// /// Gets or sets whether to initialize configuration tables. Default is true. /// public bool InitializeTables { get; set; } = true; /// /// Gets or sets the initialization connection string information. Default is SQLite. /// public SuperAPIConnectionConfig ConnectionConfig { get; set; } = new SuperAPIConnectionConfig() { DbType = SqlSugar.DbType.Sqlite, ConnectionString = "datasource=ReZero.db" }; /// /// Callback function to retrieve the current user information. /// internal Func GetCurrentUserCallback { get; set; } = () => new CallBackUserInfo { UserId = "1", UserName = "Admin" }; } /// /// Represents configuration options for the user interface settings in ReZero. /// public class UiOptions { /// /// Gets or sets the language for the UI. /// public Language UiLanguage { get; set; } /// /// Gets or sets the folder name for the default UI. Default is "default_ui". /// public string? DefaultUiFolderName { get; set; } = "default_ui"; /// /// Gets or sets the source path for the default index. Default is "/swagger". /// public string? DefaultIndexSource { get; set; } = "/swagger"; /// /// Gets or sets the path for NuGet packages. Default is the default user NuGet packages path. /// public string? NugetPackagesPath { get; set; } = @"C:\Users\Administrator\.nuget\packages"; /// /// Show system api document /// public bool ShowSystemApiDocument { get; set; } = false; /// /// Show native api document /// public bool ShowNativeApiDocument { get; set; } = true; /// /// Enable Login page Configuration on the UI /// internal bool EnableLoginPage { get; set; } } public class AopOptions { public Func? DynamicApiBeforeInvokeAsync { get; set; } public Func? DynamicApiAfterInvokeAsync { get; set; } public Func? SystemApiBeforeInvokeAsync { get; set; } public Func? SystemApiAfterInvokeAsync { get; set; } } }