A real-frequency solver for the Anderson impurity model based on bath optimization and cluster perturbation theory