Skip to content

Commit 2999011

Browse files
committed
Convert db backend to use Gorm, with basis for support
for Mysql and Postgres in addition to existing Sqlite
1 parent b4e5b8b commit 2999011

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1212
-3072
lines changed

backend/Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tasks:
3030
silent: true
3131
- cmd: rm -f dist/bin/*
3232
silent: true
33-
- cmd: go build -buildvcs=false -ldflags="-X main.commit={{.GIT_COMMIT}} -X main.version={{.VERSION}}" -o ../dist/bin/server ./cmd/server/main.go
33+
- cmd: go build -tags 'json1' -buildvcs=false -ldflags="-X main.commit={{.GIT_COMMIT}} -X main.version={{.VERSION}}" -o ../dist/bin/server ./cmd/server/main.go
3434
silent: true
3535
- cmd: go build -buildvcs=false -ldflags="-X main.commit={{.GIT_COMMIT}} -X main.version={{.VERSION}}" -o ../dist/bin/ipranges ./cmd/ipranges/main.go
3636
silent: true

backend/cmd/server/main.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"npm/internal/entity/certificate"
1212
"npm/internal/entity/host"
1313
"npm/internal/entity/setting"
14+
"npm/internal/entity/user"
15+
"npm/internal/errors"
1416
"npm/internal/jobqueue"
1517
"npm/internal/logger"
1618
)
@@ -25,7 +27,7 @@ func main() {
2527

2628
database.Migrate(func() {
2729
setting.ApplySettings()
28-
database.CheckSetup()
30+
checkSetup()
2931

3032
// Internal Job Queue
3133
jobqueue.Start()
@@ -41,7 +43,8 @@ func main() {
4143
if irq == syscall.SIGINT || irq == syscall.SIGTERM {
4244
logger.Info("Got ", irq, " shutting server down ...")
4345
// Close db
44-
err := database.GetInstance().Close()
46+
sqlDB, _ := database.GetDB().DB()
47+
err := sqlDB.Close()
4548
if err != nil {
4649
logger.Error("DatabaseCloseError", err)
4750
}
@@ -52,3 +55,28 @@ func main() {
5255
}
5356
})
5457
}
58+
59+
// checkSetup Quick check by counting the number of users in the database
60+
func checkSetup() {
61+
db := database.GetDB()
62+
var count int64
63+
64+
if db != nil {
65+
db.Model(&user.Model{}).
66+
Where("is_disabled = ?", false).
67+
Where("is_system = ?", false).
68+
Count(&count)
69+
70+
if count == 0 {
71+
logger.Warn("No users found, starting in Setup Mode")
72+
} else {
73+
config.IsSetup = true
74+
logger.Info("Application is setup")
75+
}
76+
if config.ErrorReporting {
77+
logger.Warn("Error reporting is enabled - Application Errors WILL be sent to Sentry, you can disable this in the Settings interface")
78+
}
79+
} else {
80+
logger.Error("DatabaseError", errors.ErrDatabaseUnavailable)
81+
}
82+
}

backend/db/schema.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO "schema_migrations" (version) VALUES
2+
('20201013035318'),
3+
('20201013035839');

backend/embed/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var Assets embed.FS
1414

1515
// MigrationFiles are database migrations
1616
//
17-
//go:embed migrations/*.sql
17+
//go:embed migrations
1818
var MigrationFiles embed.FS
1919

2020
// NginxFiles hold nginx config templates

backend/embed/migrations/20201013035318_initial_schema.sql renamed to backend/embed/migrations/sqlite/20201013035318_initial_schema.sql

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,49 @@
33
CREATE TABLE IF NOT EXISTS `user`
44
(
55
id INTEGER PRIMARY KEY AUTOINCREMENT,
6-
created_on INTEGER NOT NULL DEFAULT 0,
7-
modified_on INTEGER NOT NULL DEFAULT 0,
6+
created_at INTEGER NOT NULL DEFAULT 0,
7+
updated_at INTEGER NOT NULL DEFAULT 0,
8+
is_deleted INTEGER NOT NULL DEFAULT 0,
89
name TEXT NOT NULL,
910
nickname TEXT NOT NULL,
1011
email TEXT NOT NULL,
1112
is_system INTEGER NOT NULL DEFAULT 0,
12-
is_disabled INTEGER NOT NULL DEFAULT 0,
13-
is_deleted INTEGER NOT NULL DEFAULT 0
13+
is_disabled INTEGER NOT NULL DEFAULT 0
1414
);
1515

1616
CREATE TABLE IF NOT EXISTS `capability`
1717
(
18-
id INTEGER PRIMARY KEY AUTOINCREMENT,
19-
name TEXT NOT NULL,
18+
name TEXT PRIMARY KEY,
2019
UNIQUE (name)
2120
);
2221

2322
CREATE TABLE IF NOT EXISTS `user_has_capability`
2423
(
2524
user_id INTEGER NOT NULL,
26-
capability_id INTEGER NOT NULL,
27-
UNIQUE (user_id, capability_id),
28-
FOREIGN KEY (capability_id) REFERENCES capability (id)
25+
capability_name TEXT NOT NULL,
26+
UNIQUE (user_id, capability_name),
27+
FOREIGN KEY (capability_name) REFERENCES capability (name)
2928
);
3029

3130
CREATE TABLE IF NOT EXISTS `auth`
3231
(
3332
id INTEGER PRIMARY KEY AUTOINCREMENT,
34-
created_on INTEGER NOT NULL DEFAULT 0,
35-
modified_on INTEGER NOT NULL DEFAULT 0,
33+
created_at INTEGER NOT NULL DEFAULT 0,
34+
updated_at INTEGER NOT NULL DEFAULT 0,
35+
is_deleted INTEGER NOT NULL DEFAULT 0,
3636
user_id INTEGER NOT NULL,
3737
type TEXT NOT NULL,
3838
secret TEXT NOT NULL,
39-
is_deleted INTEGER NOT NULL DEFAULT 0,
4039
FOREIGN KEY (user_id) REFERENCES user (id),
4140
UNIQUE (user_id, type)
4241
);
4342

4443
CREATE TABLE IF NOT EXISTS `setting`
4544
(
4645
id INTEGER PRIMARY KEY AUTOINCREMENT,
47-
created_on INTEGER NOT NULL DEFAULT 0,
48-
modified_on INTEGER NOT NULL DEFAULT 0,
46+
created_at INTEGER NOT NULL DEFAULT 0,
47+
updated_at INTEGER NOT NULL DEFAULT 0,
48+
is_deleted INTEGER NOT NULL DEFAULT 0,
4949
name TEXT NOT NULL,
5050
description TEXT NOT NULL DEFAULT "",
5151
value TEXT NOT NULL,
@@ -55,8 +55,9 @@ CREATE TABLE IF NOT EXISTS `setting`
5555
CREATE TABLE IF NOT EXISTS `audit_log`
5656
(
5757
id INTEGER PRIMARY KEY AUTOINCREMENT,
58-
created_on INTEGER NOT NULL DEFAULT 0,
59-
modified_on INTEGER NOT NULL DEFAULT 0,
58+
created_at INTEGER NOT NULL DEFAULT 0,
59+
updated_at INTEGER NOT NULL DEFAULT 0,
60+
is_deleted INTEGER NOT NULL DEFAULT 0,
6061
user_id INTEGER NOT NULL,
6162
object_type TEXT NOT NULL,
6263
object_id INTEGER NOT NULL,
@@ -68,36 +69,37 @@ CREATE TABLE IF NOT EXISTS `audit_log`
6869
CREATE TABLE IF NOT EXISTS `certificate_authority`
6970
(
7071
id INTEGER PRIMARY KEY AUTOINCREMENT,
71-
created_on INTEGER NOT NULL DEFAULT 0,
72-
modified_on INTEGER NOT NULL DEFAULT 0,
72+
created_at INTEGER NOT NULL DEFAULT 0,
73+
updated_at INTEGER NOT NULL DEFAULT 0,
74+
is_deleted INTEGER NOT NULL DEFAULT 0,
7375
name TEXT NOT NULL,
7476
acmesh_server TEXT NOT NULL DEFAULT "",
7577
ca_bundle TEXT NOT NULL DEFAULT "",
7678
is_wildcard_supported INTEGER NOT NULL DEFAULT 0, -- specific to each CA, acme v1 doesn't usually have wildcards
7779
max_domains INTEGER NOT NULL DEFAULT 5, -- per request
78-
is_readonly INTEGER NOT NULL DEFAULT 0,
79-
is_deleted INTEGER NOT NULL DEFAULT 0
80+
is_readonly INTEGER NOT NULL DEFAULT 0
8081
);
8182

8283
CREATE TABLE IF NOT EXISTS `dns_provider`
8384
(
8485
id INTEGER PRIMARY KEY AUTOINCREMENT,
85-
created_on INTEGER NOT NULL DEFAULT 0,
86-
modified_on INTEGER NOT NULL DEFAULT 0,
86+
created_at INTEGER NOT NULL DEFAULT 0,
87+
updated_at INTEGER NOT NULL DEFAULT 0,
88+
is_deleted INTEGER NOT NULL DEFAULT 0,
8789
user_id INTEGER NOT NULL,
8890
name TEXT NOT NULL,
8991
acmesh_name TEXT NOT NULL,
9092
dns_sleep INTEGER NOT NULL DEFAULT 0,
9193
meta TEXT NOT NULL,
92-
is_deleted INTEGER NOT NULL DEFAULT 0,
9394
FOREIGN KEY (user_id) REFERENCES user (id)
9495
);
9596

9697
CREATE TABLE IF NOT EXISTS `certificate`
9798
(
9899
id INTEGER PRIMARY KEY AUTOINCREMENT,
99-
created_on INTEGER NOT NULL DEFAULT 0,
100-
modified_on INTEGER NOT NULL DEFAULT 0,
100+
created_at INTEGER NOT NULL DEFAULT 0,
101+
updated_at INTEGER NOT NULL DEFAULT 0,
102+
is_deleted INTEGER NOT NULL DEFAULT 0,
101103
type TEXT NOT NULL, -- custom,dns,http
102104
user_id INTEGER NOT NULL,
103105
certificate_authority_id INTEGER, -- 0 for a custom cert
@@ -109,7 +111,6 @@ CREATE TABLE IF NOT EXISTS `certificate`
109111
error_message text NOT NULL DEFAULT "",
110112
meta TEXT NOT NULL,
111113
is_ecc INTEGER NOT NULL DEFAULT 0,
112-
is_deleted INTEGER NOT NULL DEFAULT 0,
113114
FOREIGN KEY (user_id) REFERENCES user (id),
114115
FOREIGN KEY (certificate_authority_id) REFERENCES certificate_authority (id),
115116
FOREIGN KEY (dns_provider_id) REFERENCES dns_provider (id)
@@ -118,24 +119,25 @@ CREATE TABLE IF NOT EXISTS `certificate`
118119
CREATE TABLE IF NOT EXISTS `stream`
119120
(
120121
id INTEGER PRIMARY KEY AUTOINCREMENT,
121-
created_on INTEGER NOT NULL DEFAULT 0,
122-
modified_on INTEGER NOT NULL DEFAULT 0,
122+
created_at INTEGER NOT NULL DEFAULT 0,
123+
updated_at INTEGER NOT NULL DEFAULT 0,
124+
is_deleted INTEGER NOT NULL DEFAULT 0,
123125
user_id INTEGER NOT NULL,
124126
listen_interface TEXT NOT NULL,
125127
incoming_port INTEGER NOT NULL,
126128
tcp_forwarding INTEGER NOT NULL DEFAULT 0,
127129
udp_forwarding INTEGER NOT NULL DEFAULT 0,
128130
advanced_config TEXT NOT NULL,
129131
is_disabled INTEGER NOT NULL DEFAULT 0,
130-
is_deleted INTEGER NOT NULL DEFAULT 0,
131132
FOREIGN KEY (user_id) REFERENCES user (id)
132133
);
133134

134135
CREATE TABLE IF NOT EXISTS `upstream`
135136
(
136137
id INTEGER PRIMARY KEY AUTOINCREMENT,
137-
created_on INTEGER NOT NULL DEFAULT 0,
138-
modified_on INTEGER NOT NULL DEFAULT 0,
138+
created_at INTEGER NOT NULL DEFAULT 0,
139+
updated_at INTEGER NOT NULL DEFAULT 0,
140+
is_deleted INTEGER NOT NULL DEFAULT 0,
139141
user_id INTEGER NOT NULL,
140142
name TEXT NOT NULL,
141143
nginx_template_id INTEGER NOT NULL,
@@ -148,57 +150,57 @@ CREATE TABLE IF NOT EXISTS `upstream`
148150
advanced_config TEXT NOT NULL,
149151
status TEXT NOT NULL DEFAULT "",
150152
error_message TEXT NOT NULL DEFAULT "",
151-
is_deleted INTEGER NOT NULL DEFAULT 0,
152153
FOREIGN KEY (user_id) REFERENCES user (id),
153154
FOREIGN KEY (nginx_template_id) REFERENCES nginx_template (id)
154155
);
155156

156157
CREATE TABLE IF NOT EXISTS `upstream_server`
157158
(
158159
id INTEGER PRIMARY KEY AUTOINCREMENT,
159-
created_on INTEGER NOT NULL DEFAULT 0,
160-
modified_on INTEGER NOT NULL DEFAULT 0,
160+
created_at INTEGER NOT NULL DEFAULT 0,
161+
updated_at INTEGER NOT NULL DEFAULT 0,
162+
is_deleted INTEGER NOT NULL DEFAULT 0,
161163
upstream_id INTEGER NOT NULL,
162164
server TEXT NOT NULL,
163165
weight INTEGER NOT NULL DEFAULT 0,
164166
max_conns INTEGER NOT NULL DEFAULT 0,
165167
max_fails INTEGER NOT NULL DEFAULT 0,
166168
fail_timeout INTEGER NOT NULL DEFAULT 0,
167-
backup INTEGER NOT NULL DEFAULT 0,
168-
is_deleted INTEGER NOT NULL DEFAULT 0,
169+
is_backup INTEGER NOT NULL DEFAULT 0,
169170
FOREIGN KEY (upstream_id) REFERENCES upstream (id)
170171
);
171172

172173
CREATE TABLE IF NOT EXISTS `access_list`
173174
(
174175
id INTEGER PRIMARY KEY AUTOINCREMENT,
175-
created_on INTEGER NOT NULL DEFAULT 0,
176-
modified_on INTEGER NOT NULL DEFAULT 0,
176+
created_at INTEGER NOT NULL DEFAULT 0,
177+
updated_at INTEGER NOT NULL DEFAULT 0,
178+
is_deleted INTEGER NOT NULL DEFAULT 0,
177179
user_id INTEGER NOT NULL,
178180
name TEXT NOT NULL,
179181
meta TEXT NOT NULL,
180-
is_deleted INTEGER NOT NULL DEFAULT 0,
181182
FOREIGN KEY (user_id) REFERENCES user (id)
182183
);
183184

184185
CREATE TABLE IF NOT EXISTS `nginx_template`
185186
(
186187
id INTEGER PRIMARY KEY AUTOINCREMENT,
187-
created_on INTEGER NOT NULL DEFAULT 0,
188-
modified_on INTEGER NOT NULL DEFAULT 0,
188+
created_at INTEGER NOT NULL DEFAULT 0,
189+
updated_at INTEGER NOT NULL DEFAULT 0,
190+
is_deleted INTEGER NOT NULL DEFAULT 0,
189191
user_id INTEGER NOT NULL,
190192
name TEXT NOT NULL,
191193
type TEXT NOT NULL,
192194
template TEXT NOT NULL,
193-
is_deleted INTEGER NOT NULL DEFAULT 0,
194195
FOREIGN KEY (user_id) REFERENCES user (id)
195196
);
196197

197198
CREATE TABLE IF NOT EXISTS `host`
198199
(
199200
id INTEGER PRIMARY KEY AUTOINCREMENT,
200-
created_on INTEGER NOT NULL DEFAULT 0,
201-
modified_on INTEGER NOT NULL DEFAULT 0,
201+
created_at INTEGER NOT NULL DEFAULT 0,
202+
updated_at INTEGER NOT NULL DEFAULT 0,
203+
is_deleted INTEGER NOT NULL DEFAULT 0,
202204
user_id INTEGER NOT NULL,
203205
type TEXT NOT NULL,
204206
nginx_template_id INTEGER NOT NULL,
@@ -222,7 +224,6 @@ CREATE TABLE IF NOT EXISTS `host`
222224
status TEXT NOT NULL DEFAULT "",
223225
error_message TEXT NOT NULL DEFAULT "",
224226
is_disabled INTEGER NOT NULL DEFAULT 0,
225-
is_deleted INTEGER NOT NULL DEFAULT 0,
226227
FOREIGN KEY (user_id) REFERENCES user (id),
227228
FOREIGN KEY (nginx_template_id) REFERENCES nginx_template (id),
228229
FOREIGN KEY (upstream_id) REFERENCES upstream (id),

0 commit comments

Comments
 (0)