|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 5 | + |
| 6 | +logging.basicConfig(filename="oncat_server.log", level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") |
| 7 | + |
| 8 | + |
| 9 | +class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): |
| 10 | + """Fake ONCat server for testing purposes""" |
| 11 | + |
| 12 | + def do_POST(self): |
| 13 | + if self.headers.get("Authorization") != "Bearer test-token": |
| 14 | + self.send_response(403) |
| 15 | + self.send_header("Content-type", "application/json") |
| 16 | + self.end_headers() |
| 17 | + response = {"error": "Forbidden"} |
| 18 | + self.wfile.write(json.dumps(response).encode("utf-8")) |
| 19 | + return |
| 20 | + |
| 21 | + if self.path.startswith("/api/datafiles/"): |
| 22 | + location = self.path.replace("/api/datafiles", "").replace("/ingest", "") |
| 23 | + logging.info("Received datafile ingest request for %s", location) |
| 24 | + elif self.path.startswith("/api/reductions/"): |
| 25 | + location = self.path.replace("/api/reductions", "").replace("/ingest", "") |
| 26 | + logging.info("Received reduction ingest request for %s", location) |
| 27 | + else: |
| 28 | + self.send_response(400) |
| 29 | + self.send_header("Content-type", "application/json") |
| 30 | + self.end_headers() |
| 31 | + logging.error("Unknown endpoint: %s", self.path) |
| 32 | + response = {"error": "Unknown endpoint"} |
| 33 | + self.wfile.write(json.dumps(response).encode("utf-8")) |
| 34 | + return |
| 35 | + |
| 36 | + if not os.path.isfile(location): |
| 37 | + self.send_response(404) |
| 38 | + self.send_header("Content-type", "application/json") |
| 39 | + self.end_headers() |
| 40 | + logging.error("File not found: %s", location) |
| 41 | + response = {"error": "File not found"} |
| 42 | + self.wfile.write(json.dumps(response).encode("utf-8")) |
| 43 | + return |
| 44 | + |
| 45 | + try: |
| 46 | + # assume the format /facility/instrument/experiment/nexus/instrument_run_number.nxs.h5 |
| 47 | + # the real server would be more complex |
| 48 | + _, facility, instrument, experiment, *_, filename = location.split("/") |
| 49 | + run_number = int(filename.split("_")[-1].split(".")[0]) |
| 50 | + except Exception: |
| 51 | + self.send_response(400) |
| 52 | + self.send_header("Content-type", "application/json") |
| 53 | + self.end_headers() |
| 54 | + logging.error("Invalid path format: %s", location) |
| 55 | + response = {"error": "Invalid path format"} |
| 56 | + self.wfile.write(json.dumps(response).encode("utf-8")) |
| 57 | + return |
| 58 | + |
| 59 | + # Send response |
| 60 | + self.send_response(200) |
| 61 | + self.send_header("Content-type", "application/json") |
| 62 | + self.end_headers() |
| 63 | + response = { |
| 64 | + "location": location, |
| 65 | + "facility": facility, |
| 66 | + "instrument": instrument, |
| 67 | + "experiment": experiment, |
| 68 | + "indexed": {"run_number": run_number}, |
| 69 | + } |
| 70 | + self.wfile.write(json.dumps(response).encode("utf-8")) |
| 71 | + |
| 72 | + |
| 73 | +def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=8000): |
| 74 | + server_address = ("", port) |
| 75 | + httpd = server_class(server_address, handler_class) |
| 76 | + httpd.serve_forever() |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + run() |
0 commit comments