<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

async def rewrite_middleware(server, request):
    '''
    Sanic middleware that utilizes a security class's "rewrite" method to
    check
    '''
    if singletons.settings.SECURITY is not None:
        security_class = singletons.settings.load('SECURITY')
    else:
        security_class = DummySecurity
    security = security_class()
    try:
        new_path = await security.rewrite(request)
    except SecurityException as e:
        msg = ''
        if DEBUG:
            msg = str(e)
        return server.response.text(msg, status=400)
    request.path = new_path