Added guide how to start project, added docker support, also fixed ids in backend

This commit is contained in:
Bogdan 2024-12-20 11:34:14 +03:00
parent 3b27feb430
commit 345644d3f7
8 changed files with 104 additions and 3 deletions

View File

@ -1 +1,4 @@
# My test social network site based on next.js and actix.rs
To start project just writ `docker compose up -d`
And go to http://127.0.0.1:3000/

5
backend/.dockerignore Normal file
View File

@ -0,0 +1,5 @@
target
Dockerfile
.dockerignore
.gitignore
README.md

17
backend/Dockerfile Normal file
View File

@ -0,0 +1,17 @@
# Use the official Rust image as the base image
FROM rust:latest
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy the Cargo.toml and Cargo.lock files
COPY Cargo.toml Cargo.lock ./
# Copy the source code
COPY src ./src
# Install dependencies and build the application
RUN cargo build --release
# Set the command to run the application
CMD ["./target/release/backend"]

View File

@ -31,7 +31,7 @@ async fn main() -> std::io::Result<()> {
.app_data(web::Data::new(db.clone()))
.configure(routes::init)
})
.bind(("127.0.0.1", 8080))?
.bind(("0.0.0.0", 8080))?
.run()
.await
}

View File

@ -47,13 +47,21 @@ async fn get_next_id(db: &MongoRepo) -> i32 {
// Initialize counter with 100
let initial_counter = Counter {
id: "user_id".to_string(),
seq: 100,
seq: 101,
};
counter_collection
.insert_one(initial_counter)
.await
.expect("Failed to initialize counter");
100
// Immediately increment to 101 for first user
let update = doc! { "$inc": { "seq": 1 } };
let updated = counter_collection
.find_one_and_update(filter, update)
.await
.expect("Failed to update counter")
.unwrap();
updated.seq // Will return 101 for first user
}
}
}

39
docker-compose.yml Normal file
View File

@ -0,0 +1,39 @@
services:
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- '27017:27017'
volumes:
- mongo-data:/data/db
networks:
- social-network
backend:
image: deusbog/backend-social-network
container_name: backend-social-network
environment:
- DATABASE_URL=mongodb://mongodb:27017/social_network
ports:
- '8080:8080'
depends_on:
- mongodb
networks:
- social-network
frontend:
image: deusbog/frontend-social-network
container_name: frontend-social-network
ports:
- '3000:3000'
depends_on:
- backend
networks:
- social-network
volumes:
mongo-data:
networks:
social-network:
driver: bridge

6
frontend/.dockerignore Normal file
View File

@ -0,0 +1,6 @@
**/node_modules/
**/.next/
.gitignore
README.md
Dockerfile
.dockerignore

23
frontend/Dockerfile Normal file
View File

@ -0,0 +1,23 @@
# Use the official Node.js image as the base image
FROM node:latest
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Build the Next.js application
RUN npm run build
# Expose the port the app runs on
EXPOSE 3000
# Start the Next.js application
CMD ["npm", "start"]