diff --git a/MetingJS.Server/MetingJS.Server.csproj b/MetingJS.Server/MetingJS.Server.csproj index 896aed2..0d31ec1 100644 --- a/MetingJS.Server/MetingJS.Server.csproj +++ b/MetingJS.Server/MetingJS.Server.csproj @@ -19,4 +19,6 @@ + + diff --git a/MetingJS.Server/Models/AppSettings.cs b/MetingJS.Server/Models/AppSettings.cs index 06931e8..7790aa1 100644 --- a/MetingJS.Server/Models/AppSettings.cs +++ b/MetingJS.Server/Models/AppSettings.cs @@ -4,8 +4,6 @@ namespace MetingJS.Server.Models { public class AppSettings { - public static AppSettings Config { get; set; } - public AppSettings(IConfiguration configuration) { configuration.Bind(this); diff --git a/MetingJS.Server/Models/MusicList.cs b/MetingJS.Server/Models/MusicList.cs index 36e499a..af43bb2 100644 --- a/MetingJS.Server/Models/MusicList.cs +++ b/MetingJS.Server/Models/MusicList.cs @@ -1,24 +1,18 @@ -using System.Text.Json; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; using Meting4Net.Core; using Meting4Net.Core.Models.Standard; -using static MetingJS.Server.Models.AppSettings; namespace MetingJS.Server.Models { public class MusicList { - public MusicList() - { - } - - public MusicList(Music_search_item aItem, ServerProvider server) + public MusicList(Music_search_item aItem, ServerProvider server, AppSettings appSettings) { Title = aItem.name; Author = string.Join(" / ", aItem.artist); - Url = $"{Config.Url}?server={server:G}&type=url&id={aItem.url_id}"; - Pic = $"{Config.Url}?server={server:G}&type=pic&id={aItem.pic_id}"; - Lrc = $"{Config.Url}?server={server:G}&type=lrc&id={aItem.lyric_id}"; + Url = $"{appSettings.Url}?server={server:G}&type=url&id={aItem.url_id}"; + Pic = $"{appSettings.Url}?server={server:G}&type=pic&id={aItem.pic_id}"; + Lrc = $"{appSettings.Url}?server={server:G}&type=lrc&id={aItem.lyric_id}"; } [JsonPropertyName("title")] public string Title { get; set; } diff --git a/MetingJS.Server/Startup.cs b/MetingJS.Server/Startup.cs index ceec585..f57cd85 100644 --- a/MetingJS.Server/Startup.cs +++ b/MetingJS.Server/Startup.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using static MetingJS.Server.Models.AppSettings; namespace MetingJS.Server { @@ -14,7 +13,6 @@ public class Startup public Startup(IConfiguration configuration) { Configuration = configuration; - Config = new AppSettings(configuration); } public IConfiguration Configuration { get; } @@ -22,27 +20,23 @@ public Startup(IConfiguration configuration) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddSingleton(new AppSettings(Configuration)); services.AddSingleton(); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AppSettings appSettings) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - app.UseCors(builder => - builder.WithOrigins(Config.WithOrigins).WithMethods("GET", "OPTIONS") - .AllowAnyHeader().AllowCredentials()); - } - else - { - app.UseCors(builder => - builder.WithOrigins(Config.WithOrigins).WithMethods("GET", "OPTIONS") - .AllowAnyHeader().AllowCredentials()); } + app.UseCors(builder => + builder.WithOrigins(appSettings.WithOrigins).SetIsOriginAllowedToAllowWildcardSubdomains().WithMethods("GET", "OPTIONS") + .AllowAnyHeader()); + app.UseRouting(); app.UseAuthorization(); diff --git a/MetingJS.Server/Utils/Meting.cs b/MetingJS.Server/Utils/Meting.cs index d1482b6..75b9efd 100644 --- a/MetingJS.Server/Utils/Meting.cs +++ b/MetingJS.Server/Utils/Meting.cs @@ -4,27 +4,33 @@ using Meting4Net.Core; using MetingJS.Server.Models; using MMeting = Meting4Net.Core.Meting; -using static MetingJS.Server.Models.AppSettings; using static MetingJS.Server.Models.QueryType; namespace MetingJS.Server.Utils { public class Meting : IMeting { + private readonly AppSettings _config; + + public Meting(AppSettings appSettings) + { + _config = appSettings; + } + public string GetLrc(ServerProvider server, string id) { - var lrc = new MMeting(server) {TryCount = 20}.LyricObj(id); + var lrc = new MMeting(server) { TryCount = 20 }.LyricObj(id); return lrc.lyric; } public string GetPic(ServerProvider server, string id) { - var meting = new MMeting(server) {TryCount = 10}; + var meting = new MMeting(server) { TryCount = 10 }; var pic = meting.PicObj(id, 90); var picUrl = pic.url; - if (!string.IsNullOrEmpty(picUrl) && Config.Replace.Pic != null) - picUrl = Replace(picUrl, Config.Replace.Pic); + if (!string.IsNullOrEmpty(picUrl) && _config.Replace.Pic != null) + picUrl = Replace(picUrl, _config.Replace.Pic); return picUrl; } @@ -33,8 +39,8 @@ public string GetUrl(ServerProvider server, string id) var meting = new MMeting(server) { TryCount = 10 }; var url = meting.UrlObj(id); var urlUrl = url.url; - if (!string.IsNullOrEmpty(urlUrl) && Config.Replace.Url != null) - urlUrl = Replace(urlUrl, Config.Replace.Url); + if (!string.IsNullOrEmpty(urlUrl) && _config.Replace.Url != null) + urlUrl = Replace(urlUrl, _config.Replace.Url); return urlUrl; } @@ -45,19 +51,19 @@ public string Search(ServerProvider server, QueryType type, string id) switch (type) { case Album: - list = meting.AlbumObj(id).Select(s => new MusicList(s, server)).ToList(); + list = meting.AlbumObj(id).Select(s => new MusicList(s, server, _config)).ToList(); break; case Artist: - list = meting.ArtistObj(id).Select(s => new MusicList(s, server)).ToList(); + list = meting.ArtistObj(id).Select(s => new MusicList(s, server, _config)).ToList(); break; case PlayList: - list = meting.PlaylistObj(id).Select(s => new MusicList(s, server)).ToList(); + list = meting.PlaylistObj(id).Select(s => new MusicList(s, server, _config)).ToList(); break; case QueryType.Search: - list = meting.SearchObj(id).Select(s => new MusicList(s, server)).ToList(); + list = meting.SearchObj(id).Select(s => new MusicList(s, server, _config)).ToList(); break; case Song: - list = new List {new MusicList(meting.SongObj(id), server)}; + list = new List { new MusicList(meting.SongObj(id), server, _config) }; break; } diff --git a/MetingJS.Server/appsettings.Development.json b/MetingJS.Server/appsettings.Development.json index 6926f91..0b3e664 100644 --- a/MetingJS.Server/appsettings.Development.json +++ b/MetingJS.Server/appsettings.Development.json @@ -10,7 +10,7 @@ "WithOrigins": [ "*" ], - "Urls": "http://localhost:5000;http://localhost:5001", + "Urls": "http://localhost:5000", "Url": "http://localhost:5001/api/music", "Replace": { "Url": [ diff --git a/MetingJS.Server/appsettings.json b/MetingJS.Server/appsettings.json index c71727d..f2ed340 100644 --- a/MetingJS.Server/appsettings.json +++ b/MetingJS.Server/appsettings.json @@ -9,7 +9,7 @@ "AllowedHosts": "*", "WithOrigins": [ "https://*.xxwhite.com", - "https://*.xwhite.studio" + "*" ], "Urls": "http://localhost:5001", "Url": "",