Skip to content

Commit

Permalink
Merge pull request #20 from Syriiin/add-download-missing-beatmap-envvar
Browse files Browse the repository at this point in the history
Add DOWNLOAD_MISSING_BEATMAPS env var
  • Loading branch information
Syriiin authored May 18, 2024
2 parents de076f1 + ce58a78 commit a3f13f3
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 121 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"swashbuckle.aspnetcore.cli": {
"version": "6.6.1",
"commands": [
"swagger"
]
}
}
}
6 changes: 6 additions & 0 deletions .github/workflows/test-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: make test-e2e

check-api-reference:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- run: make check-api-reference
18 changes: 16 additions & 2 deletions Difficalcy/Controllers/CalculatorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ public ActionResult<CalculatorInfo> GetInfo()
[HttpGet("calculation")]
public async Task<ActionResult<TCalculation>> GetCalculation([FromQuery] TScore score)
{
return Ok(await calculatorService.GetCalculation(score));
try
{
return Ok(await calculatorService.GetCalculation(score));
}
catch (BeatmapNotFoundException e)
{
return BadRequest(new { error = e.Message });
}
}

/// <summary>
Expand All @@ -43,7 +50,14 @@ public async Task<ActionResult<TCalculation>> GetCalculation([FromQuery] TScore
[Consumes("application/json")]
public async Task<ActionResult<TCalculation[]>> GetCalculationBatch([FromBody] TScore[] scores)
{
return Ok(await Task.WhenAll(scores.Select(score => calculatorService.GetCalculation(score))));
try
{
return Ok(await Task.WhenAll(scores.Select(calculatorService.GetCalculation)));
}
catch (BeatmapNotFoundException e)
{
return BadRequest(new { error = e.Message });
}
}
}
}
8 changes: 8 additions & 0 deletions Difficalcy/Services/BeatmapNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Difficalcy.Services
{
public class BeatmapNotFoundException(string beatmapId) : Exception($"Beatmap {beatmapId} not found")
{
}
}
4 changes: 1 addition & 3 deletions Difficalcy/Services/TestBeatmapProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Difficalcy.Services
{
Expand All @@ -11,7 +9,7 @@ public class TestBeatmapProvider(string resourceAssemblyName) : IBeatmapProvider
public Task EnsureBeatmap(string beatmapId)
{
var resourceName = GetResourceName(beatmapId);
_ = ResourceAssembly.GetManifestResourceInfo(resourceName) ?? throw new BadHttpRequestException($"Beatmap not found: {beatmapId}");
_ = ResourceAssembly.GetManifestResourceInfo(resourceName) ?? throw new BeatmapNotFoundException(beatmapId);
return Task.CompletedTask;
}

Expand Down
18 changes: 12 additions & 6 deletions Difficalcy/Services/WebBeatmapProvider.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;

namespace Difficalcy.Services
{
public class WebBeatmapProvider(IConfiguration configuration) : IBeatmapProvider
{
private readonly string _beatmapDirectory = configuration["BEATMAP_DIRECTORY"];
private readonly string _downloadMissingBeatmaps = configuration["DOWNLOAD_MISSING_BEATMAPS"];
private readonly HttpClient _httpClient = new();

public async Task EnsureBeatmap(string beatmapId)
{
var beatmapPath = GetBeatmapPath(beatmapId);
if (!File.Exists(beatmapPath))
{
if (_downloadMissingBeatmaps != "true")
throw new BeatmapNotFoundException(beatmapId);

using var response = await _httpClient.GetAsync($"https://osu.ppy.sh/osu/{beatmapId}");
if (!response.IsSuccessStatusCode)
throw new BadHttpRequestException("Beatmap not found");
if (!response.IsSuccessStatusCode || response.Content.Headers.ContentLength == 0)
throw new BeatmapNotFoundException(beatmapId);

using var fs = new FileStream(beatmapPath, FileMode.CreateNew);
if (fs.Length == 0)
throw new BadHttpRequestException("Beatmap not found");

await response.Content.CopyToAsync(fs);
if (fs.Length == 0)
{
fs.Close();
File.Delete(beatmapPath);
throw new BeatmapNotFoundException(beatmapId);
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ LABEL org.opencontainers.image.source https://github.com/Syriiin/difficalcy

WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:80
ENV ASPNETCORE_ENVIRONMENT=Production
ENV BEATMAP_DIRECTORY=/beatmaps
ENV ASPNETCORE_URLS="http://+:80"
ENV ASPNETCORE_ENVIRONMENT="Production"
ENV BEATMAP_DIRECTORY="/beatmaps"
ENV DOWNLOAD_MISSING_BEATMAPS="true"

VOLUME ${BEATMAP_DIRECTORY}
# chmod 777 so that this volume can be read/written by other containers that might use different uids
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ start-dev: build-dev ## Starts development environment
clean-dev: ## Cleans development environment
$(COMPOSE_APP_DEV) down --remove-orphans

update-openapi-schemas: ## Updates OpenAPI schemas in docs site
curl localhost:5000/swagger/v1/swagger.json -o docs/docs/api-reference/difficalcy-osu.json
curl localhost:5001/swagger/v1/swagger.json -o docs/docs/api-reference/difficalcy-taiko.json
curl localhost:5002/swagger/v1/swagger.json -o docs/docs/api-reference/difficalcy-catch.json
curl localhost:5003/swagger/v1/swagger.json -o docs/docs/api-reference/difficalcy-mania.json
update-api-reference: ## Updates OpenAPI schemas in docs site
$(COMPOSE_TOOLING_RUN) scripts/update-api-reference.sh

check-api-reference: ## Checks OpenAPI schemas are updated
$(COMPOSE_TOOLING_RUN) scripts/check-api-reference.sh

build-docs: ## Builds documentation site
$(COMPOSE_RUN_DOCS) build --strict --clean
Expand Down
2 changes: 2 additions & 0 deletions compose.tooling.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ services:
build:
context: .
dockerfile: ./tooling.Dockerfile
volumes:
- .:/app
49 changes: 23 additions & 26 deletions docs/docs/api-reference/difficalcy-catch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand All @@ -37,8 +37,7 @@
"maximum": 2147483647,
"minimum": 0,
"type": "integer",
"format": "int32",
"nullable": true
"format": "int32"
}
},
{
Expand All @@ -58,8 +57,7 @@
"maximum": 2147483647,
"minimum": 0,
"type": "integer",
"format": "int32",
"nullable": true
"format": "int32"
}
},
{
Expand All @@ -69,8 +67,7 @@
"maximum": 2147483647,
"minimum": 0,
"type": "integer",
"format": "int32",
"nullable": true
"format": "int32"
}
},
{
Expand All @@ -94,7 +91,7 @@
],
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand All @@ -118,15 +115,14 @@
"type": "array",
"items": {
"$ref": "#/components/schemas/CatchScore"
},
"nullable": true
}
}
}
}
},
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -170,17 +166,27 @@
},
"additionalProperties": false
},
"CatchDifficulty": {
"CatchCalculation": {
"type": "object",
"properties": {
"total": {
"difficulty": {
"$ref": "#/components/schemas/CatchDifficulty"
},
"performance": {
"$ref": "#/components/schemas/CatchPerformance"
},
"accuracy": {
"type": "number",
"format": "double"
},
"combo": {
"type": "number",
"format": "double"
}
},
"additionalProperties": false
},
"CatchPerformance": {
"CatchDifficulty": {
"type": "object",
"properties": {
"total": {
Expand All @@ -190,20 +196,10 @@
},
"additionalProperties": false
},
"CatchCalculation": {
"CatchPerformance": {
"type": "object",
"properties": {
"difficulty": {
"$ref": "#/components/schemas/CatchDifficulty"
},
"performance": {
"$ref": "#/components/schemas/CatchPerformance"
},
"accuracy": {
"type": "number",
"format": "double"
},
"combo": {
"total": {
"type": "number",
"format": "double"
}
Expand All @@ -217,6 +213,7 @@
"type": "object",
"properties": {
"beatmapId": {
"minLength": 1,
"type": "string"
},
"mods": {
Expand Down
38 changes: 19 additions & 19 deletions docs/docs/api-reference/difficalcy-mania.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -101,7 +101,7 @@
],
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand All @@ -125,15 +125,14 @@
"type": "array",
"items": {
"$ref": "#/components/schemas/ManiaScore"
},
"nullable": true
}
}
}
}
},
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -177,40 +176,40 @@
},
"additionalProperties": false
},
"ManiaDifficulty": {
"ManiaCalculation": {
"type": "object",
"properties": {
"total": {
"difficulty": {
"$ref": "#/components/schemas/ManiaDifficulty"
},
"performance": {
"$ref": "#/components/schemas/ManiaPerformance"
},
"accuracy": {
"type": "number",
"format": "double"
}
},
"additionalProperties": false
},
"ManiaPerformance": {
"ManiaDifficulty": {
"type": "object",
"properties": {
"total": {
"type": "number",
"format": "double"
},
"difficulty": {
"type": "number",
"format": "double"
}
},
"additionalProperties": false
},
"ManiaCalculation": {
"ManiaPerformance": {
"type": "object",
"properties": {
"difficulty": {
"$ref": "#/components/schemas/ManiaDifficulty"
},
"performance": {
"$ref": "#/components/schemas/ManiaPerformance"
"total": {
"type": "number",
"format": "double"
},
"accuracy": {
"difficulty": {
"type": "number",
"format": "double"
}
Expand All @@ -224,6 +223,7 @@
"type": "object",
"properties": {
"beatmapId": {
"minLength": 1,
"type": "string"
},
"mods": {
Expand Down
Loading

0 comments on commit a3f13f3

Please sign in to comment.