1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/edition/boot/runtime.go

107 lines
2.8 KiB
Go
Raw Normal View History

2017-07-20 12:30:03 +01:00
// 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"
2017-07-20 12:30:03 +01:00
"time"
"github.com/documize/community/core/database"
"github.com/documize/community/core/env"
2017-07-21 12:20:13 +01:00
"github.com/documize/community/core/secrets"
"github.com/documize/community/domain/store"
"github.com/documize/community/edition/storage"
2017-07-20 12:30:03 +01:00
"github.com/jmoiron/sqlx"
)
// InitRuntime prepares runtime using command line and environment variables.
func InitRuntime(r *env.Runtime, s *store.Store) bool {
2019-10-26 16:15:56 +01:00
// We need SALT to hash auth JWT tokens.
2017-07-20 12:30:03 +01:00
if r.Flags.Salt == "" {
2017-07-21 12:20:13 +01:00
r.Flags.Salt = secrets.RandSalt()
2017-07-20 12:30:03 +01:00
2017-07-21 12:20:13 +01:00
if r.Flags.Salt == "" {
2017-07-20 12:30:03 +01:00
return false
}
2019-10-26 16:15:56 +01:00
r.Log.Info("please set DOCUMIZESALT environment variable or use -salt switch with this value: " + r.Flags.Salt)
2017-07-20 12:30:03 +01:00
}
2019-10-26 16:15:56 +01:00
// We can use either or both HTTP and HTTPS ports.
2017-07-20 12:30:03 +01:00
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.
2018-09-12 20:03:34 +01:00
switch r.Flags.DBType {
case "mysql":
storage.SetMySQLProvider(r, s)
2018-09-12 20:03:34 +01:00
case "mariadb":
storage.SetMySQLProvider(r, s)
2018-09-12 20:03:34 +01:00
case "percona":
storage.SetMySQLProvider(r, s)
case "postgresql":
storage.SetPostgreSQLProvider(r, s)
2019-03-26 08:51:02 -04:00
case "sqlserver":
storage.SetSQLServerProvider(r, s)
default:
r.Log.Infof("Unsupported database type: %s", r.Flags.DBType)
2019-10-26 16:15:56 +01:00
r.Log.Info("Documize supports the following database types: mysql mariadb percona postgresql sqlserver")
os.Exit(1)
return false
2018-09-12 20:03:34 +01:00
}
2019-10-26 16:15:56 +01:00
// Open connection to database.
2019-04-01 20:08:57 +01:00
db, err := sqlx.Open(r.StoreProvider.DriverName(), r.StoreProvider.MakeConnectionString())
2017-07-20 18:46:11 +01:00
if err != nil {
r.Log.Error("Unable to open database", err)
os.Exit(1)
return false
2017-07-20 18:46:11 +01:00
}
2019-10-26 16:15:56 +01:00
// Set the database handle.
2017-07-20 18:46:11 +01:00
r.Db = db
2019-10-26 16:15:56 +01:00
// Set connection defaults.
2017-07-20 18:46:11 +01:00
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.
2017-07-20 18:46:11 +01:00
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)
2017-07-20 18:46:11 +01:00
return false
}
// Check database and upgrade if required.
if r.Flags.SiteMode != env.SiteModeOffline {
2017-07-20 18:46:11 +01:00
if database.Check(r) {
if err := database.InstallUpgrade(r, true); err != nil {
2017-07-20 18:46:11 +01:00
r.Log.Error("unable to run database migration", err)
return false
}
}
}
2017-07-20 12:30:03 +01:00
return true
}