feat: add admin panel with multi-chain support and notification system

- Add full admin REST API: auth, API keys, chains, chain tokens, wallets,
  orders, RPC nodes, settings, dashboard stats, notifications
- Add JWT-based admin authentication and API key auth middleware
- Add multi-chain listeners: BSC, ETH, Polygon, Plasma, EVM WebSocket
- Add RPC node health check job with automatic failover
- Add Telegram notification channel for order events
- Add installer for first-run database initialization
- Add new DB models: admin_user, api_key, chain, chain_token,
  rpc_node, settings, notification_channel
- Add order statistics aggregation (daily/monthly/by-chain)
- Serve built admin SPA from embedded www/ assets
- Add comprehensive test coverage for all new features
This commit is contained in:
line-6000
2026-04-22 02:28:31 +08:00
parent 097c716714
commit 6bb47d4b00
247 changed files with 9933 additions and 1279 deletions
+97
View File
@@ -0,0 +1,97 @@
package task
import (
"net"
"net/url"
"strings"
"sync"
"time"
"github.com/assimon/luuu/model/data"
"github.com/assimon/luuu/model/mdb"
"github.com/assimon/luuu/util/log"
)
const rpcProbeTimeout = 5 * time.Second
// RpcHealthJob periodically probes every enabled rpc_nodes row and
// writes status/last_latency_ms. Results drive SelectRpcNode weighted
// picking at runtime.
type RpcHealthJob struct{}
var gRpcHealthJobLock sync.Mutex
func (r RpcHealthJob) Run() {
gRpcHealthJobLock.Lock()
defer gRpcHealthJobLock.Unlock()
nodes, err := data.ListRpcNodes("")
if err != nil {
log.Sugar.Errorf("[rpc-health] list nodes err=%v", err)
return
}
var wg sync.WaitGroup
for i := range nodes {
if !nodes[i].Enabled {
continue
}
wg.Add(1)
go func(n mdb.RpcNode) {
defer wg.Done()
status, latency := ProbeNode(n.Url)
if err := data.UpdateRpcNodeHealth(n.ID, status, latency); err != nil {
log.Sugar.Warnf("[rpc-health] update node %d err=%v", n.ID, err)
}
}(nodes[i])
}
wg.Wait()
}
// ProbeNode does a TCP dial to the RPC URL and returns (status, latencyMs).
// Exported so the admin controller can reuse it without duplicating logic.
func ProbeNode(rawURL string) (string, int) {
addr, err := ParseAddress(rawURL)
if err != nil {
return mdb.RpcNodeStatusDown, -1
}
dur, err := MeasureTCPDial(addr, rpcProbeTimeout)
if err != nil {
return mdb.RpcNodeStatusDown, -1
}
return mdb.RpcNodeStatusOk, int(dur.Milliseconds())
}
func ParseAddress(raw string) (string, error) {
if !strings.Contains(raw, "://") {
raw = "tcp://" + raw
}
u, err := url.Parse(raw)
if err != nil {
return "", err
}
host := u.Hostname()
port := u.Port()
if port == "" {
switch u.Scheme {
case "https", "wss":
port = "443"
default:
port = "80"
}
}
return host + ":" + port, nil
}
func MeasureTCPDial(addr string, timeout time.Duration) (time.Duration, error) {
start := time.Now()
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return 0, err
}
defer conn.Close()
return time.Since(start), nil
}