-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
187 lines (154 loc) · 7.06 KB
/
server.py
File metadata and controls
187 lines (154 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""
Simple HTTP Server for Data Classification Research Web Application
This server provides a local development environment for the research data visualization web app.
It serves static files and can be extended to handle API requests for file operations.
Usage:
python server.py [port]
Default port: 8000
"""
import http.server
import socketserver
import os
import sys
import json
from urllib.parse import urlparse, parse_qs
import mimetypes
class ResearchDataHandler(http.server.SimpleHTTPRequestHandler):
"""Custom HTTP request handler for the research data web application."""
def __init__(self, *args, **kwargs):
# Set up MIME types for better file serving
mimetypes.add_type('application/javascript', '.js')
mimetypes.add_type('text/css', '.css')
super().__init__(*args, **kwargs)
def do_GET(self):
"""Handle GET requests."""
parsed_url = urlparse(self.path)
path = parsed_url.path
# Handle API endpoints
if path.startswith('/api/'):
self.handle_api_request(path, parsed_url.query)
return
# Serve static files
super().do_GET()
def handle_api_request(self, path, query):
"""Handle API requests for file operations."""
try:
if path == '/api/files':
self.handle_files_api()
elif path == '/api/file-content':
self.handle_file_content_api(query)
else:
self.send_error(404, "API endpoint not found")
except Exception as e:
self.send_error(500, f"Internal server error: {str(e)}")
def handle_files_api(self):
"""Return list of available files."""
try:
files = []
august_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'August')
print(f"Looking for August directory at: {august_dir}")
print(f"Directory exists: {os.path.exists(august_dir)}")
if os.path.exists(august_dir):
file_list = os.listdir(august_dir)
print(f"Found {len(file_list)} files in August directory")
for filename in file_list:
if filename.endswith(('.csv', '.txt')):
file_path = os.path.join(august_dir, filename)
file_size = os.path.getsize(file_path)
# Extract country code from filename
if filename.startswith('done_processed_') and filename.endswith('_data.csv'):
country_code = filename[15:-9] # Extract country code
file_type = 'csv'
records = self.count_csv_records(file_path)
elif filename.startswith('done_processed_') and filename.endswith('_data_stats.txt'):
country_code = filename[15:-14] # Extract country code
file_type = 'stats'
records = None
else:
continue
files.append({
'countryCode': country_code,
'fileName': filename,
'fileType': file_type,
'filePath': file_path,
'size': round(file_size / 1024, 1), # Convert to KB
'records': records
})
print(f"Returning {len(files)} files to client")
self.send_json_response(files)
except Exception as e:
print(f"Error in handle_files_api: {str(e)}")
import traceback
traceback.print_exc()
self.send_error(500, f"Error reading files: {str(e)}")
def handle_file_content_api(self, query):
"""Return content of a specific file."""
try:
params = parse_qs(query)
file_path = params.get('path', [None])[0]
if not file_path or not os.path.exists(file_path):
self.send_error(404, "File not found")
return
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
self.send_json_response({
'content': content,
'filename': os.path.basename(file_path)
})
except Exception as e:
self.send_error(500, f"Error reading file: {str(e)}")
def count_csv_records(self, file_path):
"""Count records in a CSV file (excluding header)."""
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
return max(0, len(lines) - 1) # Subtract header
except:
return None
def send_json_response(self, data):
"""Send JSON response."""
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
self.wfile.write(json.dumps(data).encode('utf-8'))
def log_message(self, format, *args):
"""Custom logging to show requests."""
print(f"[{self.log_date_time_string()}] {format % args}")
def main():
"""Main function to start the server."""
# Get port from environment variable (for Railway) or command line argument
port = int(os.environ.get('PORT', 8000))
if len(sys.argv) > 1:
try:
port = int(sys.argv[1])
except ValueError:
print("Invalid port number. Using default port 8000.")
# Change to the directory containing this script
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
print(f"Working directory: {os.getcwd()}")
print(f"Files in current directory: {os.listdir('.')}")
print(f"August directory exists: {os.path.exists('August')}")
with socketserver.TCPServer(("", port), ResearchDataHandler) as httpd:
print(f"🚀 Research Data Web Application Server")
print(f"📍 Serving at: http://localhost:{port}")
print(f"📁 Root directory: {os.getcwd()}")
print(f"📊 Data directory: {os.path.join(os.getcwd(), 'August')}")
print("\n✨ Features:")
print(" • Static file serving")
print(" • API endpoints for file operations")
print(" • CSV and stats file handling")
print(" • Cross-origin resource sharing enabled")
print("\n🛑 Press Ctrl+C to stop the server")
print("=" * 50)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n🛑 Server stopped by user")
httpd.shutdown()
if __name__ == "__main__":
main()