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
|
package daemon
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"path/filepath"
"time"
"codeberg.org/snonux/goprecords/internal/authkeys"
"codeberg.org/snonux/goprecords/internal/goprecords"
)
const (
defaultReadHeaderTimeout = 10 * time.Second
defaultReadTimeout = 2 * time.Minute
defaultWriteTimeout = 2 * time.Minute
defaultIdleTimeout = 2 * time.Minute
)
// Config holds settings for the HTTP report/upload daemon.
type Config struct {
StatsDir string
Addr string
AuthDB string
LogOutput io.Writer
}
// NewHandler returns HTTP handlers for daemon routes using the default auth DB
// under statsDir. It returns an error if the auth store cannot be opened.
func NewHandler(statsDir string) (http.Handler, error) {
store, err := openAuthStore(context.Background(), statsDir, "")
if err != nil {
return nil, fmt.Errorf("auth db: %w", err)
}
return routes(statsDir, "", store), nil
}
func routes(statsDir, authDB string, store *authkeys.Store) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/health", health)
mux.HandleFunc("/livez", health)
mux.HandleFunc("/readyz", readiness(statsDir, authDB))
mux.HandleFunc("/report", report(statsDir))
mux.Handle("/upload/", uploadHandler(statsDir, store))
return mux
}
func logWriter(cfg Config) io.Writer {
if cfg.LogOutput != nil {
return cfg.LogOutput
}
return os.Stdout
}
func newDaemonLogger(w io.Writer) (*slog.Logger, slog.Handler) {
h := slog.NewTextHandler(w, &slog.HandlerOptions{Level: slog.LevelInfo})
return slog.New(h), h
}
type statusRecorder struct {
http.ResponseWriter
code int
}
func (r *statusRecorder) WriteHeader(status int) {
if r.code == 0 {
r.code = status
}
r.ResponseWriter.WriteHeader(status)
}
func (r *statusRecorder) Write(b []byte) (int, error) {
if r.code == 0 {
r.code = http.StatusOK
}
return r.ResponseWriter.Write(b)
}
func withAccessLog(log *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec := &statusRecorder{ResponseWriter: w}
start := time.Now()
next.ServeHTTP(rec, r)
code := rec.code
if code == 0 {
code = http.StatusOK
}
log.Info("http_request", "method", r.Method, "path", r.URL.Path, "status", code, "duration_ms", time.Since(start).Milliseconds())
})
}
func newDaemonHTTPServer(addr string, handler http.Handler, errLog *log.Logger) *http.Server {
return &http.Server{
Addr: addr,
Handler: handler,
ErrorLog: errLog,
ReadHeaderTimeout: defaultReadHeaderTimeout,
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
IdleTimeout: defaultIdleTimeout,
}
}
// Run starts the HTTP server for cfg until ctx is canceled, then shuts down gracefully.
func Run(ctx context.Context, cfg Config) error {
if cfg.StatsDir == "" {
return fmt.Errorf("stats directory is required")
}
if cfg.Addr == "" {
return fmt.Errorf("listen address is required")
}
w := logWriter(cfg)
slogLog, textHandler := newDaemonLogger(w)
store, err := openAuthStore(ctx, cfg.StatsDir, cfg.AuthDB)
if err != nil {
return fmt.Errorf("auth db: %w", err)
}
defer store.Close()
srv := newDaemonHTTPServer(cfg.Addr, withAccessLog(slogLog, routes(cfg.StatsDir, cfg.AuthDB, store)),
slog.NewLogLogger(textHandler, slog.LevelError))
slogLog.Info("daemon_listen", "addr", cfg.Addr)
errCh := make(chan error, 1)
go func() { errCh <- srv.ListenAndServe() }()
select {
case <-ctx.Done():
shutCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_ = srv.Shutdown(shutCtx)
err := <-errCh
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("shutdown: %w", err)
}
return ctx.Err()
case err := <-errCh:
if errors.Is(err, http.ErrServerClosed) {
return nil
}
if err != nil {
return fmt.Errorf("listen %s: %w", cfg.Addr, err)
}
return nil
}
}
func health(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok\n"))
}
func readiness(statsDir, authDB string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if err := checkReadinessDirs(statsDir, authDB); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok\n"))
}
}
func checkReadinessDirs(statsDir, authDB string) error {
absStats, err := filepath.Abs(statsDir)
if err != nil {
return fmt.Errorf("stats-dir: %w", err)
}
if err := checkDirReadableWritable(absStats); err != nil {
return fmt.Errorf("stats-dir: %w", err)
}
authPath := authDB
if authPath == "" {
authPath = authkeys.DefaultPath(statsDir)
}
absAuth, err := filepath.Abs(authPath)
if err != nil {
return fmt.Errorf("auth-db: %w", err)
}
dbDir := filepath.Clean(filepath.Dir(absAuth))
if dbDir != filepath.Clean(absStats) {
if err := checkDirReadableWritable(dbDir); err != nil {
return fmt.Errorf("auth-db dir: %w", err)
}
}
return nil
}
func checkDirReadableWritable(dir string) error {
fi, err := os.Stat(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("missing")
}
return err
}
if !fi.IsDir() {
return fmt.Errorf("not a directory")
}
f, err := os.Open(dir)
if err != nil {
return fmt.Errorf("not readable: %w", err)
}
_, err = f.Readdirnames(1)
_ = f.Close()
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("not readable: %w", err)
}
tmp, err := os.CreateTemp(dir, ".goprecords-ready-*")
if err != nil {
return fmt.Errorf("not writable: %w", err)
}
name := tmp.Name()
if err := tmp.Close(); err != nil {
_ = os.Remove(name)
return fmt.Errorf("not writable: %w", err)
}
if err := os.Remove(name); err != nil {
return fmt.Errorf("not writable: %w", err)
}
return nil
}
func report(statsDir string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
cfg, err := goprecords.ParseReportQuery(r.URL.Query())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
aggr := goprecords.NewAggregator(statsDir)
aggregates, err := aggr.Aggregate(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var buf bytes.Buffer
if err := goprecords.WriteReports(&buf, aggregates, cfg); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", reportContentType(cfg.OutputFormat))
w.WriteHeader(http.StatusOK)
_, _ = w.Write(buf.Bytes())
}
}
func reportContentType(f goprecords.OutputFormat) string {
switch f {
case goprecords.FormatMarkdown:
return "text/markdown; charset=utf-8"
case goprecords.FormatGemtext:
return "text/gemini; charset=utf-8"
case goprecords.FormatHTML:
return "text/html; charset=utf-8"
default:
return "text/plain; charset=utf-8"
}
}
|