const { Client: ElasticsearchClient } = require("@elastic/elasticsearch");
const Minio = require("minio");
const { elasticsearchUrl, s3EndpointRaw } = require("./env");

const s3Url = new URL(s3EndpointRaw);

const minioClient = new Minio.Client({
  endPoint: s3Url.hostname,
  port: Number(s3Url.port || (s3Url.protocol === "https:" ? 443 : 80)),
  useSSL: s3Url.protocol === "https:",
  accessKey: process.env.S3_ACCESS_KEY || "minioadmin",
  secretKey: process.env.S3_SECRET_KEY || "minioadmin"
});

const elasticsearchClient = new ElasticsearchClient({ node: elasticsearchUrl });

module.exports = {
  minioClient,
  elasticsearchClient
};
