Browse Source

Optimization

Vova Tkach 6 years ago
parent
commit
7838ab4173
2 changed files with 30 additions and 23 deletions
  1. 3 0
      .gitignore
  2. 27 23
      engine/engine.go

+ 3 - 0
.gitignore

@@ -28,3 +28,6 @@
 
 # Default host mysql config file
 /hosts/localhost/config/mysql.json
+
+# All else
+/hosts/localhost/config/.installed

+ 27 - 23
engine/engine.go

@@ -78,33 +78,37 @@ func (this *Engine) Process() bool {
 	}
 	defer this.Wrap.DB.Close()
 
-	// ---
-	// TODO: Very expensive operation, optimize that by file flag
 	// Show add first user form if no any user in database
-	var count int
-	err = this.Wrap.DB.QueryRow(`
-		SELECT
-			COUNT(*)
-		FROM
-			users
-		;`,
-	).Scan(
-		&count,
-	)
-	if err != nil {
-		utils.SystemErrorPageEngine(this.Wrap.W, err)
-		return true
-	}
-	if count <= 0 {
-		// Redirect
-		if this.redirectToCP() {
+	if !utils.IsFileExists(this.Wrap.DConfig + string(os.PathSeparator) + ".installed") {
+		var count int
+		err = this.Wrap.DB.QueryRow(`
+			SELECT
+				COUNT(*)
+			FROM
+				users
+			;`,
+		).Scan(
+			&count,
+		)
+		if err != nil {
+			utils.SystemErrorPageEngine(this.Wrap.W, err)
 			return true
 		}
-		// Show first user form
-		utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpFirstUser, nil)
-		return true
+		if count <= 0 {
+			// Redirect
+			if this.redirectToCP() {
+				return true
+			}
+			// Show first user form
+			utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpFirstUser, nil)
+			return true
+		} else {
+			// Mark what one or more users already exists
+			if _, err := os.OpenFile(this.Wrap.DConfig+string(os.PathSeparator)+".installed", os.O_RDONLY|os.O_CREATE, 0666); err != nil {
+				this.Wrap.LogError(err.Error())
+			}
+		}
 	}
-	// ---
 
 	// Show login page if need
 	if this.Wrap.S.GetInt("UserId", 0) <= 0 {