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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string, jsonify, request
|
| 2 |
+
import requests
|
| 3 |
+
import time
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from threading import Thread
|
| 8 |
+
import schedule
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Store websites and their status
|
| 13 |
+
websites_file = 'websites.json'
|
| 14 |
+
history_file = 'history.json'
|
| 15 |
+
|
| 16 |
+
# Default websites
|
| 17 |
+
default_websites = [
|
| 18 |
+
{"id": 1, "name": "Google", "url": "https://www.google.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"},
|
| 19 |
+
{"id": 2, "name": "GitHub", "url": "https://github.com", "status": "unknown", "uptime": 100, "response_time": 0, "last_checked": "Never"}
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def load_websites():
|
| 23 |
+
if os.path.exists(websites_file):
|
| 24 |
+
try:
|
| 25 |
+
with open(websites_file, 'r') as f:
|
| 26 |
+
return json.load(f)
|
| 27 |
+
except:
|
| 28 |
+
return default_websites.copy()
|
| 29 |
+
return default_websites.copy()
|
| 30 |
+
|
| 31 |
+
def save_websites(websites):
|
| 32 |
+
with open(websites_file, 'w') as f:
|
| 33 |
+
json.dump(websites, f, indent=2)
|
| 34 |
+
|
| 35 |
+
def load_history():
|
| 36 |
+
if os.path.exists(history_file):
|
| 37 |
+
try:
|
| 38 |
+
with open(history_file, 'r') as f:
|
| 39 |
+
return json.load(f)
|
| 40 |
+
except:
|
| 41 |
+
return {}
|
| 42 |
+
return {}
|
| 43 |
+
|
| 44 |
+
def save_history(history):
|
| 45 |
+
with open(history_file, 'w') as f:
|
| 46 |
+
json.dump(history, f, indent=2)
|
| 47 |
+
|
| 48 |
+
def check_website(website):
|
| 49 |
+
try:
|
| 50 |
+
start_time = time.time()
|
| 51 |
+
response = requests.get(website['url'], timeout=30, allow_redirects=True)
|
| 52 |
+
response_time = round((time.time() - start_time) * 1000, 2)
|
| 53 |
+
|
| 54 |
+
if response.status_code < 400:
|
| 55 |
+
return {
|
| 56 |
+
'status': 'up',
|
| 57 |
+
'response_time': response_time,
|
| 58 |
+
'status_code': response.status_code
|
| 59 |
+
}
|
| 60 |
+
else:
|
| 61 |
+
return {
|
| 62 |
+
'status': 'down',
|
| 63 |
+
'response_time': response_time,
|
| 64 |
+
'status_code': response.status_code
|
| 65 |
+
}
|
| 66 |
+
except requests.exceptions.RequestException as e:
|
| 67 |
+
return {
|
| 68 |
+
'status': 'down',
|
| 69 |
+
'response_time': 0,
|
| 70 |
+
'error': str(e)
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
def check_all_websites():
|
| 74 |
+
websites = load_websites()
|
| 75 |
+
history = load_history()
|
| 76 |
+
|
| 77 |
+
for website in websites:
|
| 78 |
+
result = check_website(website)
|
| 79 |
+
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 80 |
+
|
| 81 |
+
website['last_checked'] = now
|
| 82 |
+
website['response_time'] = result['response_time']
|
| 83 |
+
|
| 84 |
+
if result['status'] == 'up':
|
| 85 |
+
website['status'] = 'up'
|
| 86 |
+
else:
|
| 87 |
+
website['status'] = 'down'
|
| 88 |
+
|
| 89 |
+
site_id = str(website['id'])
|
| 90 |
+
if site_id not in history:
|
| 91 |
+
history[site_id] = []
|
| 92 |
+
|
| 93 |
+
history[site_id].append({
|
| 94 |
+
'timestamp': now,
|
| 95 |
+
'status': result['status'],
|
| 96 |
+
'response_time': result['response_time']
|
| 97 |
+
})
|
| 98 |
+
|
| 99 |
+
if len(history[site_id]) > 1000:
|
| 100 |
+
history[site_id] = history[site_id][-1000:]
|
| 101 |
+
|
| 102 |
+
if history[site_id]:
|
| 103 |
+
up_count = sum(1 for h in history[site_id] if h['status'] == 'up')
|
| 104 |
+
website['uptime'] = round((up_count / len(history[site_id])) * 100, 2)
|
| 105 |
+
|
| 106 |
+
save_websites(websites)
|
| 107 |
+
save_history(history)
|
| 108 |
+
print(f"[{now}] Checked {len(websites)} websites")
|
| 109 |
+
|
| 110 |
+
def run_scheduler():
|
| 111 |
+
schedule.every(5).minutes.do(check_all_websites)
|
| 112 |
+
while True:
|
| 113 |
+
schedule.run_pending()
|
| 114 |
+
time.sleep(1)
|
| 115 |
+
|
| 116 |
+
# Initialize
|
| 117 |
+
websites = load_websites()
|
| 118 |
+
if not websites:
|
| 119 |
+
websites = default_websites.copy()
|
| 120 |
+
save_websites(websites)
|
| 121 |
+
|
| 122 |
+
# Start scheduler
|
| 123 |
+
scheduler_thread = Thread(target=run_scheduler, daemon=True)
|
| 124 |
+
scheduler_thread.start()
|
| 125 |
+
|
| 126 |
+
check_all_websites()
|
| 127 |
+
|
| 128 |
+
# Inline HTML - NO templates folder needed
|
| 129 |
+
HTML_TEMPLATE = """
|
| 130 |
+
<!DOCTYPE html>
|
| 131 |
+
<html lang="en">
|
| 132 |
+
<head>
|
| 133 |
+
<meta charset="UTF-8">
|
| 134 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 135 |
+
<title>Uptime Robot - Website Monitor</title>
|
| 136 |
+
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
|
| 137 |
+
<style>
|
| 138 |
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 139 |
+
body {
|
| 140 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 141 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 142 |
+
min-height: 100vh;
|
| 143 |
+
padding: 20px;
|
| 144 |
+
}
|
| 145 |
+
.container { max-width: 1200px; margin: 0 auto; }
|
| 146 |
+
.header {
|
| 147 |
+
text-align: center; color: white; margin-bottom: 40px; padding: 20px;
|
| 148 |
+
}
|
| 149 |
+
.header h1 { font-size: 3rem; margin-bottom: 10px; text-shadow: 2px 2px 4px rgba(0,0,0,0.3); }
|
| 150 |
+
.header p { font-size: 1.2rem; opacity: 0.9; }
|
| 151 |
+
.stats-grid {
|
| 152 |
+
display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
| 153 |
+
gap: 20px; margin-bottom: 30px;
|
| 154 |
+
}
|
| 155 |
+
.stat-card {
|
| 156 |
+
background: white; border-radius: 15px; padding: 25px;
|
| 157 |
+
text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 158 |
+
transition: transform 0.3s ease;
|
| 159 |
+
}
|
| 160 |
+
.stat-card:hover { transform: translateY(-5px); }
|
| 161 |
+
.stat-card i { font-size: 2.5rem; margin-bottom: 15px; }
|
| 162 |
+
.stat-card.up i { color: #28a745; }
|
| 163 |
+
.stat-card.down i { color: #dc3545; }
|
| 164 |
+
.stat-card.total i { color: #007bff; }
|
| 165 |
+
.stat-card.uptime i { color: #ffc107; }
|
| 166 |
+
.stat-card h3 { font-size: 2rem; margin-bottom: 5px; color: #333; }
|
| 167 |
+
.stat-card p { color: #666; font-size: 1rem; }
|
| 168 |
+
.add-website-form {
|
| 169 |
+
background: white; border-radius: 15px; padding: 30px;
|
| 170 |
+
margin-bottom: 30px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 171 |
+
}
|
| 172 |
+
.add-website-form h2 {
|
| 173 |
+
margin-bottom: 20px; color: #333;
|
| 174 |
+
display: flex; align-items: center; gap: 10px;
|
| 175 |
+
}
|
| 176 |
+
.form-group {
|
| 177 |
+
display: flex; gap: 15px; flex-wrap: wrap;
|
| 178 |
+
}
|
| 179 |
+
.form-group input {
|
| 180 |
+
flex: 1; min-width: 200px; padding: 12px 15px;
|
| 181 |
+
border: 2px solid #e0e0e0; border-radius: 10px;
|
| 182 |
+
font-size: 1rem; transition: border-color 0.3s;
|
| 183 |
+
}
|
| 184 |
+
.form-group input:focus { outline: none; border-color: #667eea; }
|
| 185 |
+
.btn {
|
| 186 |
+
padding: 12px 25px; border: none; border-radius: 10px;
|
| 187 |
+
font-size: 1rem; cursor: pointer; transition: all 0.3s;
|
| 188 |
+
display: inline-flex; align-items: center; gap: 8px;
|
| 189 |
+
}
|
| 190 |
+
.btn-primary {
|
| 191 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 192 |
+
color: white;
|
| 193 |
+
}
|
| 194 |
+
.btn-primary:hover {
|
| 195 |
+
transform: translateY(-2px);
|
| 196 |
+
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
| 197 |
+
}
|
| 198 |
+
.btn-success {
|
| 199 |
+
background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
|
| 200 |
+
color: white;
|
| 201 |
+
}
|
| 202 |
+
.btn-danger {
|
| 203 |
+
background: linear-gradient(135deg, #dc3545 0%, #c82333 100%);
|
| 204 |
+
color: white;
|
| 205 |
+
}
|
| 206 |
+
.btn-sm { padding: 8px 15px; font-size: 0.9rem; }
|
| 207 |
+
.websites-list {
|
| 208 |
+
background: white; border-radius: 15px; padding: 30px;
|
| 209 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 210 |
+
}
|
| 211 |
+
.websites-list h2 {
|
| 212 |
+
margin-bottom: 25px; color: #333;
|
| 213 |
+
display: flex; align-items: center;
|
| 214 |
+
justify-content: space-between; flex-wrap: wrap; gap: 15px;
|
| 215 |
+
}
|
| 216 |
+
.website-card {
|
| 217 |
+
border: 2px solid #f0f0f0; border-radius: 12px;
|
| 218 |
+
padding: 20px; margin-bottom: 15px;
|
| 219 |
+
transition: all 0.3s; position: relative; overflow: hidden;
|
| 220 |
+
}
|
| 221 |
+
.website-card::before {
|
| 222 |
+
content: ''; position: absolute; left: 0; top: 0; bottom: 0;
|
| 223 |
+
width: 5px; transition: background-color 0.3s;
|
| 224 |
+
}
|
| 225 |
+
.website-card.up::before { background-color: #28a745; }
|
| 226 |
+
.website-card.down::before { background-color: #dc3545; }
|
| 227 |
+
.website-card.unknown::before { background-color: #6c757d; }
|
| 228 |
+
.website-card:hover {
|
| 229 |
+
border-color: #667eea; transform: translateX(5px);
|
| 230 |
+
}
|
| 231 |
+
.website-header {
|
| 232 |
+
display: flex; justify-content: space-between;
|
| 233 |
+
align-items: center; margin-bottom: 15px;
|
| 234 |
+
flex-wrap: wrap; gap: 10px;
|
| 235 |
+
}
|
| 236 |
+
.website-name { font-size: 1.3rem; font-weight: bold; color: #333; }
|
| 237 |
+
.website-url {
|
| 238 |
+
color: #667eea; text-decoration: none; font-size: 0.95rem;
|
| 239 |
+
word-break: break-all;
|
| 240 |
+
}
|
| 241 |
+
.website-url:hover { text-decoration: underline; }
|
| 242 |
+
.status-badge {
|
| 243 |
+
padding: 6px 15px; border-radius: 20px;
|
| 244 |
+
font-size: 0.85rem; font-weight: bold; text-transform: uppercase;
|
| 245 |
+
}
|
| 246 |
+
.status-up { background: #d4edda; color: #155724; }
|
| 247 |
+
.status-down { background: #f8d7da; color: #721c24; }
|
| 248 |
+
.status-unknown { background: #e2e3e5; color: #383d41; }
|
| 249 |
+
.website-stats {
|
| 250 |
+
display: grid;
|
| 251 |
+
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
| 252 |
+
gap: 15px; margin-top: 15px;
|
| 253 |
+
}
|
| 254 |
+
.website-stat {
|
| 255 |
+
text-align: center; padding: 10px;
|
| 256 |
+
background: #f8f9fa; border-radius: 8px;
|
| 257 |
+
}
|
| 258 |
+
.website-stat-value { font-size: 1.5rem; font-weight: bold; color: #333; }
|
| 259 |
+
.website-stat-label { font-size: 0.85rem; color: #666; margin-top: 5px; }
|
| 260 |
+
.website-actions { display: flex; gap: 10px; margin-top: 15px; }
|
| 261 |
+
.progress-bar {
|
| 262 |
+
width: 100%; height: 8px; background: #e9ecef;
|
| 263 |
+
border-radius: 4px; overflow: hidden; margin-top: 10px;
|
| 264 |
+
}
|
| 265 |
+
.progress-fill {
|
| 266 |
+
height: 100%; border-radius: 4px; transition: width 0.5s ease;
|
| 267 |
+
}
|
| 268 |
+
.progress-fill.high { background: linear-gradient(90deg, #28a745, #20c997); }
|
| 269 |
+
.progress-fill.medium { background: linear-gradient(90deg, #ffc107, #ff9800); }
|
| 270 |
+
.progress-fill.low { background: linear-gradient(90deg, #dc3545, #ff5722); }
|
| 271 |
+
.loading { text-align: center; padding: 40px; color: #666; }
|
| 272 |
+
.loading i { font-size: 3rem; animation: spin 1s linear infinite; }
|
| 273 |
+
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
|
| 274 |
+
.empty-state { text-align: center; padding: 60px 20px; color: #666; }
|
| 275 |
+
.empty-state i { font-size: 4rem; margin-bottom: 20px; color: #ddd; }
|
| 276 |
+
.toast {
|
| 277 |
+
position: fixed; bottom: 20px; right: 20px;
|
| 278 |
+
padding: 15px 25px; border-radius: 10px;
|
| 279 |
+
color: white; font-weight: bold; z-index: 1000;
|
| 280 |
+
animation: slideIn 0.3s ease;
|
| 281 |
+
}
|
| 282 |
+
@keyframes slideIn { from { transform: translateX(400px); } to { transform: translateX(0); } }
|
| 283 |
+
.toast.success { background: #28a745; }
|
| 284 |
+
.toast.error { background: #dc3545; }
|
| 285 |
+
@media (max-width: 768px) {
|
| 286 |
+
.header h1 { font-size: 2rem; }
|
| 287 |
+
.form-group { flex-direction: column; }
|
| 288 |
+
.form-group input { width: 100%; }
|
| 289 |
+
.website-header { flex-direction: column; align-items: flex-start; }
|
| 290 |
+
}
|
| 291 |
+
</style>
|
| 292 |
+
</head>
|
| 293 |
+
<body>
|
| 294 |
+
<div class="container">
|
| 295 |
+
<div class="header">
|
| 296 |
+
<h1><i class="fas fa-heartbeat"></i> Uptime Robot</h1>
|
| 297 |
+
<p>Monitor your websites 24/7</p>
|
| 298 |
+
</div>
|
| 299 |
+
|
| 300 |
+
<div class="stats-grid" id="statsGrid">
|
| 301 |
+
<div class="stat-card total">
|
| 302 |
+
<i class="fas fa-globe"></i>
|
| 303 |
+
<h3 id="totalSites">0</h3>
|
| 304 |
+
<p>Total Websites</p>
|
| 305 |
+
</div>
|
| 306 |
+
<div class="stat-card up">
|
| 307 |
+
<i class="fas fa-check-circle"></i>
|
| 308 |
+
<h3 id="upSites">0</h3>
|
| 309 |
+
<p>Websites Up</p>
|
| 310 |
+
</div>
|
| 311 |
+
<div class="stat-card down">
|
| 312 |
+
<i class="fas fa-times-circle"></i>
|
| 313 |
+
<h3 id="downSites">0</h3>
|
| 314 |
+
<p>Websites Down</p>
|
| 315 |
+
</div>
|
| 316 |
+
<div class="stat-card uptime">
|
| 317 |
+
<i class="fas fa-chart-line"></i>
|
| 318 |
+
<h3 id="avgUptime">100%</h3>
|
| 319 |
+
<p>Average Uptime</p>
|
| 320 |
+
</div>
|
| 321 |
+
</div>
|
| 322 |
+
|
| 323 |
+
<div class="add-website-form">
|
| 324 |
+
<h2><i class="fas fa-plus-circle"></i> Add Website</h2>
|
| 325 |
+
<div class="form-group">
|
| 326 |
+
<input type="text" id="siteName" placeholder="Website Name (e.g., My Blog)">
|
| 327 |
+
<input type="url" id="siteUrl" placeholder="Website URL (e.g., https://example.com)">
|
| 328 |
+
<button class="btn btn-primary" onclick="addWebsite()">
|
| 329 |
+
<i class="fas fa-plus"></i> Add Website
|
| 330 |
+
</button>
|
| 331 |
+
</div>
|
| 332 |
+
</div>
|
| 333 |
+
|
| 334 |
+
<div class="websites-list">
|
| 335 |
+
<h2>
|
| 336 |
+
<span><i class="fas fa-list"></i> Monitored Websites</span>
|
| 337 |
+
<button class="btn btn-success btn-sm" onclick="checkAllNow()">
|
| 338 |
+
<i class="fas fa-sync-alt"></i> Check All Now
|
| 339 |
+
</button>
|
| 340 |
+
</h2>
|
| 341 |
+
<div id="websitesContainer">
|
| 342 |
+
<div class="loading">
|
| 343 |
+
<i class="fas fa-spinner"></i>
|
| 344 |
+
<p>Loading websites...</p>
|
| 345 |
+
</div>
|
| 346 |
+
</div>
|
| 347 |
+
</div>
|
| 348 |
+
</div>
|
| 349 |
+
|
| 350 |
+
<div id="toastContainer"></div>
|
| 351 |
+
|
| 352 |
+
<script>
|
| 353 |
+
let websites = [];
|
| 354 |
+
|
| 355 |
+
document.addEventListener('DOMContentLoaded', loadWebsites);
|
| 356 |
+
setInterval(loadWebsites, 30000);
|
| 357 |
+
|
| 358 |
+
async function loadWebsites() {
|
| 359 |
+
try {
|
| 360 |
+
const response = await fetch('/api/websites');
|
| 361 |
+
websites = await response.json();
|
| 362 |
+
renderWebsites();
|
| 363 |
+
updateStats();
|
| 364 |
+
} catch (error) {
|
| 365 |
+
showToast('Error loading websites', 'error');
|
| 366 |
+
}
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
function renderWebsites() {
|
| 370 |
+
const container = document.getElementById('websitesContainer');
|
| 371 |
+
|
| 372 |
+
if (websites.length === 0) {
|
| 373 |
+
container.innerHTML = `
|
| 374 |
+
<div class="empty-state">
|
| 375 |
+
<i class="fas fa-inbox"></i>
|
| 376 |
+
<h3>No websites added yet</h3>
|
| 377 |
+
<p>Add a website above to start monitoring</p>
|
| 378 |
+
</div>
|
| 379 |
+
`;
|
| 380 |
+
return;
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
+
container.innerHTML = websites.map(site => `
|
| 384 |
+
<div class="website-card ${site.status}">
|
| 385 |
+
<div class="website-header">
|
| 386 |
+
<div>
|
| 387 |
+
<div class="website-name">${escapeHtml(site.name)}</div>
|
| 388 |
+
<a href="${escapeHtml(site.url)}" target="_blank" class="website-url">
|
| 389 |
+
<i class="fas fa-external-link-alt"></i> ${escapeHtml(site.url)}
|
| 390 |
+
</a>
|
| 391 |
+
</div>
|
| 392 |
+
<span class="status-badge status-${site.status}">
|
| 393 |
+
<i class="fas fa-${site.status === 'up' ? 'check' : site.status === 'down' ? 'times' : 'question'}"></i>
|
| 394 |
+
${site.status.toUpperCase()}
|
| 395 |
+
</span>
|
| 396 |
+
</div>
|
| 397 |
+
|
| 398 |
+
<div class="progress-bar">
|
| 399 |
+
<div class="progress-fill ${site.uptime >= 99 ? 'high' : site.uptime >= 95 ? 'medium' : 'low'}"
|
| 400 |
+
style="width: ${site.uptime}%"></div>
|
| 401 |
+
</div>
|
| 402 |
+
|
| 403 |
+
<div class="website-stats">
|
| 404 |
+
<div class="website-stat">
|
| 405 |
+
<div class="website-stat-value">${site.uptime}%</div>
|
| 406 |
+
<div class="website-stat-label">Uptime</div>
|
| 407 |
+
</div>
|
| 408 |
+
<div class="website-stat">
|
| 409 |
+
<div class="website-stat-value">${site.response_time}ms</div>
|
| 410 |
+
<div class="website-stat-label">Response Time</div>
|
| 411 |
+
</div>
|
| 412 |
+
<div class="website-stat">
|
| 413 |
+
<div class="website-stat-value">${formatTime(site.last_checked)}</div>
|
| 414 |
+
<div class="website-stat-label">Last Checked</div>
|
| 415 |
+
</div>
|
| 416 |
+
</div>
|
| 417 |
+
|
| 418 |
+
<div class="website-actions">
|
| 419 |
+
<button class="btn btn-danger btn-sm" onclick="deleteWebsite(${site.id})">
|
| 420 |
+
<i class="fas fa-trash"></i> Remove
|
| 421 |
+
</button>
|
| 422 |
+
</div>
|
| 423 |
+
</div>
|
| 424 |
+
`).join('');
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
function updateStats() {
|
| 428 |
+
const total = websites.length;
|
| 429 |
+
const up = websites.filter(w => w.status === 'up').length;
|
| 430 |
+
const down = websites.filter(w => w.status === 'down').length;
|
| 431 |
+
const avgUptime = total > 0 ? (websites.reduce((sum, w) => sum + w.uptime, 0) / total).toFixed(2) : 100;
|
| 432 |
+
|
| 433 |
+
document.getElementById('totalSites').textContent = total;
|
| 434 |
+
document.getElementById('upSites').textContent = up;
|
| 435 |
+
document.getElementById('downSites').textContent = down;
|
| 436 |
+
document.getElementById('avgUptime').textContent = avgUptime + '%';
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
async function addWebsite() {
|
| 440 |
+
const nameInput = document.getElementById('siteName');
|
| 441 |
+
const urlInput = document.getElementById('siteUrl');
|
| 442 |
+
|
| 443 |
+
const name = nameInput.value.trim();
|
| 444 |
+
let url = urlInput.value.trim();
|
| 445 |
+
|
| 446 |
+
if (!name || !url) {
|
| 447 |
+
showToast('Please fill in all fields', 'error');
|
| 448 |
+
return;
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
if (!url.startsWith('http://') && !url.startsWith('https://')) {
|
| 452 |
+
url = 'https://' + url;
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
try {
|
| 456 |
+
const response = await fetch('/api/websites', {
|
| 457 |
+
method: 'POST',
|
| 458 |
+
headers: { 'Content-Type': 'application/json' },
|
| 459 |
+
body: JSON.stringify({ name, url })
|
| 460 |
+
});
|
| 461 |
+
|
| 462 |
+
if (response.ok) {
|
| 463 |
+
showToast('Website added successfully!', 'success');
|
| 464 |
+
nameInput.value = '';
|
| 465 |
+
urlInput.value = '';
|
| 466 |
+
loadWebsites();
|
| 467 |
+
} else {
|
| 468 |
+
showToast('Failed to add website', 'error');
|
| 469 |
+
}
|
| 470 |
+
} catch (error) {
|
| 471 |
+
showToast('Error adding website', 'error');
|
| 472 |
+
}
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
async function deleteWebsite(id) {
|
| 476 |
+
if (!confirm('Are you sure you want to remove this website?')) return;
|
| 477 |
+
|
| 478 |
+
try {
|
| 479 |
+
const response = await fetch(`/api/websites/${id}`, {
|
| 480 |
+
method: 'DELETE'
|
| 481 |
+
});
|
| 482 |
+
|
| 483 |
+
if (response.ok) {
|
| 484 |
+
showToast('Website removed successfully!', 'success');
|
| 485 |
+
loadWebsites();
|
| 486 |
+
} else {
|
| 487 |
+
showToast('Failed to remove website', 'error');
|
| 488 |
+
}
|
| 489 |
+
} catch (error) {
|
| 490 |
+
showToast('Error removing website', 'error');
|
| 491 |
+
}
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
async function checkAllNow() {
|
| 495 |
+
const btn = document.querySelector('.btn-success');
|
| 496 |
+
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Checking...';
|
| 497 |
+
btn.disabled = true;
|
| 498 |
+
|
| 499 |
+
try {
|
| 500 |
+
const response = await fetch('/api/check-now', {
|
| 501 |
+
method: 'POST'
|
| 502 |
+
});
|
| 503 |
+
|
| 504 |
+
if (response.ok) {
|
| 505 |
+
showToast('All websites checked!', 'success');
|
| 506 |
+
loadWebsites();
|
| 507 |
+
} else {
|
| 508 |
+
showToast('Failed to check websites', 'error');
|
| 509 |
+
}
|
| 510 |
+
} catch (error) {
|
| 511 |
+
showToast('Error checking websites', 'error');
|
| 512 |
+
} finally {
|
| 513 |
+
btn.innerHTML = '<i class="fas fa-sync-alt"></i> Check All Now';
|
| 514 |
+
btn.disabled = false;
|
| 515 |
+
}
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
function showToast(message, type) {
|
| 519 |
+
const container = document.getElementById('toastContainer');
|
| 520 |
+
const toast = document.createElement('div');
|
| 521 |
+
toast.className = `toast ${type}`;
|
| 522 |
+
toast.innerHTML = `<i class="fas fa-${type === 'success' ? 'check' : 'exclamation-triangle'}"></i> ${message}`;
|
| 523 |
+
container.appendChild(toast);
|
| 524 |
+
|
| 525 |
+
setTimeout(() => {
|
| 526 |
+
toast.remove();
|
| 527 |
+
}, 3000);
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
function escapeHtml(text) {
|
| 531 |
+
const div = document.createElement('div');
|
| 532 |
+
div.textContent = text;
|
| 533 |
+
return div.innerHTML;
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
function formatTime(time) {
|
| 537 |
+
if (time === 'Never') return 'Never';
|
| 538 |
+
const date = new Date(time);
|
| 539 |
+
const now = new Date();
|
| 540 |
+
const diff = Math.floor((now - date) / 1000);
|
| 541 |
+
|
| 542 |
+
if (diff < 60) return 'Just now';
|
| 543 |
+
if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
|
| 544 |
+
if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
|
| 545 |
+
return `${Math.floor(diff / 86400)}d ago`;
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
document.getElementById('siteUrl').addEventListener('keypress', function(e) {
|
| 549 |
+
if (e.key === 'Enter') addWebsite();
|
| 550 |
+
});
|
| 551 |
+
</script>
|
| 552 |
+
</body>
|
| 553 |
+
</html>
|
| 554 |
+
"""
|
| 555 |
+
|
| 556 |
+
@app.route('/')
|
| 557 |
+
def index():
|
| 558 |
+
return render_template_string(HTML_TEMPLATE)
|
| 559 |
+
|
| 560 |
+
@app.route('/api/websites')
|
| 561 |
+
def get_websites():
|
| 562 |
+
return jsonify(load_websites())
|
| 563 |
+
|
| 564 |
+
@app.route('/api/websites', methods=['POST'])
|
| 565 |
+
def add_website():
|
| 566 |
+
data = request.json
|
| 567 |
+
websites = load_websites()
|
| 568 |
+
|
| 569 |
+
new_id = max([w['id'] for w in websites], default=0) + 1
|
| 570 |
+
|
| 571 |
+
new_website = {
|
| 572 |
+
'id': new_id,
|
| 573 |
+
'name': data.get('name', 'Unnamed'),
|
| 574 |
+
'url': data['url'],
|
| 575 |
+
'status': 'unknown',
|
| 576 |
+
'uptime': 100,
|
| 577 |
+
'response_time': 0,
|
| 578 |
+
'last_checked': 'Never'
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
websites.append(new_website)
|
| 582 |
+
save_websites(websites)
|
| 583 |
+
check_all_websites()
|
| 584 |
+
|
| 585 |
+
return jsonify(new_website), 201
|
| 586 |
+
|
| 587 |
+
@app.route('/api/websites/<int:website_id>', methods=['DELETE'])
|
| 588 |
+
def delete_website(website_id):
|
| 589 |
+
websites = load_websites()
|
| 590 |
+
websites = [w for w in websites if w['id'] != website_id]
|
| 591 |
+
save_websites(websites)
|
| 592 |
+
|
| 593 |
+
history = load_history()
|
| 594 |
+
if str(website_id) in history:
|
| 595 |
+
del history[str(website_id)]
|
| 596 |
+
save_history(history)
|
| 597 |
+
|
| 598 |
+
return jsonify({'message': 'Website deleted'})
|
| 599 |
+
|
| 600 |
+
@app.route('/api/check-now', methods=['POST'])
|
| 601 |
+
def check_now():
|
| 602 |
+
check_all_websites()
|
| 603 |
+
return jsonify({'message': 'Check completed', 'websites': load_websites()})
|
| 604 |
+
|
| 605 |
+
if __name__ == '__main__':
|
| 606 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|