Implement AS and prefix detail pages
- Implement handleASDetail() and handlePrefixDetail() HTML handlers - Create AS detail HTML template with prefix listings - Create prefix detail HTML template with route information - Add timeSince template function for human-readable durations - Update templates.go to include new templates - Server-side rendered pages as requested (no client-side API calls)
This commit is contained in:
parent
27ae80ea2e
commit
aeeb5e7d7d
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@ -458,16 +459,160 @@ func (s *Server) handlePrefixDetailJSON() http.HandlerFunc {
|
||||
|
||||
// handleASDetail returns a handler that serves the AS detail HTML page
|
||||
func (s *Server) handleASDetail() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
// TODO: Implement AS detail HTML page
|
||||
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
asnStr := chi.URLParam(r, "asn")
|
||||
asn, err := strconv.Atoi(asnStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid ASN", http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
asInfo, prefixes, err := s.db.GetASDetails(asn)
|
||||
if err != nil {
|
||||
if errors.Is(err, database.ErrNoRoute) {
|
||||
http.Error(w, "AS not found", http.StatusNotFound)
|
||||
} else {
|
||||
s.logger.Error("Failed to get AS details", "error", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Group prefixes by IP version
|
||||
const ipVersionV4 = 4
|
||||
var ipv4Prefixes, ipv6Prefixes []database.LiveRoute
|
||||
for _, p := range prefixes {
|
||||
if p.IPVersion == ipVersionV4 {
|
||||
ipv4Prefixes = append(ipv4Prefixes, p)
|
||||
} else {
|
||||
ipv6Prefixes = append(ipv6Prefixes, p)
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare template data
|
||||
data := struct {
|
||||
ASN *database.ASN
|
||||
IPv4Prefixes []database.LiveRoute
|
||||
IPv6Prefixes []database.LiveRoute
|
||||
TotalCount int
|
||||
IPv4Count int
|
||||
IPv6Count int
|
||||
}{
|
||||
ASN: asInfo,
|
||||
IPv4Prefixes: ipv4Prefixes,
|
||||
IPv6Prefixes: ipv6Prefixes,
|
||||
TotalCount: len(prefixes),
|
||||
IPv4Count: len(ipv4Prefixes),
|
||||
IPv6Count: len(ipv6Prefixes),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
tmpl := templates.ASDetailTemplate()
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
s.logger.Error("Failed to render AS detail template", "error", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handlePrefixDetail returns a handler that serves the prefix detail HTML page
|
||||
func (s *Server) handlePrefixDetail() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
// TODO: Implement prefix detail HTML page
|
||||
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
prefix := chi.URLParam(r, "prefix")
|
||||
if prefix == "" {
|
||||
http.Error(w, "Prefix parameter is required", http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
routes, err := s.db.GetPrefixDetails(prefix)
|
||||
if err != nil {
|
||||
if errors.Is(err, database.ErrNoRoute) {
|
||||
http.Error(w, "Prefix not found", http.StatusNotFound)
|
||||
} else {
|
||||
s.logger.Error("Failed to get prefix details", "error", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Group by origin AS and collect unique AS info
|
||||
type ASNInfo struct {
|
||||
Number int
|
||||
Handle string
|
||||
Description string
|
||||
PeerCount int
|
||||
}
|
||||
originMap := make(map[int]*ASNInfo)
|
||||
for _, route := range routes {
|
||||
if _, exists := originMap[route.OriginASN]; !exists {
|
||||
// Get AS info from database
|
||||
asInfo, _, _ := s.db.GetASDetails(route.OriginASN)
|
||||
handle := ""
|
||||
description := ""
|
||||
if asInfo != nil {
|
||||
handle = asInfo.Handle
|
||||
description = asInfo.Description
|
||||
}
|
||||
originMap[route.OriginASN] = &ASNInfo{
|
||||
Number: route.OriginASN,
|
||||
Handle: handle,
|
||||
Description: description,
|
||||
PeerCount: 0,
|
||||
}
|
||||
}
|
||||
originMap[route.OriginASN].PeerCount++
|
||||
}
|
||||
|
||||
// Get the first route to extract some common info
|
||||
var maskLength, ipVersion int
|
||||
if len(routes) > 0 {
|
||||
// Parse CIDR to get mask length and IP version
|
||||
_, ipNet, err := net.ParseCIDR(prefix)
|
||||
if err == nil {
|
||||
ones, _ := ipNet.Mask.Size()
|
||||
maskLength = ones
|
||||
if ipNet.IP.To4() != nil {
|
||||
ipVersion = 4
|
||||
} else {
|
||||
ipVersion = 6
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert origin map to sorted slice
|
||||
var origins []*ASNInfo
|
||||
for _, origin := range originMap {
|
||||
origins = append(origins, origin)
|
||||
}
|
||||
|
||||
// Prepare template data
|
||||
data := struct {
|
||||
Prefix string
|
||||
MaskLength int
|
||||
IPVersion int
|
||||
Routes []database.LiveRoute
|
||||
Origins []*ASNInfo
|
||||
PeerCount int
|
||||
OriginCount int
|
||||
}{
|
||||
Prefix: prefix,
|
||||
MaskLength: maskLength,
|
||||
IPVersion: ipVersion,
|
||||
Routes: routes,
|
||||
Origins: origins,
|
||||
PeerCount: len(routes),
|
||||
OriginCount: len(originMap),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
tmpl := templates.PrefixDetailTemplate()
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
s.logger.Error("Failed to render prefix detail template", "error", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
228
internal/templates/as_detail.html
Normal file
228
internal/templates/as_detail.html
Normal file
@ -0,0 +1,228 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>AS{{.ASN.Number}} - {{.ASN.Handle}} - RouteWatch</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background: #f5f5f5;
|
||||
color: #333;
|
||||
}
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
background: white;
|
||||
padding: 30px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
h1 {
|
||||
margin: 0 0 10px 0;
|
||||
color: #2c3e50;
|
||||
}
|
||||
.subtitle {
|
||||
color: #7f8c8d;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.info-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
||||
gap: 20px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.info-card {
|
||||
background: #f8f9fa;
|
||||
padding: 20px;
|
||||
border-radius: 6px;
|
||||
border-left: 4px solid #3498db;
|
||||
}
|
||||
.info-label {
|
||||
font-size: 14px;
|
||||
color: #7f8c8d;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.info-value {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
color: #2c3e50;
|
||||
}
|
||||
.prefix-section {
|
||||
margin-top: 30px;
|
||||
}
|
||||
.prefix-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.prefix-header h2 {
|
||||
margin: 0;
|
||||
color: #2c3e50;
|
||||
}
|
||||
.prefix-count {
|
||||
background: #e74c3c;
|
||||
color: white;
|
||||
padding: 5px 12px;
|
||||
border-radius: 20px;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.prefix-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
background: white;
|
||||
border: 1px solid #e0e0e0;
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.prefix-table th {
|
||||
background: #34495e;
|
||||
color: white;
|
||||
padding: 12px;
|
||||
text-align: left;
|
||||
font-weight: 600;
|
||||
}
|
||||
.prefix-table td {
|
||||
padding: 12px;
|
||||
border-bottom: 1px solid #e0e0e0;
|
||||
}
|
||||
.prefix-table tr:hover {
|
||||
background: #f8f9fa;
|
||||
}
|
||||
.prefix-table tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
.prefix-link {
|
||||
color: #3498db;
|
||||
text-decoration: none;
|
||||
font-family: monospace;
|
||||
}
|
||||
.prefix-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
.age {
|
||||
color: #7f8c8d;
|
||||
font-size: 14px;
|
||||
}
|
||||
.nav-link {
|
||||
display: inline-block;
|
||||
margin-bottom: 20px;
|
||||
color: #3498db;
|
||||
text-decoration: none;
|
||||
}
|
||||
.nav-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 40px;
|
||||
color: #7f8c8d;
|
||||
}
|
||||
@media (max-width: 768px) {
|
||||
.container {
|
||||
padding: 20px;
|
||||
}
|
||||
.info-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<a href="/status" class="nav-link">← Back to Status</a>
|
||||
|
||||
<h1>AS{{.ASN.Number}}{{if .ASN.Handle}} - {{.ASN.Handle}}{{end}}</h1>
|
||||
{{if .ASN.Description}}
|
||||
<p class="subtitle">{{.ASN.Description}}</p>
|
||||
{{end}}
|
||||
|
||||
<div class="info-grid">
|
||||
<div class="info-card">
|
||||
<div class="info-label">Total Prefixes</div>
|
||||
<div class="info-value">{{.TotalCount}}</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">IPv4 Prefixes</div>
|
||||
<div class="info-value">{{.IPv4Count}}</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">IPv6 Prefixes</div>
|
||||
<div class="info-value">{{.IPv6Count}}</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">First Seen</div>
|
||||
<div class="info-value">{{.ASN.FirstSeen.Format "2006-01-02"}}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{if .IPv4Prefixes}}
|
||||
<div class="prefix-section">
|
||||
<div class="prefix-header">
|
||||
<h2>IPv4 Prefixes</h2>
|
||||
<span class="prefix-count">{{.IPv4Count}}</span>
|
||||
</div>
|
||||
<table class="prefix-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Prefix</th>
|
||||
<th>Mask Length</th>
|
||||
<th>Last Updated</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .IPv4Prefixes}}
|
||||
<tr>
|
||||
<td><a href="/prefix/{{.Prefix}}" class="prefix-link">{{.Prefix}}</a></td>
|
||||
<td>/{{.MaskLength}}</td>
|
||||
<td>{{.LastUpdated.Format "2006-01-02 15:04:05"}}</td>
|
||||
<td class="age">{{.LastUpdated | timeSince}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if .IPv6Prefixes}}
|
||||
<div class="prefix-section">
|
||||
<div class="prefix-header">
|
||||
<h2>IPv6 Prefixes</h2>
|
||||
<span class="prefix-count">{{.IPv6Count}}</span>
|
||||
</div>
|
||||
<table class="prefix-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Prefix</th>
|
||||
<th>Mask Length</th>
|
||||
<th>Last Updated</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .IPv6Prefixes}}
|
||||
<tr>
|
||||
<td><a href="/prefix/{{.Prefix}}" class="prefix-link">{{.Prefix}}</a></td>
|
||||
<td>/{{.MaskLength}}</td>
|
||||
<td>{{.LastUpdated.Format "2006-01-02 15:04:05"}}</td>
|
||||
<td class="age">{{.LastUpdated | timeSince}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if eq .TotalCount 0}}
|
||||
<div class="empty-state">
|
||||
<p>No prefixes announced by this AS</p>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
253
internal/templates/prefix_detail.html
Normal file
253
internal/templates/prefix_detail.html
Normal file
@ -0,0 +1,253 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Prefix}} - RouteWatch</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background: #f5f5f5;
|
||||
color: #333;
|
||||
}
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
background: white;
|
||||
padding: 30px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
h1 {
|
||||
margin: 0 0 10px 0;
|
||||
color: #2c3e50;
|
||||
font-family: monospace;
|
||||
font-size: 28px;
|
||||
}
|
||||
.subtitle {
|
||||
color: #7f8c8d;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.info-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: 20px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.info-card {
|
||||
background: #f8f9fa;
|
||||
padding: 20px;
|
||||
border-radius: 6px;
|
||||
border-left: 4px solid #3498db;
|
||||
}
|
||||
.info-label {
|
||||
font-size: 14px;
|
||||
color: #7f8c8d;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.info-value {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
color: #2c3e50;
|
||||
}
|
||||
.routes-section {
|
||||
margin-top: 30px;
|
||||
}
|
||||
.routes-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.routes-header h2 {
|
||||
margin: 0;
|
||||
color: #2c3e50;
|
||||
}
|
||||
.route-count {
|
||||
background: #e74c3c;
|
||||
color: white;
|
||||
padding: 5px 12px;
|
||||
border-radius: 20px;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.route-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
background: white;
|
||||
border: 1px solid #e0e0e0;
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.route-table th {
|
||||
background: #34495e;
|
||||
color: white;
|
||||
padding: 12px;
|
||||
text-align: left;
|
||||
font-weight: 600;
|
||||
}
|
||||
.route-table td {
|
||||
padding: 12px;
|
||||
border-bottom: 1px solid #e0e0e0;
|
||||
}
|
||||
.route-table tr:hover {
|
||||
background: #f8f9fa;
|
||||
}
|
||||
.route-table tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
.as-link {
|
||||
color: #3498db;
|
||||
text-decoration: none;
|
||||
}
|
||||
.as-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
.peer-ip {
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
color: #555;
|
||||
}
|
||||
.as-path {
|
||||
font-family: monospace;
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
max-width: 300px;
|
||||
overflow-x: auto;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.age {
|
||||
color: #7f8c8d;
|
||||
font-size: 14px;
|
||||
}
|
||||
.nav-link {
|
||||
display: inline-block;
|
||||
margin-bottom: 20px;
|
||||
color: #3498db;
|
||||
text-decoration: none;
|
||||
}
|
||||
.nav-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
.origins-section {
|
||||
margin-top: 30px;
|
||||
background: #f8f9fa;
|
||||
padding: 20px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
.origins-section h3 {
|
||||
margin-top: 0;
|
||||
color: #2c3e50;
|
||||
}
|
||||
.origin-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 10px;
|
||||
}
|
||||
.origin-item {
|
||||
background: white;
|
||||
padding: 10px 15px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #e0e0e0;
|
||||
}
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 40px;
|
||||
color: #7f8c8d;
|
||||
}
|
||||
@media (max-width: 768px) {
|
||||
.container {
|
||||
padding: 20px;
|
||||
}
|
||||
.info-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.route-table {
|
||||
font-size: 14px;
|
||||
}
|
||||
.as-path {
|
||||
max-width: 150px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<a href="/status" class="nav-link">← Back to Status</a>
|
||||
|
||||
<h1>{{.Prefix}}</h1>
|
||||
<p class="subtitle">IPv{{.IPVersion}} Prefix{{if .MaskLength}} • /{{.MaskLength}}{{end}}</p>
|
||||
|
||||
<div class="info-grid">
|
||||
<div class="info-card">
|
||||
<div class="info-label">Seen from Peers</div>
|
||||
<div class="info-value">{{.PeerCount}}</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">Origin ASNs</div>
|
||||
<div class="info-value">{{.OriginCount}}</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">IP Version</div>
|
||||
<div class="info-value">IPv{{.IPVersion}}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{if .Origins}}
|
||||
<div class="origins-section">
|
||||
<h3>Origin ASNs</h3>
|
||||
<div class="origin-list">
|
||||
{{range .Origins}}
|
||||
<div class="origin-item">
|
||||
<a href="/as/{{.Number}}" class="as-link">AS{{.Number}}</a>
|
||||
{{if .Handle}} ({{.Handle}}){{end}}
|
||||
<span style="color: #7f8c8d; margin-left: 10px;">{{.PeerCount}} peer{{if ne .PeerCount 1}}s{{end}}</span>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if .Routes}}
|
||||
<div class="routes-section">
|
||||
<div class="routes-header">
|
||||
<h2>Route Details</h2>
|
||||
<span class="route-count">{{.PeerCount}} route{{if ne .PeerCount 1}}s{{end}}</span>
|
||||
</div>
|
||||
<table class="route-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Origin AS</th>
|
||||
<th>Peer IP</th>
|
||||
<th>AS Path</th>
|
||||
<th>Next Hop</th>
|
||||
<th>Last Updated</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Routes}}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="/as/{{.OriginASN}}" class="as-link">AS{{.OriginASN}}</a>
|
||||
</td>
|
||||
<td class="peer-ip">{{.PeerIP}}</td>
|
||||
<td class="as-path">{{range $i, $as := .ASPath}}{{if $i}} → {{end}}{{$as}}{{end}}</td>
|
||||
<td class="peer-ip">{{.NextHop}}</td>
|
||||
<td>{{.LastUpdated.Format "2006-01-02 15:04:05"}}</td>
|
||||
<td class="age">{{.LastUpdated | timeSince}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{else}}
|
||||
<div class="empty-state">
|
||||
<p>No routes found for this prefix</p>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -5,14 +5,23 @@ import (
|
||||
_ "embed"
|
||||
"html/template"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed status.html
|
||||
var statusHTML string
|
||||
|
||||
//go:embed as_detail.html
|
||||
var asDetailHTML string
|
||||
|
||||
//go:embed prefix_detail.html
|
||||
var prefixDetailHTML string
|
||||
|
||||
// Templates contains all parsed templates
|
||||
type Templates struct {
|
||||
Status *template.Template
|
||||
Status *template.Template
|
||||
ASDetail *template.Template
|
||||
PrefixDetail *template.Template
|
||||
}
|
||||
|
||||
var (
|
||||
@ -22,17 +31,72 @@ var (
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
const (
|
||||
hoursPerDay = 24
|
||||
daysPerMonth = 30
|
||||
)
|
||||
|
||||
// timeSince returns a human-readable duration since the given time
|
||||
func timeSince(t time.Time) string {
|
||||
duration := time.Since(t)
|
||||
if duration < time.Minute {
|
||||
return "just now"
|
||||
}
|
||||
if duration < time.Hour {
|
||||
minutes := int(duration.Minutes())
|
||||
if minutes == 1 {
|
||||
return "1 minute ago"
|
||||
}
|
||||
|
||||
return duration.Truncate(time.Minute).String() + " ago"
|
||||
}
|
||||
if duration < hoursPerDay*time.Hour {
|
||||
hours := int(duration.Hours())
|
||||
if hours == 1 {
|
||||
return "1 hour ago"
|
||||
}
|
||||
|
||||
return duration.Truncate(time.Hour).String() + " ago"
|
||||
}
|
||||
days := int(duration.Hours() / hoursPerDay)
|
||||
if days == 1 {
|
||||
return "1 day ago"
|
||||
}
|
||||
if days < daysPerMonth {
|
||||
return duration.Truncate(hoursPerDay*time.Hour).String() + " ago"
|
||||
}
|
||||
|
||||
return t.Format("2006-01-02")
|
||||
}
|
||||
|
||||
// initTemplates parses all embedded templates
|
||||
func initTemplates() {
|
||||
var err error
|
||||
|
||||
defaultTemplates = &Templates{}
|
||||
|
||||
// Create common template functions
|
||||
funcs := template.FuncMap{
|
||||
"timeSince": timeSince,
|
||||
}
|
||||
|
||||
// Parse status template
|
||||
defaultTemplates.Status, err = template.New("status").Parse(statusHTML)
|
||||
if err != nil {
|
||||
panic("failed to parse status template: " + err.Error())
|
||||
}
|
||||
|
||||
// Parse AS detail template
|
||||
defaultTemplates.ASDetail, err = template.New("asDetail").Funcs(funcs).Parse(asDetailHTML)
|
||||
if err != nil {
|
||||
panic("failed to parse AS detail template: " + err.Error())
|
||||
}
|
||||
|
||||
// Parse prefix detail template
|
||||
defaultTemplates.PrefixDetail, err = template.New("prefixDetail").Funcs(funcs).Parse(prefixDetailHTML)
|
||||
if err != nil {
|
||||
panic("failed to parse prefix detail template: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the singleton Templates instance
|
||||
@ -46,3 +110,13 @@ func Get() *Templates {
|
||||
func StatusTemplate() *template.Template {
|
||||
return Get().Status
|
||||
}
|
||||
|
||||
// ASDetailTemplate returns the parsed AS detail template
|
||||
func ASDetailTemplate() *template.Template {
|
||||
return Get().ASDetail
|
||||
}
|
||||
|
||||
// PrefixDetailTemplate returns the parsed prefix detail template
|
||||
func PrefixDetailTemplate() *template.Template {
|
||||
return Get().PrefixDetail
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user