Skip to content

Commit 458a199

Browse files
committed
1 parent a4477d7 commit 458a199

File tree

7 files changed

+40
-9
lines changed

7 files changed

+40
-9
lines changed

server/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
- deploy
1717

18+
- diff my currently used scnsend script vs the one in the docs here
19+
1820
-------------------------------------------------------------------------------------------------------------------------------
1921

2022
- in my script: use (backupname || hostname) for sendername

server/common/ginext/gin.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ func NewEngine(cfg scn.Config) *gin.Engine {
1313
engine.RedirectFixedPath = false
1414
engine.RedirectTrailingSlash = false
1515

16-
engine.Use(CorsMiddleware())
16+
if cfg.Cors {
17+
engine.Use(CorsMiddleware())
18+
}
1719

1820
if cfg.GinDebug {
1921
ginlogger := gin.Logger()

server/common/ginresp/resp.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ func (j dataHTTPResponse) Write(g *gin.Context) {
5151
g.Data(j.statusCode, j.contentType, j.data)
5252
}
5353

54+
type errorHTTPResponse struct {
55+
statusCode int
56+
data any
57+
error error
58+
}
59+
60+
func (j errorHTTPResponse) Write(g *gin.Context) {
61+
g.JSON(j.statusCode, j.data)
62+
}
63+
5464
func Status(sc int) HTTPResponse {
5565
return &emptyHTTPResponse{statusCode: sc}
5666
}
@@ -98,7 +108,7 @@ func createApiError(g *gin.Context, ident string, status int, errorid apierr.API
98108
Msg(fmt.Sprintf("[%s] %s", ident, msg))
99109

100110
if scn.Conf.ReturnRawErrors {
101-
return &jsonHTTPResponse{
111+
return &errorHTTPResponse{
102112
statusCode: status,
103113
data: apiError{
104114
Success: false,
@@ -108,16 +118,18 @@ func createApiError(g *gin.Context, ident string, status int, errorid apierr.API
108118
RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))),
109119
Trace: string(debug.Stack()),
110120
},
121+
error: e,
111122
}
112123
} else {
113-
return &jsonHTTPResponse{
124+
return &errorHTTPResponse{
114125
statusCode: status,
115126
data: apiError{
116127
Success: false,
117128
Error: int(errorid),
118129
ErrorHighlight: int(highlight),
119130
Message: msg,
120131
},
132+
error: e,
121133
}
122134
}
123135
}

server/config.go

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Config struct {
2424
DBConnMaxLifetime time.Duration `env:"SCN_DB_CONNEXTIONMAXLIFETIME"`
2525
DBConnMaxIdleTime time.Duration `env:"SCN_DB_CONNEXTIONMAXIDLETIME"`
2626
DBCheckForeignKeys bool `env:"SCN_DB_CHECKFOREIGNKEYS"`
27+
DBSingleConn bool `env:"SCN_DB_SINGLECONNECTION"`
2728
RequestTimeout time.Duration `env:"SCN_REQUEST_TIMEOUT"`
2829
ReturnRawErrors bool `env:"SCN_ERROR_RETURN"`
2930
DummyFirebase bool `env:"SCN_DUMMY_FB"`
@@ -39,6 +40,7 @@ type Config struct {
3940
GoogleAPIPrivateKey string `env:"SCN_GOOG_PRIVATEKEY"`
4041
GooglePackageName string `env:"SCN_GOOG_PACKAGENAME"`
4142
GoogleProProductID string `env:"SCN_GOOG_PROPRODUCTID"`
43+
Cors bool `env:"SCN_CORS"`
4244
}
4345

4446
var Conf Config
@@ -55,6 +57,7 @@ var configLocHost = func() Config {
5557
DBJournal: "WAL",
5658
DBTimeout: 5 * time.Second,
5759
DBCheckForeignKeys: false,
60+
DBSingleConn: true,
5861
DBMaxOpenConns: 5,
5962
DBMaxIdleConns: 5,
6063
DBConnMaxLifetime: 60 * time.Minute,
@@ -74,6 +77,7 @@ var configLocHost = func() Config {
7477
GoogleAPIPrivateKey: "",
7578
GooglePackageName: "",
7679
GoogleProProductID: "",
80+
Cors: true,
7781
}
7882
}
7983

@@ -89,6 +93,7 @@ var configLocDocker = func() Config {
8993
DBJournal: "WAL",
9094
DBTimeout: 5 * time.Second,
9195
DBCheckForeignKeys: false,
96+
DBSingleConn: true,
9297
DBMaxOpenConns: 5,
9398
DBMaxIdleConns: 5,
9499
DBConnMaxLifetime: 60 * time.Minute,
@@ -108,6 +113,7 @@ var configLocDocker = func() Config {
108113
GoogleAPIPrivateKey: "",
109114
GooglePackageName: "",
110115
GoogleProProductID: "",
116+
Cors: true,
111117
}
112118
}
113119

@@ -123,6 +129,7 @@ var configDev = func() Config {
123129
DBJournal: "WAL",
124130
DBTimeout: 5 * time.Second,
125131
DBCheckForeignKeys: false,
132+
DBSingleConn: true,
126133
DBMaxOpenConns: 5,
127134
DBMaxIdleConns: 5,
128135
DBConnMaxLifetime: 60 * time.Minute,
@@ -142,6 +149,7 @@ var configDev = func() Config {
142149
GoogleAPIPrivateKey: confEnv("SCN_GOOG_PRIVATEKEY"),
143150
GooglePackageName: confEnv("SCN_GOOG_PACKAGENAME"),
144151
GoogleProProductID: confEnv("SCN_GOOG_PROPRODUCTID"),
152+
Cors: true,
145153
}
146154
}
147155

@@ -157,6 +165,7 @@ var configStag = func() Config {
157165
DBJournal: "WAL",
158166
DBTimeout: 5 * time.Second,
159167
DBCheckForeignKeys: false,
168+
DBSingleConn: true,
160169
DBMaxOpenConns: 5,
161170
DBMaxIdleConns: 5,
162171
DBConnMaxLifetime: 60 * time.Minute,
@@ -176,6 +185,7 @@ var configStag = func() Config {
176185
GoogleAPIPrivateKey: confEnv("SCN_GOOG_PRIVATEKEY"),
177186
GooglePackageName: confEnv("SCN_GOOG_PACKAGENAME"),
178187
GoogleProProductID: confEnv("SCN_GOOG_PROPRODUCTID"),
188+
Cors: true,
179189
}
180190
}
181191

@@ -191,6 +201,7 @@ var configProd = func() Config {
191201
DBJournal: "WAL",
192202
DBTimeout: 5 * time.Second,
193203
DBCheckForeignKeys: false,
204+
DBSingleConn: true,
194205
DBMaxOpenConns: 5,
195206
DBMaxIdleConns: 5,
196207
DBConnMaxLifetime: 60 * time.Minute,
@@ -210,6 +221,7 @@ var configProd = func() Config {
210221
GoogleAPIPrivateKey: confEnv("SCN_SCN_GOOG_PRIVATEKEY"),
211222
GooglePackageName: confEnv("SCN_SCN_GOOG_PACKAGENAME"),
212223
GoogleProProductID: confEnv("SCN_SCN_GOOG_PROPRODUCTID"),
224+
Cors: true,
213225
}
214226
}
215227

server/db/database.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ func NewDatabase(conf server.Config) (*Database, error) {
2727
return nil, err
2828
}
2929

30-
xdb.SetMaxOpenConns(5)
31-
xdb.SetMaxIdleConns(5)
32-
xdb.SetConnMaxLifetime(60 * time.Minute)
33-
xdb.SetConnMaxIdleTime(60 * time.Minute)
30+
if conf.DBSingleConn {
31+
xdb.SetMaxOpenConns(1)
32+
} else {
33+
xdb.SetMaxOpenConns(5)
34+
xdb.SetMaxIdleConns(5)
35+
xdb.SetConnMaxLifetime(60 * time.Minute)
36+
xdb.SetConnMaxIdleTime(60 * time.Minute)
37+
}
3438

3539
qqdb := sq.NewDB(xdb)
3640

server/test/send_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,6 @@ func TestQuotaExceededPro(t *testing.T) {
14291429
}
14301430

14311431
func TestSendParallel(t *testing.T) {
1432-
t.SkipNow()
1433-
14341432
_, baseUrl, stop := tt.StartSimpleWebserver(t)
14351433
defer stop()
14361434

server/test/util/webserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func StartSimpleWebserver(t *testing.T) (*logic.Application, string, func()) {
6161
RequestTimeout: 30 * time.Second,
6262
ReturnRawErrors: true,
6363
DummyFirebase: true,
64+
DBSingleConn: true,
6465
}
6566

6667
sqlite, err := db.NewDatabase(conf)

0 commit comments

Comments
 (0)