Skip to content

Commit

Permalink
feat(afdian.server): update
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Dec 8, 2021
1 parent 9ea1557 commit 86d1bfd
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 29 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
- ASPNETCORE_URLS=http://*:80
volumes:
# 注意: Linux 下 区分大小写
- ./App_Data:/app/App_Data
# - ./appsettings.json:/app/appsettings.json // 无法使用 docker-compose 此方法 挂载单个文件, 使用下方挂载单个文件
- type: bind
source: ./appsettings.json
Expand Down
5 changes: 4 additions & 1 deletion src/Afdian.Server/Afdian.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
Expand All @@ -26,14 +27,16 @@
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\js\" />
<Folder Include="wwwroot\libs\" />
</ItemGroup>

<ItemGroup>
<None Update="Files\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="App_Data\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file added src/Afdian.Server/App_Data/afdian.sqlite
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Afdian.Server/Configuration/AfdianConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{
public class AfdianConfiguration
{
/// <summary>
/// afdian Webhook 路径效验 vToken
/// </summary>
public string VToken { get; set; }

public string UserId { get; set; }
Expand Down
49 changes: 41 additions & 8 deletions src/Afdian.Server/Controllers/BadgeController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Afdian.Sdk;
using Afdian.Server.Configuration;
using Afdian.Server.RequestModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Afdian.Server.Models;
using System.Linq;

namespace Afdian.Server.Controllers
{
Expand All @@ -14,18 +17,22 @@ public class BadgeController : ControllerBase

private readonly ILogger<BadgeController> _logger;

private readonly ApplicationDbContext _applicationDbContext;

#region Ctor
public BadgeController(IOptionsMonitor<AfdianConfiguration> afdianConfigurationOptionsMonitor, ILogger<BadgeController> logger)
public BadgeController(IOptionsMonitor<AfdianConfiguration> afdianConfigurationOptionsMonitor, ApplicationDbContext applicationDbContext,
ILogger<BadgeController> logger)
{
this.AfdianConfiguration = afdianConfigurationOptionsMonitor.CurrentValue;
_applicationDbContext = applicationDbContext;
_logger = logger;
}
#endregion

#region Actions

[Route("/badge.svg")]
[HttpGet, HttpPost]
[HttpGet]
public async Task<IActionResult> Badge([FromQuery] string badgeToken)
{
badgeToken = System.Net.WebUtility.UrlDecode(badgeToken);
Expand All @@ -42,18 +49,44 @@ public async Task<IActionResult> Badge([FromQuery] string badgeToken)
}

[Route("/{id}/badge.svg")]
[HttpGet, HttpPost]
public async Task<IActionResult> Badge([FromRoute] int id)
[HttpGet]
public async Task<IActionResult> Badge([FromRoute] int id, [FromQuery] BadgeRequestModel badgeRequestModel)
{
// TODO: 根据 id 查询数据库
string userId = "";
string token = "";
Badge badge = _applicationDbContext.Badge?.FirstOrDefault(m => m.Id == id);
if (badge == null)
{
return NotFound();
}
string userId = badge.UserId;
string token = badge.Token;

return await Task.FromResult(MakeBadgeSvg(userId, token));
}

[HttpPost]
public async Task<IActionResult> Create(string userId, string token)
{
AfdianClient afdianClient = new AfdianClient(userId, token);
var jsonStr = await afdianClient.PingAsync();
if (!jsonStr.Contains("200"))
{
return Content("不合法的 user_id, token");
}
Badge badge = new Badge()
{
CreateTime = DateTime.Now,
UserId = userId,
Token = token
};
await _applicationDbContext.Badge.AddAsync(badge);
await _applicationDbContext.SaveChangesAsync();

return Content(badge.Id.ToString());
}

[Route("/{userId}/{token}/badge.svg")]
[HttpGet, HttpPost]
[HttpGet]
public async Task<IActionResult> Badge([FromRoute] string userId, [FromRoute] string token)
{
// 注意: 不要给 将 planId 放入参数列表, 否则变成必需项
Expand Down Expand Up @@ -92,7 +125,7 @@ private ContentResult MakeBadgeSvg(string userId, string token, string planId =
svgStr = svgStr.Replace("{{count}}", sponsorCount.ToString()).Replace("{{countTextLength}}", countTextLength.ToString());

return Content(svgStr, "image/svg+xml;charset=utf-8");
}
}

#endregion

Expand Down
16 changes: 0 additions & 16 deletions src/Afdian.Server/Controllers/HomeController.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/Afdian.Server/Models/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;

namespace Afdian.Server.Models
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}

public virtual DbSet<Badge> Badge { get; set; }

}
}
18 changes: 18 additions & 0 deletions src/Afdian.Server/Models/Badge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;

namespace Afdian.Server.Models
{
public class Badge
{
[Key]
public int Id { get; set; }

public string UserId { get; set; }

public string Token { get; set; }

public DateTime CreateTime { get; set; }

public string Ip { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/Afdian.Server/RequestModels/BadgeRequestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using Microsoft.AspNetCore.Mvc;

namespace Afdian.Server.RequestModels
{
/// <summary>
/// 模型绑定: https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0
/// </summary>
public class BadgeRequestModel
{
public string PlanId { get; set; } = "";


}
}
10 changes: 8 additions & 2 deletions src/Afdian.Server/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Afdian.Server.Configuration;
using Afdian.Server.Services;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot;

namespace Afdian.Server
Expand Down Expand Up @@ -57,6 +58,10 @@ public void ConfigureServices(IServiceCollection services)
// https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-5.0#add-newtonsoftjson-based-json-format-support
services.AddControllers()
.AddNewtonsoftJson();

string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<Models.ApplicationDbContext>(options=>
options.UseSqlite(connectionString));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand All @@ -72,10 +77,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseRouting();
app.UseCors();

app.UseAuthorization();

app.UseDefaultFiles();
app.UseStaticFiles();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
// Configure custom endpoint per Telegram API recommendations:
Expand Down
3 changes: 3 additions & 0 deletions src/Afdian.Server/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=App_Data/afdian.sqlite;"
},
"AfdianConfiguration": {
"VToken": "abc",
"UserId": "eqe",
Expand Down
3 changes: 3 additions & 0 deletions src/Afdian.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=App_Data/afdian.sqlite;"
},
"AfdianConfiguration": {
"VToken": "{VToken}",
"UserId": "{UserId}",
Expand Down
27 changes: 25 additions & 2 deletions src/Afdian.Server/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,32 @@
<html>
<head>
<meta charset="utf-8" />
<title></title>
<title>Afdian.Server | Afdian.Sdk | 爱发电 非官方 .NET SDK</title>
<!-- 移动端设置 -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div>Hello World!</div>
<div class="container">
<div class="row align-items-center">
<div class="col align-self-center">
<div class="form">
<div class="form-group">
<label>user_id</label>
<input id="js-input-userId" type="text" class="form-control">
</div>
<div class="form-group">
<label>token</label>
<input id="js-input-token" type="text" class="form-control">
</div>
<button id="js-btn-getBadge-1" class="btn btn-info btn-block" style="margin-top:14px;">获取Badge - 显式UserId,Token</button>
</div>
</div>
</div>
</div>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="js/main.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions src/Afdian.Server/wwwroot/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@


0 comments on commit 86d1bfd

Please sign in to comment.