perf: precompile regex

This commit is contained in:
Polianin Nikita 2024-07-04 23:39:12 +03:00
parent 7e2016080f
commit 05ca45db49

View File

@ -33,7 +33,7 @@ namespace Mirea.Api.Endpoint.Controllers.Configuration;
[ApiController] [ApiController]
[MaintenanceModeIgnore] [MaintenanceModeIgnore]
[ApiExplorerSettings(IgnoreApi = true)] [ApiExplorerSettings(IgnoreApi = true)]
public class SetupController( public partial class SetupController(
ISetupToken setupToken, ISetupToken setupToken,
IMaintenanceModeNotConfigureService notConfigureService, IMaintenanceModeNotConfigureService notConfigureService,
IMemoryCache cache, IMemoryCache cache,
@ -208,7 +208,7 @@ public class SetupController(
[BadRequestResponse] [BadRequestResponse]
public ActionResult<string> CreateAdmin([FromBody] CreateUserRequest user) public ActionResult<string> CreateAdmin([FromBody] CreateUserRequest user)
{ {
if (user.Password.Length < 8 || !Regex.IsMatch(user.Password, "[A-Z]+") || !Regex.IsMatch(user.Password, "[!@#$%^&*]+")) if (user.Password.Length < 8 || !PasswordExistUpperLetter().IsMatch(user.Password) || !PasswordExistSpecialSymbol().IsMatch(user.Password))
throw new ControllerArgumentException("The password must be at least 8 characters long and contain at least one uppercase letter and one special character."); throw new ControllerArgumentException("The password must be at least 8 characters long and contain at least one uppercase letter and one special character.");
if (!MailAddress.TryCreate(user.Email, out _)) if (!MailAddress.TryCreate(user.Email, out _))
@ -348,4 +348,10 @@ public class SetupController(
return true; return true;
} }
[GeneratedRegex("[A-Z]+")]
private static partial Regex PasswordExistUpperLetter();
[GeneratedRegex("[!@#$%^&*]+")]
private static partial Regex PasswordExistSpecialSymbol();
} }