Browse Source

Display error if localhost dir not exists

Vova Tkach 6 years ago
parent
commit
221a40df84
3 changed files with 21 additions and 13 deletions
  1. 1 1
      engine/wrapper/static.go
  2. 1 1
      engine/wrapper/wrapper.go
  3. 19 11
      main.go

+ 1 - 1
engine/wrapper/static.go

@@ -193,7 +193,7 @@ func (this *Wrapper) printTmplPageError(perr error) {
 	})
 	})
 }
 }
 
 
-func (this *Wrapper) printEnginePageError(perr error) {
+func (this *Wrapper) PrintEnginePageError(perr error) {
 	tmpl, err := template.New("template").Parse(string(templates.PageEngError))
 	tmpl, err := template.New("template").Parse(string(templates.PageEngError))
 	if err != nil {
 	if err != nil {
 		(*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 		(*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")

+ 1 - 1
engine/wrapper/wrapper.go

@@ -212,7 +212,7 @@ func (this *Wrapper) TmplBackEnd(tcont []byte, data interface{}) bool {
 
 
 func (this *Wrapper) EngineErrMsgOnError(err error) bool {
 func (this *Wrapper) EngineErrMsgOnError(err error) bool {
 	if err != nil {
 	if err != nil {
-		this.printEnginePageError(err)
+		this.PrintEnginePageError(err)
 		return true
 		return true
 	}
 	}
 	return false
 	return false

+ 19 - 11
main.go

@@ -2,6 +2,7 @@ package main
 
 
 import (
 import (
 	"context"
 	"context"
+	"errors"
 	"flag"
 	"flag"
 	"fmt"
 	"fmt"
 	"log"
 	"log"
@@ -114,17 +115,24 @@ func handler(w http.ResponseWriter, r *http.Request) {
 	// Set server name
 	// Set server name
 	w.Header().Set("Server", "fave.pro/"+constants.ServerVersion)
 	w.Header().Set("Server", "fave.pro/"+constants.ServerVersion)
 
 
+	wrap := wrapper.New(&w, r, host, port, ParamWwwDir, VhostHomeDir)
+
+	// Check if localhost exists
+	if !vhExists(VhostHomeDir) && !strings.HasPrefix(r.URL.Path, "/assets/") {
+		wrap.PrintEnginePageError(errors.New("Folder " + VhostHomeDir + " is not found"))
+		return
+	}
+
 	// Create and start engine
 	// Create and start engine
-	wrapper.New(&w, r, host, port, ParamWwwDir, VhostHomeDir).
-		Run(func(wrapper *wrapper.Wrapper) bool {
-			// Actions
-			action := actions.New(wrapper)
-			if action.Run() {
-				wrapper.Session.Save()
-				return true
-			}
+	wrap.Run(func(wrapper *wrapper.Wrapper) bool {
+		// Actions
+		action := actions.New(wrapper)
+		if action.Run() {
+			wrapper.Session.Save()
+			return true
+		}
 
 
-			// Pages
-			return handlerPage(wrapper)
-		})
+		// Pages
+		return handlerPage(wrapper)
+	})
 }
 }