-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
64 lines (53 loc) · 1.14 KB
/
module.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
package certmagicsqlite3
import (
"database/sql"
"errors"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func init() {
caddy.RegisterModule(Storage{})
}
func (Storage) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.storage.sqlite",
New: func() caddy.Module {
return new(Storage)
},
}
}
func (s *Storage) Provision(ctx caddy.Context) error {
if s.DataSourceName == "" {
return errors.New("set data source name")
}
db, err := sql.Open("sqlite", s.DataSourceName)
if err != nil {
return err
}
for _, stmt := range []string{pragmaWALEnabled, pragma500BusyTimeout, pragmaCaseSenstive} {
_, err = db.Exec(stmt, nil)
if err != nil {
return err
}
}
_, err = db.Exec(createTable)
if err != nil {
return err
}
s.db = db
s.lockExpirationTimeOut = defaultLockTimeOut
return nil
}
func (s *Storage) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if !d.Args(&s.DataSourceName) {
return d.ArgErr()
}
}
return nil
}
// interface guards
var (
_ caddy.Provisioner = (*Storage)(nil)
_ caddyfile.Unmarshaler = (*Storage)(nil)
)