-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (132 loc) · 3.18 KB
/
main.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"time"
bolt "go.etcd.io/bbolt"
)
var (
port int
dbPath string
db *bolt.DB
)
func init() {
flag.IntVar(&port, "port", 9092, "HTTP server port number")
flag.StringVar(&dbPath, "db", "my.db", "BoltDB file path")
}
func main() {
flag.Parse()
// Open Bolt database
var err error
o := bolt.DefaultOptions
o.Timeout = 5 * time.Second
db, err = bolt.Open(dbPath, 0600, o)
if err != nil {
log.Fatal(fmt.Errorf("fail to open bolt db: %w", err))
}
defer db.Close()
// Initialize router and routes
mux := http.NewServeMux()
mux.HandleFunc("/buckets/", listBucketKeys)
// Start server
addr := fmt.Sprintf("127.0.0.1:%d", port)
log.Printf("Server started on %s", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}
type bucketDetail struct {
Key string
Value string
IsNested bool
}
func listTopBuckets() ([]bucketDetail, error) {
var bucketDetails []bucketDetail
err := db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
bucketDetails = append(bucketDetails, bucketDetail{
Key: toString(name),
Value: "",
IsNested: true,
})
return nil
})
})
return bucketDetails, err
}
func listNestedBuckets(bucketNames []string) ([]bucketDetail, error) {
var bucketDetails []bucketDetail
err := db.View(func(tx *bolt.Tx) error {
var bkt = tx.Bucket([]byte(bucketNames[0]))
for _, bucketName := range bucketNames[1:] {
bkt = bkt.Bucket([]byte(bucketName))
if bkt == nil {
return fmt.Errorf("bucket '%s' not found", bucketName)
}
}
err := bkt.ForEach(func(k, v []byte) error {
// nested buckets
isNested := false
if v == nil {
isNested = true
}
bucketDetails = append(bucketDetails, bucketDetail{
Key: toString(k),
Value: toString(v),
IsNested: isNested,
})
return nil
})
return err
})
return bucketDetails, err
}
//go:embed templates/*
var templates embed.FS
// listBucketKeys handles the /buckets/... endpoint
func listBucketKeys(w http.ResponseWriter, r *http.Request) {
// Extract bucket name from URL path
path := strings.TrimPrefix(r.URL.Path, "/buckets/")
bucketNames := strings.Split(path, "/")
var bucketDetails []bucketDetail
var err error
// No bucket specified, list all root-level buckets
if path == "" {
bucketDetails, err = listTopBuckets()
} else {
bucketDetails, err = listNestedBuckets(bucketNames)
}
// Render HTML template
type templateData struct {
Error error
BucketPath string
BucketDetails []bucketDetail
}
data := templateData{
Error: err,
BucketPath: path,
BucketDetails: bucketDetails,
}
tmpl, err := template.ParseFS(templates, "templates/buckets.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// toString defines how to convert bucket key/value to string
func toString(buf []byte) string {
if buf == nil {
return ""
}
return string(buf)
}