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

92 lines
2.5 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 (
"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"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/domain"
2017-07-20 12:30:03 +01:00
"github.com/jmoiron/sqlx"
)
// InitRuntime prepares runtime using command line and environment variables.
2017-07-26 10:50:26 +01:00
func InitRuntime(r *env.Runtime, s *domain.Store) bool {
2017-07-21 12:20:13 +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
}
r.Log.Info("please set DOCUMIZESALT or use -salt with this value: " + r.Flags.Salt)
}
2017-07-21 12:20:13 +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"
}
}
2018-09-12 20:03:34 +01:00
// Work out required storage provider set it up.
switch r.Flags.DBType {
case "mysql":
r.Storage = env.StoreProvider{Type: env.StoreTypeMySQL, DriverName: "mysql"}
StoreMySQL(r, s)
case "mariadb":
r.Storage = env.StoreProvider{Type: env.StoreTypeMariaDB, DriverName: "mysql"}
StoreMySQL(r, s)
case "percona":
r.Storage = env.StoreProvider{Type: env.StoreTypePercona, DriverName: "mysql"}
StoreMySQL(r, s)
}
// Open connection to database
db, err := sqlx.Open(r.Storage.DriverName, r.Storage.ConnectionString(r.Flags.DBConn))
2017-07-20 18:46:11 +01:00
if err != nil {
r.Log.Error("unable to setup database", err)
}
r.Db = db
r.Db.SetMaxIdleConns(30)
r.Db.SetMaxOpenConns(100)
r.Db.SetConnMaxLifetime(time.Second * 14400)
err = r.Db.Ping()
if err != nil {
2018-09-12 20:03:34 +01:00
r.Log.Error("unable to connect to database - "+r.Storage.Example, err)
2017-07-20 18:46:11 +01:00
return false
}
2018-09-12 20:03:34 +01:00
// Go into setup mode if required.
if r.Flags.SiteMode != env.SiteModeOffline {
2017-07-20 18:46:11 +01:00
if database.Check(r) {
2017-08-02 15:26:31 +01:00
if err := database.Migrate(r, true /* the config table exists */); 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
}
2018-09-12 20:03:34 +01:00
// Clever way to detect database type:
// https://github.com/golang-sql/sqlexp/blob/c2488a8be21d20d31abf0d05c2735efd2d09afe4/quoter.go#L46