# 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.3

ARG CORS_PROXY_DEFAULT_PORT=8080
ARG CORS_PROXY_DEFAULT_ORIGIN=*
ARG CORS_PROXY_DEFAULT_VERBOSE=false

ENV HOME          /home/kie-sandbox
ENV NVM_DIR       $HOME/.nvm
ENV NODE_VERSION  v20.8.1

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" \
  && export CORS_PROXY_HTTP_PORT=$CORS_PROXY_DEFAULT_PORT \
  && export CORS_PROXY_ORIGIN=CORS_PROXY_DEFAULT_ORIGIN \
  && export CORS_PROXY_VERBOSE=CORS_PROXY_DEFAULT_VERBOSE


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

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