diff --git a/backend.Dockerfile b/backend.Dockerfile index 4514548..e1af73f 100644 --- a/backend.Dockerfile +++ b/backend.Dockerfile @@ -7,4 +7,4 @@ COPY .env . RUN npm i EXPOSE 3000 -ENTRYPOINT ["npm", "run", "start"] +CMD ["npm", "run", "start"] diff --git a/backend/src/controllers/user.ts b/backend/src/controllers/user.ts index 7970cba..5322e06 100644 --- a/backend/src/controllers/user.ts +++ b/backend/src/controllers/user.ts @@ -5,6 +5,7 @@ import jwt from 'jsonwebtoken'; import { AppDataSource } from '../data-source'; import { User } from "../entity/User"; import { Post } from "../entity/Post"; +import { MoreThan } from "typeorm"; class UserController { async register(req: Request, res: Response): Promise { @@ -35,11 +36,38 @@ class UserController { async getPosts(req: Request, res: Response): Promise { const { userId } = req.params; - const posts = await AppDataSource.manager.findBy(Post, {authorId: Number.parseInt(userId)}); - console.log(posts); - + const posts = await AppDataSource.manager.findBy(Post, { authorId: Number.parseInt(userId) }); + res.status(200).send(posts); } + + async getAll(req: Request, res: Response): Promise { + const users = await AppDataSource.manager.find(User, + { + where: + { id: MoreThan(0) }, + select: + { id: true, username: true, password_hash: false } + } + ); + + res.status(200).send(users); + } + + async getById(req: Request, res: Response): Promise { + const userId = Number.parseInt(req.params.userId); + + const user = await AppDataSource.manager.findOne(User, + { + where: + { id: userId }, + select: + { id: false, username: true, password_hash: false } + } + ); + + res.status(200).send(user); + } } export default new UserController(); \ No newline at end of file diff --git a/backend/src/index.ts b/backend/src/index.ts index efdd72e..92a2269 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -5,6 +5,7 @@ import fs from 'fs'; import cookieParser from 'cookie-parser'; import PostRouter from "./routers/post"; import session from "express-session"; +import path from "path"; const app = express(); @@ -16,6 +17,7 @@ app.use(session({ resave: false, saveUninitialized: false, })); +app.use('/files', express.static(path.join(process.env.UPLOAD_DESTINATION))); if (!fs.existsSync(process.env.UPLOAD_DESTINATION)) fs.mkdirSync(process.env.UPLOAD_DESTINATION); diff --git a/backend/src/middlewares/existance.ts b/backend/src/middlewares/existance.ts index 9d1bcf8..0d9b831 100644 --- a/backend/src/middlewares/existance.ts +++ b/backend/src/middlewares/existance.ts @@ -6,12 +6,20 @@ import { Post } from "../entity/Post"; const userShouldExist = async (req: Request, res: Response, next: NextFunction): Promise => { const { username } = req.body; const { userId } = req.params; - - if (!(await AppDataSource.manager.findOneBy(User, { - username - })) || !(await AppDataSource.manager.findOneBy(User, { - id: Number.parseInt(userId) - }))) { + let user; + if (username) { + user = await AppDataSource.manager.findOneBy(User, { + username + }); + } else if (Number.parseInt(userId)) { + user = await AppDataSource.manager.findOneBy(User, { + id: Number.parseInt(userId) + }); + } else { + res.status(404).send("User does not exist."); + return; + } + if (!user) { res.status(404).send("User does not exist."); return; } diff --git a/backend/src/routers/user.ts b/backend/src/routers/user.ts index eaf7e16..b7d88a8 100644 --- a/backend/src/routers/user.ts +++ b/backend/src/routers/user.ts @@ -8,5 +8,7 @@ const UserRouter = Router(); UserRouter.post('/register', existance.userShouldNotExist, UserController.register); UserRouter.post('/login', existance.userShouldExist, UserController.login); UserRouter.get('/posts/:userId', existance.userShouldExist, UserController.getPosts); +UserRouter.get('/all', UserController.getAll); +UserRouter.get('/by-id/:userId', existance.userShouldExist, UserController.getById); export default UserRouter; \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8dd2602..37fa3e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,10 +15,25 @@ services: context: . dockerfile: backend.Dockerfile env_file: .env - ports: - - 8080:${APP_PORT} depends_on: - database restart: on-failure volumes: - ./data/files:${UPLOAD_DESTINATION} +# frontend: +# build: +# context: . +# dockerfile: frontend.Dockerfile +# depends_on: +# - backend +# restart: on-failure + proxy: + image: 'nginx:latest' + ports: + - 8080:80 + depends_on: + # - frontend + - backend + restart: on-failure + volumes: + - ./data/nginx:/etc/nginx diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..7564b82 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,35 @@ +events {} + +http { + server { + listen 80; + server_name defaultserver; + client_max_body_size 200M; + location / { + proxy_pass http://frontend:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } + + location /api/v1/ { + proxy_pass http://backend:3000/api/v1/; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } + + location /media { + proxy_pass http://backend:3000/files; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } + } +}