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
12 changes: 12 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.12-slim

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

COPY . .

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"]
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')

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
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
nginx:
image: nginx:1.27-alpine
ports:
- "80:80"
volumes:
- sockets:/sockets
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
depends_on:
- frontend
- backend

frontend:
build: ./frontend
volumes:
- sockets:/sockets
environment:
- SOCKET_PATH=/sockets/frontend.sock

backend:
build: ./backend
volumes:
- sockets:/sockets
environment:
- SOCKET_PATH=/sockets/backend.sock

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

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

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

CMD ["sh", "-c", "rm -f $SOCKET_PATH && node frontend.js"]
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://nginx/api";

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)
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 <[email protected]>",
"dependencies": {
"express": "latest",
"jade": "latest"
},
"devDependencies": {
"@types/express": "^4.17.21",
"typescript": "^5.5.3"
},
"scripts": {
"build": "tsc frontend.ts"
}
}
15 changes: 15 additions & 0 deletions nginx/conf.d/default.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://unix:/sockets/backend.sock:/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
13 changes: 13 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
worker_processes auto;

events { worker_connections 1024; }

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

include /etc/nginx/conf.d/*.conf;
}