Skip to content

Commit

Permalink
The server now has authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasmus authored and Rasmus committed Nov 29, 2023
1 parent aff69c6 commit 5d8e869
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "-u", "-m", "server.server", "--host", "0.0.0.0", "--port", "8000", "--reload"]
CMD ["python", "-u", "-m", "server.server", "--host", "0.0.0.0", "--port", "80", "--reload"]
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ services:
server:
build: . # Dockerfile location
container_name: server-container
command: python -u -m server.server --host 0.0.0.0 --port 8000 --reload
command: python -u -m server.server --host 0.0.0.0 --port 80 --reload
volumes:
- .:/code # Mount current directory to /code in the image
ports:
- "8000:8000"
- "8000:80"
13 changes: 11 additions & 2 deletions server/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import os
from relation_extraction.NaiveMVP.main import handle_relation_post_request

class PreProcessingHandler(BaseHTTPRequestHandler):
Expand All @@ -9,6 +10,14 @@ def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_content = {"post_data": self.rfile.read(content_length), "post_json": {}}

if self.headers.get("Access-Authorization").__str__() != os.getenv("API_SECRET"):
message = "Unauthorized"
self.send_response(401)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(bytes(f"Error occured! {message}", "utf8"))
return

if not self.handled_request_body(post_content):
return

Expand Down Expand Up @@ -47,6 +56,6 @@ def handled_request_body(self, post_content):


if __name__ == '__main__':
with HTTPServer(('', 8000), PreProcessingHandler) as server:
print("Hosting server on 0.0.0.0:8000")
with HTTPServer(('', 80), PreProcessingHandler) as server:
print("Hosting server on 0.0.0.0:80")
server.serve_forever()

0 comments on commit 5d8e869

Please sign in to comment.