Created
September 6, 2020 11:52
-
-
Save Iftimie/f4a9397b0b5452ebd60fc58e2c015f49 to your computer and use it in GitHub Desktop.
Split the monolith into separate services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from trie import Trie | |
import pickle | |
import logging | |
import falcon | |
import json | |
class Collector(object): | |
def collect_phrase(self, phrase): | |
with open("phrases.txt", 'a') as f: | |
f.write(phrase+'\n') | |
trie = Trie() | |
with open("phrases.txt", 'r') as f: | |
for line in f: | |
trie.add_phrase(line) | |
trie_local_file_name = "/app/assembler/collector/shared_data/trie.dat" | |
pickle.dump(trie, open(trie_local_file_name, "wb")) | |
class SearchResource(object): | |
def __init__(self): | |
self._collector = Collector() | |
def on_post(self, req, resp): | |
self._collector.collect_phrase(req.params['phrase']) | |
resp.status = falcon.HTTP_200 | |
app = falcon.API() | |
app.add_route('/search', SearchResource()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import falcon | |
import json | |
import logging | |
import os | |
import pickle | |
from trie import Trie | |
class Backend: | |
def __init__(self): | |
self._logger = logging.getLogger('gunicorn.error') | |
def top_phrases_for_prefix(self, prefix): | |
trie = self._load_trie() | |
return trie.top_phrases_for_prefix(prefix) | |
def _load_trie(self): | |
trie_local_file_name = "/app/distributor/backend/shared_data/trie.dat" | |
if os.path.exists(trie_local_file_name): | |
trie = pickle.load( open(trie_local_file_name, "rb")) | |
else: | |
trie = Trie() | |
return trie | |
class TopPhrasesResource(object): | |
def __init__(self): | |
self._backend = Backend() | |
def on_get(self, req, resp): | |
top_phrases = self._backend.top_phrases_for_prefix(req.params['prefix']) | |
response_body = json.dumps({"status": "success","data": {"top_phrases": top_phrases }}) | |
resp.status = falcon.HTTP_200 | |
resp.body = response_body | |
app = falcon.API() | |
app.add_route('/top-phrases', TopPhrasesResource()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.3' | |
services: | |
gateway: | |
container_name: gateway | |
image: nginx:1.19-alpine | |
ports: | |
- "80:80" | |
depends_on: | |
- distributor.backend | |
- assembler.collector | |
volumes: | |
- ./gateway/nginx.conf:/etc/nginx/nginx.conf | |
- ./gateway/www:/var/www | |
- ./gateway/log:/var/log/nginx | |
distributor.backend: | |
build: ./distributor/backend | |
ports: | |
- "6000:6000" | |
volumes: | |
- ./distributor/backend/main.py:/app/distributor/backend/main.py | |
- ./shared/trie.py:/app/distributor/backend/trie.py | |
- ./shared/shared_data:/app/distributor/backend/shared_data | |
command: gunicorn --chdir /app/distributor/backend main:app -b 0.0.0.0:6000 --reload | |
assembler.collector: | |
build: ./assembler/collector | |
ports: | |
- "5000:5000" | |
volumes: | |
- ./assembler/collector/main.py:/app/assembler/collector/main.py | |
- ./shared/trie.py:/app/assembler/collector/trie.py | |
- ./shared/shared_data:/app/assembler/collector/shared_data | |
command: gunicorn --chdir /app/assembler/collector main:app -b 0.0.0.0:5000 --reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
events {} | |
http { | |
rewrite_log on; | |
include /etc/nginx/mime.types; | |
server { | |
location / { | |
index index.html; | |
root /var/www; | |
} | |
location /top-phrases { | |
proxy_pass http://distributor.backend:6000; | |
} | |
location /search { | |
proxy_pass http://assembler.collector:5000; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment