Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
328 lines (291 loc) · 9.03 KB
/
Copy pathmain.go
File metadata and controls
328 lines (291 loc) · 9.03 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
_ "embed"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/mjl-/bstore"
"github.com/mjl-/sherpa"
"github.com/mjl-/sherpadoc"
"github.com/mjl-/sherpaprom"
)
type Software struct {
ID string // e.g. "mox" or "someSoftware", keep them valid identifiers for working in js easily.
Created time.Time `bstore:"default now"`
Updated time.Time `bstore:"default now"`
Name string `bstore:"nonzero,unique"`
URL string
Description string
OpenSource bool
Maintained bool
License string
ProgLang string
Distribution bool // Collection of other software packages.
// Corresponds to same fields in Feature.
Server bool
Service bool // Hosted service, software not necessarily available.
Library bool
Client bool
Desktop bool
Mobile bool
Web bool
Terminal bool
}
// Feature is something software implements. Like a standard (RFC), a small part of
// a standard, or just some behaviour the software has.
type Feature struct {
ID string
Created time.Time `bstore:"default now"`
Updated time.Time `bstore:"default now"`
Title string `bstore:"nonzero,unique"`
URL string
Description string
// Corresponds to same fields in Software, whether they are applicable.
Server bool
Service bool // Hosted service, software not necessarily available.
Library bool
Client bool
Desktop bool
Mobile bool
Web bool
Terminal bool
}
// Implementation status of a feature for software.
type Status string
const (
Unknown Status = ""
NotApplicable Status = "n/a"
Never Status = "Never"
No Status = "No"
Planned Status = "Planned"
Partial Status = "Partial"
Yes Status = "Yes"
)
// Implementation status of a feature by an implementation.
type Implementation struct {
ID int64
Created time.Time `bstore:"default now" json:"-"` // Not in JSON to reduce noise.
Updated time.Time `bstore:"default now"`
SoftwareID string `bstore:"ref Software,unique SoftwareID+FeatureID"`
FeatureID string `bstore:"ref Feature"`
Status Status
Bugs bool // Has known bugs
Plugin bool // If functionality is delivered by plugin.
URL string
SinceVersion string
Notes string
}
// State is available in JS.
type State struct {
Software []Software
Features []Feature
Implementations []Implementation
}
var database *bstore.DB
//go:embed api.json
var apiJSON []byte
//go:embed index.html
var indexHTML []byte
//go:embed index.js
var indexJS []byte
func mustParseAPI(api string, buf []byte) (doc sherpadoc.Section) {
err := json.Unmarshal(buf, &doc)
if err != nil {
log.Fatalf("parsing api docs: %s", err)
}
return doc
}
func xcheckf(err error, format string, args ...any) {
if err != nil {
log.Fatalf("%s: %s", fmt.Sprintf(format, args...), err)
}
}
func main() {
log.SetFlags(0)
var listenAddr, metricsAddr string
flag.StringVar(&listenAddr, "listenaddr", "localhost:8019", "address to serve public http on")
flag.StringVar(&metricsAddr, "metricsaddr", "localhost:8020", "address to serve metrics on")
flag.Usage = func() {
log.Println("usage: implementations [flags]")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
args := flag.Args()
if len(args) != 0 {
flag.Usage()
}
var err error
database, err = bstore.Open(context.Background(), "implementations.db", nil, Software{}, Feature{}, Implementation{})
xcheckf(err, "open database")
http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
html, err := os.ReadFile("index.html")
if err != nil {
html = indexHTML
}
js, err := os.ReadFile("index.js")
if err != nil {
js = indexJS
}
out := string(html)
out = strings.ReplaceAll(out, "/* placeholder */", string(js))
h := w.Header()
h.Set("Content-Type", "text/html; charset=utf-8")
h.Set("Cache-Control", "no-cache, max-age=0")
w.Write([]byte(out))
})
apiDoc := mustParseAPI("api", apiJSON)
collector, err := sherpaprom.NewCollector("implementations", nil)
xcheckf(err, "creating sherpa prometheus collector")
sherpaOpts := sherpa.HandlerOpts{Collector: collector, AdjustFunctionNames: "none"}
apiHandler, err := sherpa.NewHandler("/api/", version, API{}, &apiDoc, &sherpaOpts)
xcheckf(err, "making api handler")
http.Handle("/api/", apiHandler)
http.HandleFunc("/implementations.json", func(w http.ResponseWriter, r *http.Request) {
state, err := API{}.State(r.Context())
if err != nil {
log.Printf("state: %v", err)
http.Error(w, "500 - internal server error - "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(state); err != nil {
log.Printf("writing json: %v", err)
}
})
http.HandleFunc("/implementations.db", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
err := database.Read(r.Context(), func(tx *bstore.Tx) error {
_, err := tx.WriteTo(w)
return err
})
if err != nil {
log.Printf("writing db: %v", err)
}
})
// Prometheus metrics served on a separate port.
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.Handler())
if metricsAddr != "" {
log.Printf("listening on %s for metrics", metricsAddr)
go func() {
log.Fatalln("serving metrics", http.ListenAndServe(metricsAddr, metricsMux))
}()
}
log.Printf("listening on %s for public http, version %v", listenAddr, version)
log.Fatalln("serving public http", http.ListenAndServe(listenAddr, nil))
}
type API struct{}
func (API) ImplementationSet(ctx context.Context, impl Implementation) (rimpl Implementation, rerr error) {
impl.Updated = time.Now()
if impl.ID == 0 {
impl.Created = time.Time{}
impl.Updated = time.Time{}
err := database.Insert(ctx, &impl)
return impl, err
}
err := database.Write(ctx, func(tx *bstore.Tx) error {
o := Implementation{ID: impl.ID}
if err := tx.Get(&o); err != nil {
return fmt.Errorf("get implementation: %v", err)
}
impl.Created = o.Created
impl.Updated = time.Now()
err := tx.Update(&impl)
rimpl = impl
return err
})
return rimpl, err
}
func (API) ImplementationRemove(ctx context.Context, id int64) error {
return database.Delete(ctx, &Implementation{ID: id})
}
func (API) SoftwareCreate(ctx context.Context, s Software) error {
s.Created = time.Time{}
s.Updated = time.Time{}
for i, c := range s.ID {
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || (i > 0 && c >= '0' && c <= '9')) {
return fmt.Errorf("invalid id %q", s.ID)
}
}
return database.Insert(ctx, &s)
}
func (API) SoftwareUpdate(ctx context.Context, s Software) error {
return database.Write(ctx, func(tx *bstore.Tx) error {
os := Software{ID: s.ID}
if err := tx.Get(&os); err != nil {
return fmt.Errorf("unknown software id %s", s.ID)
}
s.Created = os.Created
s.Updated = time.Now()
return tx.Update(&s)
})
}
func (API) SoftwareRemove(ctx context.Context, id string) error {
return database.Write(ctx, func(tx *bstore.Tx) error {
_, err := bstore.QueryTx[Implementation](tx).FilterNonzero(Implementation{SoftwareID: id}).Delete()
if err != nil {
return fmt.Errorf("removing implementation details for software: %v", err)
}
return tx.Delete(&Software{ID: id})
})
}
func (API) FeatureCreate(ctx context.Context, f Feature) error {
begin := true
for _, c := range f.ID {
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || (!begin && c >= '0' && c <= '9') || (!begin && c == '.')) {
return fmt.Errorf("invalid id %q", f.ID)
}
begin = c == '.'
}
f.Created = time.Time{}
f.Updated = time.Time{}
return database.Insert(ctx, &f)
}
func (API) FeatureUpdate(ctx context.Context, f Feature) error {
return database.Write(ctx, func(tx *bstore.Tx) error {
of := Feature{ID: f.ID}
if err := tx.Get(&of); err != nil {
return fmt.Errorf("unknown feature id %s", f.ID)
}
f.Created = of.Created
f.Updated = time.Now()
return tx.Update(&f)
})
}
func (API) FeatureRemove(ctx context.Context, id string) error {
return database.Write(ctx, func(tx *bstore.Tx) error {
_, err := bstore.QueryTx[Implementation](tx).FilterNonzero(Implementation{FeatureID: id}).Delete()
if err != nil {
return fmt.Errorf("removing implementation details for feature: %v", err)
}
return tx.Delete(&Feature{ID: id})
})
}
func (API) State(ctx context.Context) (State, error) {
var state State
err := database.Read(ctx, func(tx *bstore.Tx) error {
var err error
state.Software, err = bstore.QueryTx[Software](tx).List()
if err != nil {
return fmt.Errorf("reading software from database: %v", err)
}
state.Features, err = bstore.QueryTx[Feature](tx).List()
if err != nil {
return fmt.Errorf("reading features from database: %v", err)
}
state.Implementations, err = bstore.QueryTx[Implementation](tx).List()
if err != nil {
return fmt.Errorf("reading implementations from database: %v", err)
}
return nil
})
return state, err
}