This commit is contained in:
CortexCore 2024-04-29 23:05:47 +08:00
parent 4c6f979db5
commit 0c573029b3
10 changed files with 252 additions and 121 deletions

View File

@ -1,75 +0,0 @@
using BITKit;
using IDIS;
using IDIS.Models;
using IDIS.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace IDIS_Server_SIM.Controllers;
[ApiController]
[Route("api")]
public class IDISController:Controller
{
private readonly IDIS_Service _service;
public IDISController(IDIS_Service service)
{
_service = service;
}
[HttpGet]
[Route("info")]
public IActionResult Info()
{
return new ContentResult
{
Content = Environment.MachineName
};
}
[HttpGet]
[Route("query")]
public async Task<IActionResult> Query(string key)
{
try
{
return Ok(await _service.QueryAsync(key));
}
catch (Exception e)
{
return BadRequest(ContextModel.Error(e.ToString()));
}
}
[HttpGet]
[Route("isExist")]
public async Task<bool> IsExist(string key)
{
return await _service.IsExistAsync(key);
}
[HttpPost]
[Route("register")]
public async Task<IActionResult> Register()
{
using var reader = new StreamReader(Request.Body);
var json = await reader.ReadToEndAsync();
var data = JsonConvert.DeserializeObject<IDIS_Register_Data>(json)!;
var response = await _service.RegisterAsync(data.Handle, data.TemplateVersion, data.Value);
return Ok(response);
}
[HttpGet]
[Route("update")]
public string Update([FromBody] string json)
{
return "err:0";
}
[HttpGet]
[Route("delete")]
public async Task<bool> Delete (string handle)
{
await _service.DeleteAsync(handle);
return true;
}
}

View File

@ -0,0 +1,94 @@
using BITKit;
using IDIS;
using IDIS.Models;
using IDIS.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace IDIS_Server_SIM.Controllers;
[ApiController]
public class IDIS_ServiceController:Controller
{
private readonly IDIS_Service _service;
public IDIS_ServiceController(IDIS_Service service)
{
_service = service;
}
[HttpGet]
[Route("info")]
public IActionResult Info()
{
return new ContentResult
{
Content = Environment.MachineName
};
}
[HttpGet]
[Route("/identityv2/data/detail")]
public async Task<IActionResult> Query(string handle)
{
try
{
var result = await _service.QueryAsync(handle);
var response = new IDIS_Response(result);
var json = JsonConvert.SerializeObject(response);
//return Ok(JsonConvert.SerializeObject(response));
return new ContentResult()
{
Content = json,
ContentType = "application/json",
StatusCode = 200
};
}
catch (Exception e)
{
return BadRequest(new IDIS_Response(e.Message,false)
{
Status = 2
});
}
}
[HttpGet]
[Route("isExist")]
public async Task<IActionResult> IsExist(string code)
{
return Ok(new IDIS_Response(await _service.IsExistAsync(code)));
}
[HttpPost]
[Route("/identityv2/data")]
public async Task<IActionResult> Register()
{
try
{
using var reader = new StreamReader(Request.Body);
var json = await reader.ReadToEndAsync();
var data = JsonConvert.DeserializeObject<IDIS_Register_Data>(json)!;
await _service.RegisterAsync(data.Handle, data.TemplateVersion, data.Value);
return Ok(new IDIS_Response());
}
catch (Exception e)
{
return BadRequest(new IDIS_Response(e.Message,false)
{
Status = 2
});
}
}
[HttpGet]
[Route("update")]
public string Update()
{
return "err:0";
}
[HttpGet]
[Route("delete")]
public async Task<bool> Delete (string code)
{
await _service.DeleteAsync(code);
return true;
}
}

View File

@ -0,0 +1,36 @@
using BITKit;
using IDIS;
using IDIS_Server_SIM.Extensions;
using IDIS.Models;
using IDIS.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace IDIS_Server_SIM.Controllers;
[ApiController]
public class IDIS_StatisticsController:Controller
{
private readonly IDIS_Statistics_MySQLBased _service;
private readonly ILogger<IDIS_StatisticsController> _logger;
public IDIS_StatisticsController(IDIS_Statistics_MySQLBased service, ILogger<IDIS_StatisticsController> logger)
{
_service = service;
_logger = logger;
}
[Route("/api/query/today")]
public async Task<IActionResult> GetTodayQuery()
{
var result = await _service.GetTodayQuery();
return new IDIS_Response(result).ToActionResult();
}
[Route("/api/query/count")]
public async Task<IActionResult> GetQueryCount(string handle)
{
var result = await _service.GetQueryCount(handle);
return new IDIS_Response(result).ToActionResult();
}
}

View File

