Switch to Django with visitor tracking and server info footnote
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful
This commit is contained in:
0
core/__init__.py
Normal file
0
core/__init__.py
Normal file
10
core/admin.py
Normal file
10
core/admin.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from .models import Visit
|
||||
|
||||
|
||||
@admin.register(Visit)
|
||||
class VisitAdmin(admin.ModelAdmin):
|
||||
list_display = ['ip', 'timestamp']
|
||||
list_filter = ['timestamp']
|
||||
readonly_fields = ['ip', 'timestamp']
|
||||
ordering = ['-timestamp']
|
||||
5
core/apps.py
Normal file
5
core/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core'
|
||||
12
core/models.py
Normal file
12
core/models.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Visit(models.Model):
|
||||
ip = models.GenericIPAddressField()
|
||||
timestamp = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-timestamp']
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.ip} @ {self.timestamp}"
|
||||
87
core/templates/core/index.html
Normal file
87
core/templates/core/index.html
Normal file
@@ -0,0 +1,87 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>k-boris.tech</title>
|
||||
<style>
|
||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||
background: #0f172a;
|
||||
color: #e2e8f0;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
main {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
h1 { font-size: 2.5rem; letter-spacing: -0.05em; }
|
||||
.sub { color: #94a3b8; margin-top: 0.5rem; }
|
||||
footer {
|
||||
background: #0a0f1e;
|
||||
border-top: 1px solid #1e293b;
|
||||
padding: 1.5rem 2rem;
|
||||
font-size: 0.78rem;
|
||||
color: #64748b;
|
||||
}
|
||||
.grid {
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: 1.5rem;
|
||||
}
|
||||
.section h3 {
|
||||
color: #94a3b8;
|
||||
font-size: 0.68rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.1em;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
.section p { line-height: 2; }
|
||||
.val { color: #38bdf8; font-variant-numeric: tabular-nums; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div style="text-align:center">
|
||||
<h1>k-boris.tech</h1>
|
||||
<p class="sub">Work in progress.</p>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<div class="grid">
|
||||
|
||||
<div class="section">
|
||||
<h3>Your Visit</h3>
|
||||
<p>IP address: <span class="val">{{ visitor_ip }}</span></p>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<h3>Site Traffic</h3>
|
||||
<p>
|
||||
Today: <span class="val">{{ visits_today }}</span><br>
|
||||
This week: <span class="val">{{ visits_week }}</span><br>
|
||||
This month: <span class="val">{{ visits_month }}</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<h3>Server</h3>
|
||||
<p>
|
||||
Uptime: <span class="val">{{ uptime }}</span><br>
|
||||
Load (1/5/15m): <span class="val">{{ load }}</span><br>
|
||||
Memory: <span class="val">{{ mem_used }} MB used / {{ mem_total }} MB total</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
6
core/urls.py
Normal file
6
core/urls.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
]
|
||||
61
core/views.py
Normal file
61
core/views.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from django.shortcuts import render
|
||||
from .models import Visit
|
||||
|
||||
|
||||
def _get_server_info():
|
||||
try:
|
||||
with open('/proc/uptime') as f:
|
||||
total_seconds = int(float(f.read().split()[0]))
|
||||
days, rem = divmod(total_seconds, 86400)
|
||||
hours, mins = divmod(rem, 3600)
|
||||
uptime = f"{days}d {hours}h {mins // 60}m"
|
||||
except Exception:
|
||||
uptime = 'N/A'
|
||||
|
||||
try:
|
||||
with open('/proc/loadavg') as f:
|
||||
parts = f.read().split()
|
||||
load = f"{parts[0]} {parts[1]} {parts[2]}"
|
||||
except Exception:
|
||||
load = 'N/A'
|
||||
|
||||
try:
|
||||
meminfo = {}
|
||||
with open('/proc/meminfo') as f:
|
||||
for line in f:
|
||||
key, val = line.split(':')
|
||||
meminfo[key.strip()] = int(val.split()[0])
|
||||
mem_total = meminfo['MemTotal'] // 1024
|
||||
mem_free = meminfo['MemAvailable'] // 1024
|
||||
mem_used = mem_total - mem_free
|
||||
except Exception:
|
||||
mem_total = mem_free = mem_used = 0
|
||||
|
||||
return {
|
||||
'uptime': uptime,
|
||||
'load': load,
|
||||
'mem_total': mem_total,
|
||||
'mem_free': mem_free,
|
||||
'mem_used': mem_used,
|
||||
}
|
||||
|
||||
|
||||
def index(request):
|
||||
forwarded = request.META.get('HTTP_X_FORWARDED_FOR', '')
|
||||
ip = forwarded.split(',')[0].strip() if forwarded else request.META.get('REMOTE_ADDR', 'unknown')
|
||||
|
||||
Visit.objects.create(ip=ip)
|
||||
|
||||
now = timezone.now()
|
||||
start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
context = {
|
||||
'visitor_ip': ip,
|
||||
'visits_today': Visit.objects.filter(timestamp__gte=start_of_day).count(),
|
||||
'visits_week': Visit.objects.filter(timestamp__gte=now - timedelta(days=7)).count(),
|
||||
'visits_month': Visit.objects.filter(timestamp__gte=now - timedelta(days=30)).count(),
|
||||
**_get_server_info(),
|
||||
}
|
||||
return render(request, 'core/index.html', context)
|
||||
Reference in New Issue
Block a user