1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 21:29:42 +02:00

refactored routing/web serving logic

This commit is contained in:
Harvey Kandola 2017-07-21 18:14:19 +01:00
parent 76d77bef9b
commit d888962082
16 changed files with 410 additions and 382 deletions

View file

@ -18,9 +18,8 @@ import (
"strings"
"github.com/documize/community/core/env"
"github.com/documize/community/core/log"
"github.com/documize/community/core/streamutil"
"github.com/documize/community/core/web"
"github.com/documize/community/server/web"
"github.com/jmoiron/sqlx"
)
@ -39,7 +38,7 @@ var dbPtr *sqlx.DB
func Check(runtime *env.Runtime) bool {
dbPtr = runtime.Db
log.Info("Database checks: started")
runtime.Log.Info("Database checks: started")
csBits := strings.Split(runtime.Flags.DBConn, "/")
if len(csBits) > 1 {
@ -48,7 +47,7 @@ func Check(runtime *env.Runtime) bool {
rows, err := runtime.Db.Query("SELECT VERSION() AS version, @@version_comment as comment, @@character_set_database AS charset, @@collation_database AS collation;")
if err != nil {
log.Error("Can't get MySQL configuration", err)
runtime.Log.Error("Can't get MySQL configuration", err)
web.SiteInfo.Issue = "Can't get MySQL configuration: " + err.Error()
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
@ -64,7 +63,7 @@ func Check(runtime *env.Runtime) bool {
}
if err != nil {
log.Error("no MySQL configuration returned", err)
runtime.Log.Error("no MySQL configuration returned", err)
web.SiteInfo.Issue = "no MySQL configuration return issue: " + err.Error()
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
@ -74,12 +73,12 @@ func Check(runtime *env.Runtime) bool {
// MySQL and Percona share same version scheme (e..g 5.7.10).
// MariaDB starts at 10.2.x
sqlVariant := GetSQLVariant(dbComment)
log.Info("Database checks: SQL variant " + sqlVariant)
log.Info("Database checks: SQL version " + version)
runtime.Log.Info("Database checks: SQL variant " + sqlVariant)
runtime.Log.Info("Database checks: SQL version " + version)
verNums, err := GetSQLVersion(version)
if err != nil {
log.Error("Database version check failed", err)
runtime.Log.Error("Database version check failed", err)
}
// Check minimum MySQL version as we need JSON column type.
@ -91,7 +90,7 @@ func Check(runtime *env.Runtime) bool {
for k, v := range verInts {
if verNums[k] < v {
want := fmt.Sprintf("%d.%d.%d", verInts[0], verInts[1], verInts[2])
log.Error("MySQL version element "+strconv.Itoa(k+1)+" of '"+version+"' not high enough, need at least version "+want, errors.New("bad MySQL version"))
runtime.Log.Error("MySQL version element "+strconv.Itoa(k+1)+" of '"+version+"' not high enough, need at least version "+want, errors.New("bad MySQL version"))
web.SiteInfo.Issue = "MySQL version element " + strconv.Itoa(k+1) + " of '" + version + "' not high enough, need at least version " + want
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
@ -100,13 +99,13 @@ func Check(runtime *env.Runtime) bool {
{ // check the MySQL character set and collation
if charset != "utf8" {
log.Error("MySQL character set not utf8:", errors.New(charset))
runtime.Log.Error("MySQL character set not utf8:", errors.New(charset))
web.SiteInfo.Issue = "MySQL character set not utf8: " + charset
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
if !strings.HasPrefix(collation, "utf8") {
log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
runtime.Log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
web.SiteInfo.Issue = "MySQL collation sequence not utf8...: " + collation
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
@ -118,13 +117,13 @@ func Check(runtime *env.Runtime) bool {
if err := runtime.Db.Select(&flds,
`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '`+web.SiteInfo.DBname+
`' and TABLE_TYPE='BASE TABLE'`); err != nil {
log.Error("Can't get MySQL number of tables", err)
runtime.Log.Error("Can't get MySQL number of tables", err)
web.SiteInfo.Issue = "Can't get MySQL number of tables: " + err.Error()
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
if strings.TrimSpace(flds[0]) == "0" {
log.Info("Entering database set-up mode because the database is empty.....")
runtime.Log.Info("Entering database set-up mode because the database is empty.....")
runtime.Flags.SiteMode = web.SiteModeSetup
return false
}
@ -139,7 +138,7 @@ func Check(runtime *env.Runtime) bool {
for _, table := range tables {
var dummy []string
if err := runtime.Db.Select(&dummy, "SELECT 1 FROM "+table+" LIMIT 1;"); err != nil {
log.Error("Entering bad database mode because: SELECT 1 FROM "+table+" LIMIT 1;", err)
runtime.Log.Error("Entering bad database mode because: SELECT 1 FROM "+table+" LIMIT 1;", err)
web.SiteInfo.Issue = "MySQL database is not empty, but does not contain table: " + table
runtime.Flags.SiteMode = web.SiteModeBadDB
return false