FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app
EXPOSE 80    

FROM oven/bun:alpine AS build-frontend
WORKDIR /src/Web
COPY Web/package*.json ./
RUN bun install
COPY Web/ ./
RUN bun run build

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Foxel.csproj", "./"]
RUN dotnet restore "Foxel.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet tool install --global dotnet-ef
ENV PATH="${PATH}:/root/.dotnet/tools"
RUN dotnet build "./Foxel.csproj" -c $BUILD_CONFIGURATION -o /app/build
RUN dotnet ef migrations add InitialCreate

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Foxel.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY --from=build /src/Migrations /app/Migrations
RUN apt-get update && apt-get install -y nginx && rm -rf /var/lib/apt/lists/*
COPY --from=build-frontend /src/Web/dist /var/www/html
COPY /Web/nginx.conf /etc/nginx/nginx.conf

RUN mkdir -p /var/lib/nginx/body /var/cache/nginx /var/run/nginx /app/Uploads \
    && chown -R $APP_UID:$APP_UID /var/lib/nginx /var/cache/nginx /var/run/nginx /var/log/nginx /etc/nginx /var/www/html /app/Uploads /app/Migrations \
    && mkdir -p /run \
    && chmod 777 /run

RUN echo '#!/bin/bash\n\
    # 启动nginx\n\
    nginx -g "daemon off;" &\n\
    \n\
    # 启动.NET应用程序\n\
    dotnet Foxel.dll\n\
    ' > /start.sh && chmod +x /start.sh

RUN mkdir -p /home/app/.aspnet/DataProtection-Keys \
    && chown -R $APP_UID:$APP_UID /home/app/.aspnet

USER $APP_UID
ENTRYPOINT ["/start.sh"]