philschmid HF staff commited on
Commit
ec3ccb2
1 Parent(s): 9d3c91f

use fastapi

Browse files
Files changed (3) hide show
  1. Dockerfile +21 -43
  2. app.py +39 -0
  3. requirements.txt +2 -0
Dockerfile CHANGED
@@ -1,56 +1,34 @@
1
- # Stage 1: Build the React app
2
- FROM node:16-alpine AS build
3
-
4
- # Set working directory
5
  WORKDIR /app
6
 
7
- # Copy package.json and package-lock.json
8
- COPY package*.json ./
9
-
10
- # Install dependencies
11
- RUN npm install
12
-
13
- # Copy the rest of the application code
14
  COPY . .
 
 
 
 
 
 
15
 
16
- # Build the React app
17
  RUN npm run build
 
 
18
 
19
- # Stage 2: Serve the built app with a lightweight web server
20
- FROM nginx:alpine
21
-
22
- # Install necessary packages and set up a non-root user
23
- RUN apk add --no-cache shadow \
24
- && useradd -u 1001 -U -d /home/user -s /bin/bash user \
25
- && mkdir -p /home/user \
26
- && chown -R user:user /home/user
27
-
28
- # Install nginx and set permissions
29
- RUN apk add --no-cache nginx \
30
- && mkdir -p /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run \
31
- && touch /var/run/nginx.pid \
32
- && chown -R user:user /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run/nginx.pid
33
-
34
- # Switch to the root user to perform privileged operations
35
- USER root
36
 
37
- # Remove the default Nginx configuration file
38
- RUN rm /etc/nginx/conf.d/default.conf
39
 
40
- # Copy the built files from the previous stage
41
- COPY --from=build /app/dist /usr/share/nginx/html
 
42
 
43
- # Replace the default nginx.conf with our configuration
44
- COPY nginx.conf /etc/nginx/conf.d
45
 
46
- # Change ownership of the necessary directories and files to the non-root user
47
- RUN chown -R user:user /usr/share/nginx/html /etc/nginx/conf.d /var/cache/nginx /var/run /var/log/nginx
 
48
 
49
- # Switch back to the "user" user
50
- USER user
51
 
52
- # Expose port 7860
53
- EXPOSE 7860
54
 
55
- # Start Nginx server
56
- CMD ["nginx", "-g", "daemon off;"]
 
1
+ FROM node:20-alpine AS builder
2
+ RUN apk add --no-cache libc6-compat
 
 
3
  WORKDIR /app
4
 
5
+ # Install dependencies based on the preferred package manager
 
 
 
 
 
 
6
  COPY . .
7
+ RUN \
8
+ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
9
+ elif [ -f package-lock.json ]; then npm ci; \
10
+ elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
11
+ else echo "Lockfile not found." && exit 1; \
12
+ fi
13
 
 
14
  RUN npm run build
15
+ FROM python:3.10-slim AS backend
16
+ WORKDIR /app
17
 
18
+ RUN useradd -m -u 1000 user
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ COPY ./requirements.txt .
21
+ RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
22
 
23
+ USER user
24
+ ENV HOME=/home/user \
25
+ PATH=/home/user/.local/bin:$PATH
26
 
 
 
27
 
28
+ WORKDIR $HOME/app
29
+ COPY --from=builder /app/dist ./static
30
+ COPY . .
31
 
32
+ CMD ["python", "app.py"]
 
33
 
 
 
34
 
 
 
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.staticfiles import StaticFiles
4
+ import argparse
5
+ import os
6
+
7
+ HOST = os.environ.get("API_URL", "0.0.0.0")
8
+ PORT = os.environ.get("PORT", 7860)
9
+ parser = argparse.ArgumentParser()
10
+ parser.add_argument("--host", default=HOST)
11
+ parser.add_argument("--port", type=int, default=PORT)
12
+ parser.add_argument("--reload", action="store_true", default=True)
13
+ parser.add_argument("--ssl_certfile")
14
+ parser.add_argument("--ssl_keyfile")
15
+ args = parser.parse_args()
16
+
17
+ app = FastAPI()
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_credentials=True,
22
+ allow_methods=["*"],
23
+ allow_headers=["*"],
24
+ )
25
+
26
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
27
+
28
+ if __name__ == "__main__":
29
+ import uvicorn
30
+
31
+ print(args)
32
+ uvicorn.run(
33
+ "app:app",
34
+ host=args.host,
35
+ port=args.port,
36
+ reload=args.reload,
37
+ ssl_certfile=args.ssl_certfile,
38
+ ssl_keyfile=args.ssl_keyfile,
39
+ )
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi==0.104.*
2
+ uvicorn[standard]==0.24.*