Skip to content

Commit

Permalink
what?
Browse files Browse the repository at this point in the history
  • Loading branch information
Yucked committed Jan 27, 2024
1 parent 21a51bc commit fb0e2a7
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 46 deletions.
14 changes: 14 additions & 0 deletions Rhapsody.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rhapsody", "src\Rhapsody.csproj", "{055F55B0-D3D5-4F99-8EA1-3429B35E6294}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dysc", "..\Dysc\src\Dysc.csproj", "{7BA2777E-6357-45BC-A30C-70A09FF65BD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,5 +32,17 @@ Global
{055F55B0-D3D5-4F99-8EA1-3429B35E6294}.Release|x64.Build.0 = Release|Any CPU
{055F55B0-D3D5-4F99-8EA1-3429B35E6294}.Release|x86.ActiveCfg = Release|Any CPU
{055F55B0-D3D5-4F99-8EA1-3429B35E6294}.Release|x86.Build.0 = Release|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Debug|x64.ActiveCfg = Debug|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Debug|x64.Build.0 = Debug|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Debug|x86.ActiveCfg = Debug|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Debug|x86.Build.0 = Debug|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Release|Any CPU.Build.0 = Release|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Release|x64.ActiveCfg = Release|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Release|x64.Build.0 = Release|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Release|x86.ActiveCfg = Release|Any CPU
{7BA2777E-6357-45BC-A30C-70A09FF65BD8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
12 changes: 6 additions & 6 deletions src/Controllers/PingController.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Rhapsody.Payloads.Outbound;

namespace Rhapsody.Controllers {
[Route("api/[controller]"), ApiController, Produces("application/json")]
public sealed class PingController : ControllerBase {
private readonly IMemoryCache _memoryCache;
private const string CACHE_KEY = "_Time";

public PingController(IMemoryCache memoryCache) {
_memoryCache = memoryCache;
}

[HttpGet]
public IActionResult Ping() {
if (_memoryCache.TryGetValue(CACHE_KEY, out DateTimeOffset old)) {
return Ok(old);
if (_memoryCache.TryGetValue("PING", out DateTimeOffset old)) {
return RestResponse.Ok(old);
}

old = DateTimeOffset.Now;
_memoryCache.Set(CACHE_KEY, old);
return Ok(old);
old = DateTimeOffset.UtcNow;
_memoryCache.Set("PING", old);
return RestResponse.Ok(old);
}
}
}
54 changes: 24 additions & 30 deletions src/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dysc;
using Dysc.Providers;
using Dysc.Search;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Rhapsody.Extensions;
using Rhapsody.Internals.Attributes;
using Rhapsody.Payloads.Outbound;

namespace Rhapsody.Controllers {
[Route("api/[controller]"), ApiController, Produces("application/json")]
[ServiceFilter(typeof(ProviderFilterAttribute))]
public sealed class SearchController : ControllerBase {
private readonly DyscClient _dyscClient;
private readonly Dysk _dysk;
private readonly IMemoryCache _memoryCache;
private readonly ILogger _logger;

public SearchController(DyscClient dyscClient, ILogger<SearchController> logger, IMemoryCache memoryCache) {
_dyscClient = dyscClient;
public SearchController(Dysk dysk, ILogger<SearchController> logger, IMemoryCache memoryCache) {
_dysk = dysk;
_logger = logger;
_memoryCache = memoryCache;
}
Expand All @@ -32,69 +27,67 @@ public SearchController(DyscClient dyscClient, ILogger<SearchController> logger,
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ValueTask<IActionResult> GetYouTubeAsync(string query) {
return SearchAsync(ProviderType.YouTube, query);
public ValueTask<IActionResult> GetYouTubeAsync(string query, bool isPlaylist = false) {
return SearchAsync(SourceProvider.YouTube, query, isPlaylist);
}

[HttpGet("soundcloud")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ValueTask<IActionResult> GetSoundCloudAsync(string query) {
return SearchAsync(ProviderType.SoundCloud, query);
public ValueTask<IActionResult> GetSoundCloudAsync(string query, bool isPlaylist = false) {
return SearchAsync(SourceProvider.SoundCloud, query, isPlaylist);
}

[HttpGet("bandcamp")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ValueTask<IActionResult> GetBandCampAsync(string query) {
return SearchAsync(ProviderType.BandCamp, query);
public ValueTask<IActionResult> GetBandCampAsync(string query, bool isPlaylist = false) {
return SearchAsync(SourceProvider.BandCamp, query, isPlaylist);
}

[HttpGet("hearthisat")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ValueTask<IActionResult> GetHearThisAtAsync(string query) {
return SearchAsync(ProviderType.HearThisAt, query);
public ValueTask<IActionResult> GetHearThisAtAsync(string query, bool isPlaylist = false) {
return SearchAsync(SourceProvider.HearThisAt, query, isPlaylist);
}

[HttpGet("http")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ValueTask<IActionResult> GetHttpAsync(string query) {
public ValueTask<IActionResult> GetHttpAsync(string query, bool isPlaylist = false) {
return !Uri.IsWellFormedUriString(query, UriKind.Absolute)
? new ValueTask<IActionResult>(RestResponse.Error("Query must be an absolute URI string."))
: SearchAsync(ProviderType.Http, query);
: SearchAsync(SourceProvider.Http, query, isPlaylist);
}

private async ValueTask<IActionResult> SearchAsync(ProviderType providerType, string query) {
private async ValueTask<IActionResult> SearchAsync(SourceProvider providerType, string query, bool isPlaylist) {
if (string.IsNullOrWhiteSpace(query)) {
return RestResponse.Error("Query must not be empty.");
}

try {
var provider = _dyscClient.GetProvider(providerType);
if (TrySearchCache(providerType, query, out var searchResponse)) {
}
else {
searchResponse = await provider.SearchAsync(query);
_memoryCache.Set(providerType, new[] {
searchResponse
});
var provider = _dysk.GetProvider(providerType);
if (isPlaylist) {
var playlistResult = await provider.GetPlaylistAsync(query);
return RestResponse.Ok(playlistResult);
}

return RestResponse.Ok(searchResponse);
var trackResults = await provider.SearchAsync(query);
return RestResponse.Ok(trackResults);
}
catch (Exception exception) {
_logger.LogCritical(exception, exception.StackTrace);
return RestResponse.Error(exception.Message);
}
}

private bool TrySearchCache(ProviderType providerType, string query, out SearchResponse searchResponse) {
/*
private bool TrySearchCache(SourceProvider providerType, string query, out SearchResponse searchResponse) {
searchResponse = default;
if (!_memoryCache.TryGetValue(providerType, out ICollection<SearchResponse> searchResponses)) {
return false;
Expand Down Expand Up @@ -125,8 +118,9 @@ private bool TrySearchCache(ProviderType providerType, string query, out SearchR
searchResponse = response;
return true;
}
return false;
}
*/
}
}
4 changes: 2 additions & 2 deletions src/Extensions/MiscExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Colorful;
using Dysc.Providers;
using Dysc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Rhapsody.Objects;
Expand Down Expand Up @@ -101,7 +101,7 @@ public static ApplicationOptions VerifyOptions() {
},
DefaultLevel = LogLevel.Trace
},
Providers = Enum.GetNames(typeof(ProviderType))
Providers = Enum.GetNames(typeof(SourceProvider))
.ToDictionary(x => x.ToLower(), x => true)
};
var serialize = applicationOptions.Serialize();
Expand Down
8 changes: 4 additions & 4 deletions src/Internals/Logging/LoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public readonly ILogger CreateLogger(string categoryName) {
if (_filters.TryGetFilter(ref categoryName, out var logLevel)) {
}

if (_loggers.TryGetValue(categoryName, out var logger)) {
return logger;
}
if (_loggers.TryGetValue(categoryName, out var logger)) {
return logger;
}


logger = new ColorfulLogger(categoryName, _defaultLevel, this);
logger = new ColorfulLogger(categoryName, logLevel, this);
_loggers.TryAdd(categoryName, logger);
return logger;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Internals/Middlewares/ExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task Invoke(HttpContext context) {
await _requestDelegate(context);
}
catch (Exception ex) {
_logger.LogError(ex, ex.StackTrace);
_logger.LogError(ex.Message, ex.StackTrace);
context.Response.StatusCode = 500;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ await Host.CreateDefaultBuilder()
collection.Configure<ApplicationOptions>(context.Configuration);

collection.AddScoped<ProviderFilterAttribute>();
collection.AddSingleton<DyscClient>();
collection.AddSingleton<Dysk>();
})
.RunConsoleAsync();
}
Expand Down
27 changes: 27 additions & 0 deletions src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57080/",
"sslPort": 44302
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Rhapsody": {
"commandName": "Project",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
1 change: 1 addition & 0 deletions src/configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"endpoint":{"host":"*","port":2020,"fallbackRandom":false},"authentication":{"password":"Rhapsody","endpoints":["/api/search","/ws"]},"logging":{"defaultLevel":"Trace","filters":{"System.*":3}},"providers":{"youtube":true,"soundcloud":true,"bandcamp":true,"hearthisat":true,"http":true}}
22 changes: 22 additions & 0 deletions src/wwwroot/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,26 @@ html, body {

.menu.active .bar:nth-child(3) ul li {
opacity: 1;
}

.has-border-radius-20 {
border-radius: 20px;
}

.has-border-radius-15 {
border-radius: 15px;
}

.has-shadow {
box-shadow: 0 1px 10px black;
}

.is-tea-pink {
color: #131313 !important;
background-color: #fcd4fb !important;
}

.is-somewhat-yellow {
color: #131313 !important;
background-color: #fff454 !important;
}
4 changes: 2 additions & 2 deletions src/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
</div>
</div>

<div class="container">
<section class="container has-border-radius-15 has-shadow is-tea-pink">
<h1>Yeee hawe</h1>
</div>
</section>
</body>
</html>

0 comments on commit fb0e2a7

Please sign in to comment.