-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (67 loc) · 2.27 KB
/
Copy pathmain.go
File metadata and controls
83 lines (67 loc) · 2.27 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
package main
import (
"embed"
"fmt"
"io/fs"
"log/slog"
"net/http"
"os"
"github.com/evehash/bitpeek/internal/config"
"github.com/evehash/bitpeek/internal/electrs"
"github.com/evehash/bitpeek/internal/handler"
"github.com/evehash/bitpeek/internal/rpc"
"github.com/evehash/bitpeek/internal/templates"
)
//go:embed static
var staticFS embed.FS
//go:embed templates
var templateFS embed.FS
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
slog.SetDefault(logger)
cfg, err := config.Load()
if err != nil {
slog.Error("failed to load config", "error", err)
os.Exit(1)
}
tmpl, err := templates.Parse(templateFS)
if err != nil {
slog.Error("failed to parse templates", "error", err)
os.Exit(1)
}
btc := rpc.NewClient(cfg.BitcoinURL(), cfg.Bitcoin.User, cfg.Bitcoin.Pass, cfg.Bitcoin.TimeoutMs)
el := electrs.NewClient(cfg.Electrs.Host, cfg.Electrs.Port, cfg.Electrs.TimeoutMs)
h := handler.New(cfg, btc, el, tmpl)
mux := http.NewServeMux()
// Static files
staticSub, _ := fs.Sub(staticFS, "static")
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticSub))))
// Pages
mux.HandleFunc("GET /{$}", h.Dashboard)
mux.HandleFunc("GET /blocks", h.Blocks)
mux.HandleFunc("GET /block/{heightOrHash}", h.Block)
mux.HandleFunc("GET /tx/{txid}", h.Tx)
mux.HandleFunc("GET /address/{addr}", h.Address)
mux.HandleFunc("GET /mempool", h.Mempool)
mux.HandleFunc("GET /health", h.Health)
mux.HandleFunc("GET /search", h.Search)
// Debug (only if dev_mode is enabled)
if cfg.App.DevMode {
mux.HandleFunc("GET /debug", h.Debug)
mux.HandleFunc("POST /debug/exec", h.DebugExec)
slog.Warn("dev mode enabled — debug console available at /debug")
}
// htmx partials
mux.HandleFunc("GET /partials/status", h.PartialStatus)
mux.HandleFunc("GET /partials/block/{hash}/txs", h.PartialBlockTxs)
mux.HandleFunc("GET /partials/address/{addr}/txs", h.PartialAddressTxs)
mux.HandleFunc("GET /partials/mempool-stats", h.PartialMempoolStats)
mux.HandleFunc("GET /partials/blocks", h.PartialBlocks)
addr := cfg.ListenAddr()
fmt.Printf("bitpeek running at http://%s\n", addr)
slog.Info("server starting", "addr", addr)
if err := http.ListenAndServe(addr, mux); err != nil {
slog.Error("server failed", "error", err)
os.Exit(1)
}
}