Skip to content

Commit

Permalink
nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
abraryaser02 committed May 10, 2024
1 parent ad2f6da commit cc71d8e
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 127 deletions.
2 changes: 1 addition & 1 deletion docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- backend-db

backend-db:
image: postgres:13
image: postgres:16
container_name: backend-db
build:
context: ./services/postgres/
Expand Down
17 changes: 11 additions & 6 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ version: '3.9'
services:
backend:
container_name: backend
restart: unless-stopped
build:
context: ./services/backend/
dockerfile: Dockerfile-prod
command: sh -c "while ! nc -z backend-db 5432; do sleep 0.1; done; exec gunicorn --workers 3 --bind 0.0.0.0:8000 wsgi:app"
volumes:
- ./services/backend/:/usr/src/app/
ports:
- 5001:8000 # Map external port 5001 to internal port 8000
- "5001:8000"
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=production
Expand All @@ -25,7 +26,7 @@ services:
volumes:
- postgres_volume:/var/lib/postgresql/data
ports:
- 5435:5432 # Map external port 5435 to internal port 5432
- "5435:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
Expand All @@ -37,18 +38,22 @@ services:
context: ./services/client/my-app/
dockerfile: Dockerfile-prod-2
volumes:
- ./services/client/my-app/build:/usr/share/nginx/html # Mount the build directory for serving static files
- ./services/client/my-app/build:/usr/share/nginx/html
ports:
- "3000:80"
depends_on:
- backend

nginx:
container_name: nginx
image: nginx:latest
ports:
- "8000:80" # Expose port 8000 on host for accessing Nginx
- "8000:80"
- "443:443"
restart: unless-stopped
volumes:
- ./services/nginx/nginx.conf:/etc/nginx/nginx.conf # Mount the Nginx configuration file
- ./services/client/my-app/build:/usr/share/nginx/html # Mount the build directory for serving static files
- ./services/client/my-app/nginx.conf:/etc/nginx/nginx.conf
- ./services/client/my-app/prod.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backend
- my-app
Expand Down
2 changes: 1 addition & 1 deletion load_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

echo 'loading data'

time python3 load_data.py --db=postgresql://postgres:postgres@localhost:5435/backend_dev --user_rows=1000000 --event_rows=1000000
time python3 load_data.py --db=postgresql://postgres:postgres@localhost:5435/backend_dev --user_rows=1000 --event_rows=1000

echo 'finished loading'
21 changes: 13 additions & 8 deletions services/backend/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,16 @@ def delete_event(event_id):
@app.route('/events_by_user/<int:user_id>', methods=['GET'])
def events_by_user(user_id):
query = """
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords
FROM events e
JOIN user_to_events ue ON ue.event_id = e.id_events
WHERE ue.user_id = :user_id
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords
FROM events e
JOIN user_to_events ue ON ue.event_id = e.id_events
WHERE ue.user_id = :user_id
"""
with engine.connect() as connection:
events = connection.execute(text(query), {'user_id': user_id}).fetchall()
if not events:
return jsonify({'message': 'User not\q found'}), 404
return jsonify({'message': 'User not found'}), 404

