import { HttpError } from '@cloudron/connect-lastmile';
import safe from 'safetydance';
import wellknown from '../wellknown.js';


async function get(req, res, next) {
    const host = req.headers['host'];
    const location = req.params.location.join('/');

    const [error, result] = await safe(wellknown.get(host, location));
    if (error) return next(new HttpError(404, error.message));

    if (result.redirectTo) return res.redirect(302, result.redirectTo);

    res.status(200).set('content-type', result.type).send(result.body);
}

export default {
    get
};
