/
runtime.go
109 lines (94 loc) · 2.87 KB
/
runtime.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package boot prepares runtime environment.
package boot
import (
"os"
"time"
"github.com/documize/community/core/database"
"github.com/documize/community/core/env"
"github.com/documize/community/core/secrets"
"github.com/documize/community/domain/store"
"github.com/documize/community/edition/storage"
"github.com/jmoiron/sqlx"
)
// InitRuntime prepares runtime using command line and environment variables.
func InitRuntime(r *env.Runtime, s *store.Store) bool {
// TODO: remove this line when Go1.13 is released.
os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
// We need SALT to hash auth JWT tokens
if r.Flags.Salt == "" {
r.Flags.Salt = secrets.RandSalt()
if r.Flags.Salt == "" {
return false
}
r.Log.Info("please set DOCUMIZESALT or use -salt with this value: " + r.Flags.Salt)
}
// We can use either or both HTTP and HTTPS ports
if r.Flags.SSLCertFile == "" && r.Flags.SSLKeyFile == "" {
if r.Flags.HTTPPort == "" {
r.Flags.HTTPPort = "80"
}
} else {
if r.Flags.HTTPPort == "" {
r.Flags.HTTPPort = "443"
}
}
// Set up required storage provider.
switch r.Flags.DBType {
case "mysql":
storage.SetMySQLProvider(r, s)
case "mariadb":
storage.SetMySQLProvider(r, s)
case "percona":
storage.SetMySQLProvider(r, s)
case "postgresql":
storage.SetPostgreSQLProvider(r, s)
case "sqlserver":
storage.SetSQLServerProvider(r, s)
default:
r.Log.Infof("Unsupported database type: %s", r.Flags.DBType)
r.Log.Info("Documize supports the following database types: mysql | mariadb | percona | postgresql")
os.Exit(1)
return false
}
// Open connection to database
db, err := sqlx.Open(r.StoreProvider.DriverName(), r.StoreProvider.MakeConnectionString())
if err != nil {
r.Log.Error("Unable to open database", err)
os.Exit(1)
return false
}
// Set the database handle
r.Db = db
// Set connection defaults
r.Db.SetMaxIdleConns(30)
r.Db.SetMaxOpenConns(100)
r.Db.SetConnMaxLifetime(time.Second * 14400)
// Ping verifies a connection to the database is still alive, establishing a connection if necessary.
err = r.Db.Ping()
if err != nil {
r.Log.Error("Unable to connect to database", err)
r.Log.Info(r.StoreProvider.Example())
os.Exit(1)
return false
}
// Check database and upgrade if required.
if r.Flags.SiteMode != env.SiteModeOffline {
if database.Check(r) {
if err := database.InstallUpgrade(r, true); err != nil {
r.Log.Error("unable to run database migration", err)
return false
}
}
}
return true
}