# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

FROM --platform=linux/amd64 registry.access.redhat.com/ubi9/ubi-minimal:9.5

ARG CORS_PROXY_DEFAULT_PORT=8080
ARG CORS_PROXY_DEFAULT_ALLOWED_ORIGINS=
ARG CORS_PROXY_DEFAULT_VERBOSE=false
ARG CORS_PROXY_DEFAULT_ALLOW_HOSTS=localhost,*.github.com

ENV HOME          /home/kie-sandbox
ENV NVM_DIR       $HOME/.nvm
ENV NODE_VERSION  v24.13.0

ENV CORS_PROXY_HTTP_PORT=$CORS_PROXY_DEFAULT_PORT
ENV CORS_PROXY_ALLOWED_ORIGINS=$CORS_PROXY_DEFAULT_ALLOWED_ORIGINS
ENV CORS_PROXY_VERBOSE=$CORS_PROXY_DEFAULT_VERBOSE
ENV CORS_PROXY_ALLOW_HOSTS=$CORS_PROXY_DEFAULT_ALLOW_HOSTS

RUN mkdir $HOME \
  && chgrp -R 0 $HOME \
  && chmod -R g=u $HOME \
  && chown -R 1000:0 $HOME \
  && microdnf --disableplugin=subscription-manager install -y tar gzip \
  && microdnf --disableplugin=subscription-manager clean all \
  && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \
  && /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"


ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/bin
ENV PATH $NODE_PATH:$PATH

COPY --chown=1000:0 dist-dev/cors-proxy $HOME/cors-proxy

EXPOSE $CORS_PROXY_DEFAULT_PORT

USER 1000

HEALTHCHECK --interval=1m --timeout=5s CMD curl -f http://localhost:${CORS_PROXY_HTTP_PORT}/ping || exit 1

CMD node $HOME/cors-proxy/index.js