events_list = [{'id': event.id_events,
'name': event.name,
Expand All @@ -226,9 +226,14 @@ def get_top_favorited_events():
try:
# SQL query to select the top 10 most favorited events
query = """
SELECT * FROM mv_events_with_likes
SELECT e.id_events, e.name, e.description, e.location, e.start_time, e.end_time,
e.organization, e.contact_information, e.registration_link, e.keywords,
COUNT(ue.event_id) AS likes
FROM events e
LEFT JOIN user_to_events ue ON e.id_events = ue.event_id
GROUP BY e.id_events
ORDER BY likes DESC
LIMIT 10;
LIMIT 10
"""
with engine.connect() as connection:
# Execute the SQL query
Expand Down
25 changes: 2 additions & 23 deletions services/client/my-app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,2 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules
build
27 changes: 8 additions & 19 deletions services/client/my-app/Dockerfile-dev2
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Stage 1: Build the application
FROM node:14-alpine as builder
# Use an official Node runtime as a parent image
FROM node:14

# Set the working directory
# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and lock files
COPY package.json package-lock.json* yarn.lock* ./
# Copy package.json and package-lock.json (or yarn.lock)
COPY package.json ./
COPY package-lock.json* yarn.lock* ./

# Install dependencies including 'react'
# Install dependencies
RUN npm install --silent

# Copy the rest of your app's source code
Expand All @@ -16,21 +17,9 @@ COPY . .
# Build your app
RUN npm run build

# Stage 2: Setup the runtime environment
FROM node:14-alpine

# Set the working directory
WORKDIR /usr/src/app

# Install serve to serve your application
# Install `serve` to serve your application on port 3000
RUN npm install -g serve

# Copy the build directory from the builder stage
COPY --from=builder /usr/src/app/build ./build

# Copy node_modules from builder stage if needed for runtime
COPY --from=builder /usr/src/app/node_modules ./node_modules

# Command to run the app
CMD ["serve", "-s", "build", "-l", "3000"]

Expand Down
28 changes: 15 additions & 13 deletions services/client/my-app/Dockerfile-prod-2
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# services/client/my-app/Dockerfile-dev2
# Use NGINX to serve the static files
FROM nginx:1.15.8-alpine

# Use the official Node.js image as the base image
FROM node:14-alpine
# Remove the default NGINX configuration
RUN rm /etc/nginx/conf.d/default.conf

# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the site-specific configuration file
COPY prod.conf /etc/nginx/conf.d/default.conf

# Copy package.json and package-lock.json into the container
COPY package*.json ./
COPY nginx.conf /etc/nginx/nginx.conf

# Install dependencies
RUN npm install
COPY . /usr/share/nginx/html

# Copy the rest of the application code into the container
COPY . .
# Copy the build output to the NGINX html directory
# COPY /build /usr/share/nginx/html

# Command to start the application
CMD ["npm", "start"]
# Expose port 80 to the outside world
EXPOSE 80

# Start NGINX
CMD ["nginx", "-g", "daemon off;"]
17 changes: 17 additions & 0 deletions services/client/my-app/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# services/nginx/nginx.conf

worker_processes 1;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

# Include all config files from the conf.d directory
include /etc/nginx/conf.d/*.conf;
}
8 changes: 4 additions & 4 deletions services/client/my-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions services/client/my-app/prod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# services/nginx/prod.conf

server {
listen 80;

server_name localhost; # Use localhost for local development

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

location /api {
proxy_pass http://backend:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
39 changes: 34 additions & 5 deletions services/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
# Use an official Nginx image
FROM nginx:latest
# Stage 1: Build the React application
FROM mhart/alpine-node:12 AS builder

# Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/nginx.conf
WORKDIR /usr/src/app

# Remove the default Nginx configuration file
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# Copy package.json and package-lock.json separately to leverage Docker cache
COPY package.json /usr/src/app/package.json
COPY package-lock.json /usr/src/app/package-lock.json

# Install dependencies
RUN npm install

# Copy the rest of the application files
COPY . /usr/src/app

# Build the application
RUN npm run build --production

# Stage 2: Serve the React application with NGINX
FROM nginx:1.15.8-alpine

# Remove the default NGINX configuration
RUN rm /etc/nginx/conf.d/default.conf

# Copy the custom NGINX configuration file
COPY ./nginx.conf /etc/nginx/nginx.conf

# Copy the site-specific NGINX configuration file
COPY ./services/nginx/prod.conf /etc/nginx/conf.d/default.conf

# Copy the build output to the NGINX html directory
COPY --from=builder /usr/src/app/build /usr/share/nginx/html

# Expose port 80 to the outside world
EXPOSE 80

# Start NGINX
CMD ["nginx", "-g", "daemon off;"]
54 changes: 11 additions & 43 deletions services/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,49 +1,17 @@
# services/nginx/nginx.conf

worker_processes 1;

events {
# Define any event-related settings here
worker_connections 1024;
}

http {
resolver 127.0.0.11; # Use Docker's internal DNS resolver

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;

server {
listen 80;

root /usr/share/nginx/html;
index index.html index.htm;

location / {
proxy_pass http://localhost:8000;
try_files $uri $uri/ /index.html;
}

location /api/ {
proxy_pass http://backend:8000; # Ensure this matches the service name and port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
send_timeout 60s;
}

location /static/ {
alias /usr/share/nginx/html/static/;
}

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

types {
application/font-woff2 woff2;
application/font-woff woff;
}
}
# Include all config files from the conf.d directory
include /etc/nginx/conf.d/*.conf;
}
1 change: 1 addition & 0 deletions services/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ WORKDIR /tmp/db
RUN mkdir /data && chown postgres /data

COPY schema.sql /docker-entrypoint-initdb.d/
COPY words_alpha.txt /docker-entrypoint-initdb.d/
Loading

0 comments on commit cc71d8e

Please sign in to comment.