47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System.Web.Http;
|
|
using StackExchange.Redis;
|
|
using Auth.Services.DatabaseService;
|
|
using Auth.Services;
|
|
using Auth.Entities;
|
|
|
|
namespace Auth.Controllers {
|
|
[ApiController]
|
|
[Route("api/db/")]
|
|
public class RedisController : MistoxControllerBase {
|
|
|
|
private readonly ConnectionMultiplexer _redis;
|
|
private readonly IDatabase _redisdb;
|
|
|
|
public RedisController(DatabaseService db) : base(db) {
|
|
_redis = ConnectionMultiplexer.Connect("auth-redis:6379");
|
|
_redisdb = _redis.GetDatabase();
|
|
}
|
|
|
|
[Route("get")]
|
|
[HttpGet]
|
|
public async Task<ActionResult<string>> Get(string JWT, string key) {
|
|
Account? account = AuthJWT.ValidateJWTToken(JWT);
|
|
if (account != null) {
|
|
RedisValue result = await _redisdb.StringGetAsync( account.ID + account.Site + key );
|
|
return Ok(result.ToString());
|
|
} else {
|
|
return BadRequest("JWT Not Valid");
|
|
}
|
|
}
|
|
|
|
[Route("set")]
|
|
[HttpGet]
|
|
public async Task<ActionResult> Set(string JWT, string key, string value) {
|
|
Account? account = AuthJWT.ValidateJWTToken(JWT);
|
|
if (account != null) {
|
|
await _redisdb.StringSetAsync( account.ID + account.Site + key, value );
|
|
return Ok();
|
|
} else {
|
|
return BadRequest("JWT Not Valid");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|