-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
319 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Net.Http.Json; | ||
using MusicX.Core.Models; | ||
using MusicX.Shared; | ||
using Newtonsoft.Json; | ||
using NLog; | ||
|
||
namespace MusicX.Core.Services; | ||
|
||
public class BackendConnectionService(Logger logger, string appVersion) | ||
{ | ||
private string? _token; | ||
public readonly HttpClient Client = new(); | ||
|
||
public string Host { get; set; } = | ||
#if DEBUG | ||
"http://localhost:2024"; | ||
#else | ||
"https://musicx.zznty.ru"; | ||
#endif | ||
|
||
public async void ReportMetric(string eventName, string? source = null) | ||
{ | ||
if (string.IsNullOrEmpty(_token)) | ||
throw new NullReferenceException("Token is null"); | ||
|
||
try | ||
{ | ||
using var response = await Client.PostAsJsonAsync($"/metrics/{eventName}/report", new ReportMetricRequest(appVersion, source)); | ||
response.EnsureSuccessStatusCode(); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Error(e, "Error while reporting event metric {0}", eventName); | ||
throw; | ||
} | ||
} | ||
|
||
public async Task<string> GetTokenAsync(long userId) | ||
{ | ||
if (_token is not null) return _token; | ||
logger.Info("Получение временного токена Слушать вместе"); | ||
|
||
var host = await GetHostAsync(); | ||
|
||
Client.BaseAddress = new(host); | ||
using var response = await Client.PostAsJsonAsync("/token", userId); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
_token = await response.Content.ReadFromJsonAsync<string>() ?? | ||
throw new NullReferenceException("Got null response for token request"); | ||
|
||
Client.DefaultRequestHeaders.Authorization = new("Bearer", _token); | ||
|
||
return _token; | ||
} | ||
|
||
private async Task<string> GetHostAsync() | ||
{ | ||
if (!string.IsNullOrEmpty(Host)) | ||
return Host; | ||
|
||
try | ||
{ | ||
logger.Info("Получение адресса сервера Послушать вместе"); | ||
using (var httpClient = new HttpClient()) | ||
{ | ||
var response = await httpClient.GetAsync("https://fooxboy.blob.core.windows.net/musicx/ListenTogetherServers.json"); | ||
|
||
var contents = await response.Content.ReadAsStringAsync(); | ||
|
||
var servers = JsonConvert.DeserializeObject<ListenTogetherServersModel>(contents); | ||
|
||
return Host = | ||
#if DEBUG | ||
servers.Test; | ||
#else | ||
servers.Production; | ||
#endif | ||
} | ||
} | ||
catch(Exception) | ||
{ | ||
return "http://212.192.40.71:5000"; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Security.Claims; | ||
using InfluxDB.Client; | ||
using InfluxDB.Client.Api.Domain; | ||
using InfluxDB.Client.Writes; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using MusicX.Shared; | ||
|
||
namespace MusicX.Server.Controllers; | ||
|
||
[Route("metrics")] | ||
[Authorize] | ||
public class MetricsController(IWriteApiAsync writeApi) : Controller | ||
{ | ||
[HttpPost("{eventName}/report")] | ||
public async Task Report(string eventName, [FromBody] ReportMetricRequest metricRequest) | ||
{ | ||
var userId = User.Claims.Single(b => b.Type == ClaimTypes.NameIdentifier).Value; | ||
|
||
var point = PointData.Measurement(eventName) | ||
.Tag("userId", userId) | ||
.Tag("version", metricRequest.AppVersion) | ||
.Field("value", 1) | ||
.Timestamp(DateTime.Now, WritePrecision.Ms); | ||
|
||
if (!string.IsNullOrEmpty(metricRequest.Source)) | ||
point = point.Tag("source", metricRequest.Source); | ||
|
||
await writeApi.WritePointAsync(point); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace MusicX.Shared; | ||
|
||
public record ReportMetricRequest(string AppVersion, string? Source); |
Oops, something went wrong.