# syntax=docker/dockerfile:1

FROM rust:alpine AS builder

RUN apk add --no-cache musl-dev openssl-dev openssl-libs-static protobuf-dev lld

WORKDIR /app

COPY Cargo.toml ./
RUN mkdir src && echo "fn main() {}" > src/main.rs && cargo fetch

COPY . .

ENV SQLX_OFFLINE=true

RUN OPENSSL_LIB_DIR=/usr/lib OPENSSL_INCLUDE_DIR=/usr/include/openssl OPENSSL_STATIC=1 RUSTFLAGS="-C target-feature=+aes,+sse -C relocation-model=static -C linker=ld.lld" cargo build --release --target x86_64-unknown-linux-musl

FROM alpine:latest AS final

RUN apk add --no-cache ca-certificates
RUN update-ca-certificates

WORKDIR /

COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/spfn-website /spfn-website
COPY --from=builder /app/static /static

ENTRYPOINT ["/spfn-website"]
