import { FederatedOstorageSchema } from './gql/federation.js';
import { namespace } from './interfaces.js';
import { OstorageSrvGrpcClient } from './grpc/index.js';
import { createFacadeModuleFactory } from '../../utils.js';
import { handleGetFile } from './objectDownloadReqHandler.js';
import Router from 'koa-router';
import bodyParser from 'koa-bodyparser';
export const ostorageModule = createFacadeModuleFactory(namespace, (facade, config) => {
    const ostorage = {
        client: new OstorageSrvGrpcClient(config.config.client.address, {
            ...config.config.client,
            logger: facade.logger,
        })
    };
    facade.addApolloService({
        name: namespace,
        schema: FederatedOstorageSchema(config.config)
    });
    const router = new Router();
    const endpoint = config.config.endpoint ?? 'storage';
    const route = new RegExp(`^\/${endpoint}\/([^/]+)\/(.+)`, 'i');
    router.use(bodyParser());
    router.get(endpoint, route, async (ctx, next) => {
        const authToken = ctx.request.header['authorization'];
        if (authToken?.startsWith('Bearer ')) {
            const token = authToken.split(' ')[1];
            ctx.subject = { token };
        }
        const match = ctx.req.url.match(route);
        const bucket = match[1];
        const key = match[2];
        await handleGetFile(bucket, key, ctx, ostorage.client);
        return ctx.response;
    });
    facade.koa.use(router.routes());
    facade.koa.use(router.allowedMethods());
    facade.koa.use(async (ctx, next) => {
        ctx.ostorage = ostorage;
        await next();
    });
});
//# sourceMappingURL=index.js.map