2016-07-07 18:54:16 -07:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
2016-10-17 14:00:06 -07:00
|
|
|
// This software (Documize Community Edition) is licensed under
|
2016-07-07 18:54:16 -07:00
|
|
|
// 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
|
2016-10-17 14:00:06 -07:00
|
|
|
// by contacting <sales@documize.com>.
|
2016-07-07 18:54:16 -07:00
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
2017-07-18 21:55:17 +01:00
|
|
|
// Package env provides runtime, server level setup and configuration
|
|
|
|
package env
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2018-09-12 20:03:34 +01:00
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SQL-STORE: DbVariant needs to be struct like: name, delims, std params and conn string method
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-07-18 21:55:17 +01:00
|
|
|
// Runtime provides access to database, logger and other server-level scoped objects.
|
2017-07-19 14:48:33 +01:00
|
|
|
// Use Context for per-request values.
|
2017-07-18 21:55:17 +01:00
|
|
|
type Runtime struct {
|
2018-09-12 20:03:34 +01:00
|
|
|
Flags Flags
|
|
|
|
Db *sqlx.DB
|
|
|
|
Storage StoreProvider
|
|
|
|
Log Logger
|
|
|
|
Product ProdInfo
|
2016-07-07 18:54:16 -07:00
|
|
|
}
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
// SiteModeNormal serves app
|
|
|
|
SiteModeNormal = ""
|
2018-09-12 20:03:34 +01:00
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
// SiteModeOffline serves offline.html
|
|
|
|
SiteModeOffline = "1"
|
2018-09-12 20:03:34 +01:00
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
// SiteModeSetup tells Ember to serve setup route
|
|
|
|
SiteModeSetup = "2"
|
2018-09-12 20:03:34 +01:00
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
// SiteModeBadDB redirects to db-error.html page
|
|
|
|
SiteModeBadDB = "3"
|
|
|
|
)
|
2017-08-08 18:21:38 +01:00
|
|
|
|
2018-09-12 20:03:34 +01:00
|
|
|
// StoreType represents name of database system
|
|
|
|
type StoreType string
|
2017-08-08 18:21:38 +01:00
|
|
|
|
|
|
|
const (
|
2018-09-12 20:03:34 +01:00
|
|
|
// StoreTypeMySQL is MySQL
|
|
|
|
StoreTypeMySQL StoreType = "MySQL"
|
|
|
|
|
|
|
|
// StoreTypePercona is Percona
|
|
|
|
StoreTypePercona StoreType = "Percona"
|
|
|
|
|
|
|
|
// StoreTypeMariaDB is MariaDB
|
|
|
|
StoreTypeMariaDB StoreType = "MariaDB"
|
|
|
|
|
|
|
|
// StoreTypePostgreSQL is PostgreSQL
|
|
|
|
StoreTypePostgreSQL StoreType = "PostgreSQL"
|
|
|
|
|
|
|
|
// StoreTypeMSSQL is Microsoft SQL Server
|
|
|
|
StoreTypeMSSQL StoreType = "MSSQL"
|
2017-08-08 18:21:38 +01:00
|
|
|
)
|
2018-05-10 15:10:15 +01:00
|
|
|
|
2018-09-12 20:03:34 +01:00
|
|
|
// StoreProvider contains database specific details
|
|
|
|
type StoreProvider struct {
|
|
|
|
// Type identifies storage provider
|
|
|
|
Type StoreType
|
|
|
|
|
|
|
|
// SQL driver name used to open DB connection.
|
|
|
|
DriverName string
|
|
|
|
|
|
|
|
// Database connection string parameters that must be present before connecting to DB
|
|
|
|
Params map[string]string
|
|
|
|
|
|
|
|
// Example holds storage provider specific connection string format
|
|
|
|
// used in error messages
|
|
|
|
Example string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionString returns provider specific DB connection string
|
|
|
|
// complete with default parameters.
|
|
|
|
func (s *StoreProvider) ConnectionString(cs string) string {
|
|
|
|
switch s.Type {
|
|
|
|
|
|
|
|
case StoreTypePostgreSQL:
|
|
|
|
return "pg"
|
|
|
|
|
|
|
|
case StoreTypeMSSQL:
|
|
|
|
return "sql server"
|
|
|
|
|
|
|
|
case StoreTypeMySQL, StoreTypeMariaDB, StoreTypePercona:
|
|
|
|
queryBits := strings.Split(cs, "?")
|
|
|
|
ret := queryBits[0] + "?"
|
|
|
|
retFirst := true
|
|
|
|
|
|
|
|
if len(queryBits) == 2 {
|
|
|
|
paramBits := strings.Split(queryBits[1], "&")
|
|
|
|
for _, pb := range paramBits {
|
|
|
|
found := false
|
|
|
|
if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
|
|
|
|
_, found = s.Params[strings.TrimSpace(assignBits[0])]
|
|
|
|
}
|
|
|
|
if !found { // if we can't work out what it is, put it through
|
|
|
|
if retFirst {
|
|
|
|
retFirst = false
|
|
|
|
} else {
|
|
|
|
ret += "&"
|
|
|
|
}
|
|
|
|
ret += pb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range s.Params {
|
|
|
|
if retFirst {
|
|
|
|
retFirst = false
|
|
|
|
} else {
|
|
|
|
ret += "&"
|
|
|
|
}
|
|
|
|
ret += k + "=" + v
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:10:15 +01:00
|
|
|
const (
|
|
|
|
// CommunityEdition is AGPL product variant
|
|
|
|
CommunityEdition = "Community"
|
2018-09-12 20:03:34 +01:00
|
|
|
|
2018-05-10 15:10:15 +01:00
|
|
|
// EnterpriseEdition is commercial licensed product variant
|
|
|
|
EnterpriseEdition = "Enterprise"
|
|
|
|
)
|