some new endpoints and static serving for media
This commit is contained in:
parent
84f02d3f0d
commit
5994fb8f7f
|
@ -7,4 +7,4 @@ COPY .env .
|
|||
RUN npm i
|
||||
|
||||
EXPOSE 3000
|
||||
ENTRYPOINT ["npm", "run", "start"]
|
||||
CMD ["npm", "run", "start"]
|
||||
|
|
|
@ -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<void> {
|
||||
|
@ -36,10 +37,37 @@ class UserController {
|
|||
const { userId } = req.params;
|
||||
|
||||
const posts = await AppDataSource.manager.findBy(Post, { authorId: Number.parseInt(userId) });
|
||||
console.log(posts);
|
||||
|
||||
res.status(200).send(posts);
|
||||
}
|
||||
|
||||
async getAll(req: Request, res: Response): Promise<void> {
|
||||
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<void> {
|
||||
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();
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -6,12 +6,20 @@ import { Post } from "../entity/Post";
|
|||
const userShouldExist = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
||||
const { username } = req.body;
|
||||
const { userId } = req.params;
|
||||
|
||||
if (!(await AppDataSource.manager.findOneBy(User, {
|
||||
let user;
|
||||
if (username) {
|
||||
user = await AppDataSource.manager.findOneBy(User, {
|
||||
username
|
||||
})) || !(await AppDataSource.manager.findOneBy(User, {
|
||||
});
|
||||
} 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;
|
||||
}
|
||||
|
|
|
@ -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;
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue