Skip to content

Commit a26e78d

Browse files
dunglasCopilot7-zete-7
authored
fix: create bolt.db in Caddy's data dir by default (#1139)
* fix: create bolt.db in Caddy's data dir by default * review * Update caddy/caddy_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> * fix tests * Update bolt.go Co-authored-by: Stanislau Kviatkouski <7-zete-7@users.noreply.github.com> Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> --------- Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Stanislau Kviatkouski <7-zete-7@users.noreply.github.com>
1 parent d8e2d58 commit a26e78d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

caddy/bolt.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package caddy
33
import (
44
"bytes"
55
"encoding/gob"
6+
"path/filepath"
67
"strconv"
78

89
"github.com/caddyserver/caddy/v2"
@@ -40,10 +41,15 @@ func (b *Bolt) GetTransport() mercure.Transport { //nolint:ireturn
4041
//
4142
//nolint:wrapcheck
4243
func (b *Bolt) Provision(ctx caddy.Context) error {
44+
if b.Path == "" {
45+
b.Path = filepath.Join(caddy.AppDataDir(), "mercure.db")
46+
}
47+
4348
var key bytes.Buffer
4449
if err := gob.NewEncoder(&key).Encode(b); err != nil {
4550
return err
4651
}
52+
4753
b.transportKey = key.String()
4854

4955
destructor, _, err := TransportUsagePool.LoadOrNew(b.transportKey, func() (caddy.Destructor, error) {
@@ -105,7 +111,7 @@ func (b *Bolt) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
105111

106112
f, e := strconv.ParseFloat(d.Val(), 64)
107113
if e != nil {
108-
return e
114+
return d.WrapErr(e)
109115
}
110116

111117
b.CleanupFrequency = f
@@ -117,7 +123,7 @@ func (b *Bolt) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
117123

118124
s, e := strconv.ParseUint(d.Val(), 10, 64)
119125
if e != nil {
120-
return e
126+
return d.WrapErr(e)
121127
}
122128

123129
b.Size = s

caddy/caddy_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/url"
88
"os"
9+
"path/filepath"
910
"strings"
1011
"sync"
1112
"testing"
@@ -23,19 +24,23 @@ const (
2324
)
2425

2526
func TestMercure(t *testing.T) {
27+
boltPath := filepath.Join(t.TempDir(), "bolt.db")
28+
2629
data := []struct {
2730
name string
2831
transportConfig string
2932
}{
30-
{"bolt", ""},
33+
{"bolt", `transport bolt {
34+
path ` + boltPath + `
35+
}`},
3136
{"local", "transport local\n"},
3237
}
3338

3439
for _, d := range data {
3540
t.Run(d.name, func(t *testing.T) {
3641
if d.name == "bolt" {
3742
t.Cleanup(func() {
38-
require.NoError(t, os.Remove("bolt.db"))
43+
require.NoError(t, os.Remove(boltPath))
3944
})
4045
}
4146

@@ -115,17 +120,13 @@ example.com:9080 {
115120
received.Wait()
116121

117122
if d.name != "bolt" {
118-
assert.NoFileExists(t, "bolt.db")
123+
assert.NoFileExists(t, boltPath)
119124
}
120125
})
121126
}
122127
}
123128

124129
func TestJWTPlaceholders(t *testing.T) {
125-
t.Cleanup(func() {
126-
require.NoError(t, os.Remove("bolt.db"))
127-
})
128-
129130
k, _ := os.ReadFile("../fixtures/jwt/RS256.key.pub")
130131
t.Setenv("TEST_JWT_KEY", string(k))
131132
t.Setenv("TEST_JWT_ALG", "RS256")
@@ -144,6 +145,7 @@ func TestJWTPlaceholders(t *testing.T) {
144145
mercure {
145146
anonymous
146147
publisher_jwt {env.TEST_JWT_KEY} {env.TEST_JWT_ALG}
148+
transport local
147149
}
148150
149151
respond 404

0 commit comments

Comments
 (0)