Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM python:3.12-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
nginx \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

COPY nginx/backend.conf /etc/nginx/conf.d/default.conf

RUN mkdir -p /sockets

ENV SOCKET_PATH=/sockets/backend.sock

CMD ["sh", "-c", "rm -f $SOCKET_PATH && gunicorn -w 2 -b unix:$SOCKET_PATH backend:app & nginx -g 'daemon off;'"]
19 changes: 19 additions & 0 deletions backend/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import requests
from flask import Flask


app = Flask(__name__)

@app.route("/cat_get")
def hello_world():
resp = requests.get('https://api.thecatapi.com/v1/images/search')
cat_json = resp.json()

if cat_json:
return cat_json[0].get('url', '')
else:
return ''

if __name__ == '__main__':
app.run(debug=True, host='::', port='3001')

9 changes: 9 additions & 0 deletions backend/nginx/backend.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server{
listen 8080;

location / {
proxy_pass http://unix:/sockets/backend.sock:;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
3 changes: 3 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask
requests
gunicorn
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
frontend:
build: ./frontend
ports:
- "80:80"
depends_on:
- backend

backend:
build: ./backend

volumes:
sockets:
17 changes: 17 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:20-alpine

RUN apk add --no-cache nginx

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

COPY nginx/frontend.conf /etc/nginx/http.d/default.conf

RUN mkdir -p /sockets /run/nginx
ENV SOCKET_PATH=/sockets/frontend.sock

CMD ["sh", "-c", "rm -f $SOCKET_PATH && node frontend.js & nginx -g 'daemon off;'"]
22 changes: 22 additions & 0 deletions frontend/frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require("express");
const path = require("path");
const fs = require("fs")

const app = express();
app.set('views', path.join(__dirname, '/'));
app.set('view engine', 'jade');

const socketPath = "/sockets/frontend.sock";
const backendUrl = "http://backend:8080";

app.get('/', async (req, res) => {
const cat_data = await fetch(`${backendUrl}/cat_get`);
const data = await cat_data.text();

res.render('index.jade', { title: "KITTY Frontend", cat_url: data });
});

app.listen(socketPath, () => {
try{ fs.chmodSync(socketPath, parseInt("777",8))} catch (e) { console.log(e);}
console.log(`Frontend app listening on port ${socketPath}`)
})
5 changes: 5 additions & 0 deletions frontend/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
html
head
title= title
body
img(src= cat_url)
15 changes: 15 additions & 0 deletions frontend/nginx/frontend.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
server {
listen 80;

location / {
proxy_pass http://unix:/sockets/frontend.sock:;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /api/ {
proxy_pass http://backend:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
16 changes: 16 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "hw",
"version": "1.0.0",
"author": "Slava Fedorov <slava@fuodorov.ru>",
"dependencies": {
"express": "latest",
"jade": "latest"
},
"devDependencies": {
"@types/express": "^4.17.21",
"typescript": "^5.5.3"
},
"scripts": {
"build": "tsc frontend.ts"
}
}