-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.go
50 lines (38 loc) · 916 Bytes
/
common.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
// Copyright 2021 The VPN House Authors. All rights reserved.
// Use of this source code is governed by a AGPL-style
// license that can be found in the LICENSE file.
package storage
import (
"embed"
"errors"
"github.com/jmoiron/sqlx"
"github.com/vpnhouse/tunnel/pkg/xerror"
"github.com/vpnhouse/tunnel/pkg/xstorage"
_ "github.com/mattn/go-sqlite3"
)
//go:embed db/migrations
var migrations embed.FS
var ErrNotFound = errors.New("not found")
type Storage struct {
db *sqlx.DB
}
func New(path string) (*Storage, error) {
db, err := xstorage.NewSqlite3(path, migrations)
if err != nil {
return nil, err
}
return &Storage{
db: db,
}, nil
}
func (storage *Storage) Shutdown() error {
err := storage.db.Close()
if err != nil {
return xerror.EStorageError("failed close database", err)
}
storage.db = nil
return nil
}
func (storage *Storage) Running() bool {
return storage.db != nil
}