@ -0,0 +1,61 @@
using BITKit;
using IDIS;
using IDIS.Models;
using IDIS.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace IDIS_Server_SIM.Controllers;
[ApiController]
public class IDIS_TemplateController:Controller
{
private readonly IDIS_TemplateService _service;
private readonly ILogger<IDIS_TemplateController> _logger;
public IDIS_TemplateController(IDIS_TemplateService service, ILogger<IDIS_TemplateController> logger)
{
_service = service;
_logger = logger;
}
[Route("/snms/api/template/v1")]
[HttpGet]
public async Task<IActionResult> Query(string prefix, string version)
{
try
{
return Ok(new IDIS_Response(await _service.QueryAsync(prefix, version)));
}
catch (Exception e)
{
return BadRequest(new IDIS_Response(e.Message,false)
{
Status = 2
});
}
}
[Route("/snms/api/template/v1")]
[HttpPost]
public async Task<IActionResult> Save()
{
using var reader = new StreamReader(Request.Body);
var json = await reader.ReadToEndAsync();
try
{
var data = JsonConvert.DeserializeObject<IDIS_Template>(json)!;
await _service.SaveAsync(data);
return Ok(new IDIS_Response());
}
catch (Exception e)
{
return BadRequest(new IDIS_Response(e.Message, false)
{
Status = 2
});
}
}
}

View File

@ -0,0 +1,18 @@
using IDIS;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace IDIS_Server_SIM.Extensions;
public static class IDISExtensions
{
public static IActionResult ToActionResult(this IDIS_Response response)
{
return new ContentResult()
{
Content = JsonConvert.SerializeObject(response),
ContentType = "application/json",
StatusCode = 200
};
}
}

View File

@ -5,6 +5,13 @@ using IDIS.Services;
await BITAppForNet.InitializeAsync("IDIS");
var config = Path.Combine(Environment.CurrentDirectory,"appsettings.json");
if (File.Exists(config))
{
var json = File.ReadAllText(config);
DataParser.Set(json);
}
var builder = WebApplication.CreateBuilder(args);
var cancellationTokenSource = new System.Threading.CancellationTokenSource();
@ -16,7 +23,11 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddTransient<IDIS_Service, IDIS_Service_SQLiteBased>();
builder.Services.AddTransient<IDIS_Service, IDIS_Service_MySQLBased>();
builder.Services.AddTransient<IDIS_Statistics_MySQLBased>();
builder.Services.AddTransient<IDIS_TemplateService, IDIS_TemplateService_MySQLBased>();
var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
@ -25,24 +36,30 @@ var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("请输入二级节点地址,例如:88.123.99");
string? str;
while (string.IsNullOrEmpty(str = Console.ReadLine()) is false)
while (true)
{
str = Console.ReadLine();
if (string.IsNullOrEmpty(str))
{
str = "88.123.99";
break;
}
const string pattern =@"^\d{1,3}\.\d{1,3}\.\d{1,3}$"; // 正则表达式模式
if (Regex.IsMatch(str,pattern))
{
logger.LogInformation("输入的地址符合要求。");
break;
}
logger.LogInformation("错误的地址格式,请按照正确格式输入,例如:88.123.99");
}
logger.LogInformation("输入的地址符合要求。");
#endregion
var idis_Service = app.Services.GetRequiredService<IDIS_Service>();
//idis_Service.PreAddress = str!;
app.Services.GetRequiredService<IDIS_Statistics_MySQLBased>();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

View File

@ -6,5 +6,6 @@
}
},
"Urls": "http://+:5242",
"DefaultConnection":"server=server.bitfall.icu;port=3306;database=ifactory;uid=ifactory;password=JdBfKR2dxhm76Ss2",
"AllowedHosts": "*"
}

View File

@ -1,45 +1,24 @@
# IDIS Server Console
### IDIS 标识解析服务器控制台
指南版本`v0.1`
指南版本`v0.2`
未包括所有功能
### 已支持的功能
- [x] 查询标识
- [x] 注册标识
- [x] 删除标识
- [x] 查询数据模板
- [x] 保存数据模板
### 已支持的功能
- [x] 查询当日解析的标识
- [x] 查询标识所有解析记录
## 食用指南
### 注册:
![Clip_20240428_161137.png](ReadMe%2FClip_20240428_161137.png)
#### WebApi:`localhost:5242/api/register`
```json
{
"handle": "88.123.99/202404281606",
"templateVersion": "1.0",
"value": {
"key1": "value1",
"key2": "value2"
}
}
```
### 查询:
![Clip_20240428_161438.png](ReadMe%2FClip_20240428_161438.png)
#### WebApi:`localhost:5242/api/query?key={handle}`
### 删除:
```csharp
[HttpGet]
[Route("delete")]
public async Task<bool> Delete (string handle)
{
await _service.DeleteAsync(handle);
return true;
}
```
#### WebApi:`localhost:5242/api/delete?handle={handle}`
### 检查标识码是否存在
```csharp
[HttpGet]
[Route("isExist")]
public async Task<bool> IsExist(string key)
{
return await _service.IsExistAsync(key);
}
```
#### WebApi:`localhost:5242/api/isExist?key={key}`
### 基本功能
1. 去寻找`标识解析附件1 SNMS-API-1.2.6.docx`这份文档
2. 按照`已支持的功能`去操作
### 统计功能
#### 查询当日解析的标识
![Clip_20240429_230249.png](ReadMe%2FClip_20240429_230249.png)
`Get:`http://localhost:5242/api/query/today
#### 查询单个标识所有的解析次数
![Clip_20240429_230509.png](ReadMe%2FClip_20240429_230509.png)
`Get:`http://localhost:5242/api/query/count?handle=88.101.6/xx004

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB