Browse Source

Default error page if logic not response

Vova Tkach 6 years ago
parent
commit
52a5201c83
3 changed files with 26 additions and 13 deletions
  1. 1 0
      engine/engine.go
  2. 7 10
      main.go
  3. 18 3
      utils/utils.go

+ 1 - 0
engine/engine.go

@@ -0,0 +1 @@
+package engine

+ 7 - 10
main.go

@@ -77,23 +77,23 @@ func main() {
 		vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
 
 		if !utils.IsHostDirExists(vhost_dir_config) {
-			utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_config+" is not found"))
+			utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_config+" is not found"))
 			return
 		}
 		if !utils.IsHostDirExists(vhost_dir_htdocs) {
-			utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
+			utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
 			return
 		}
 		if !utils.IsHostDirExists(vhost_dir_logs) {
-			utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
+			utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
 			return
 		}
 		if !utils.IsHostDirExists(vhost_dir_template) {
-			utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_template+" is not found"))
+			utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_template+" is not found"))
 			return
 		}
 		if !utils.IsHostDirExists(vhost_dir_tmp) {
-			utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
+			utils.SystemErrorPageEngine(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
 			return
 		}
 
@@ -118,13 +118,10 @@ func main() {
 		}
 
 		// Error 404
-		// TODO: display default template
-		w.WriteHeader(http.StatusNotFound)
-		w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-		w.Header().Set("Content-Type", "text/html")
-		w.Write([]byte(`404`))
+		utils.SystemErrorPage404(w)
 	})
 
+	// TODO: call it in background time by time
 	// Delete expired session files
 	// session.Clean("./tmp")
 }

+ 18 - 3
utils/utils.go

@@ -86,10 +86,10 @@ func GetTmplError(err error) consts.TmplError {
 	}
 }
 
-func SystemErrorPage(w http.ResponseWriter, err error) {
+func SystemErrorPageEngine(w http.ResponseWriter, err error) {
 	if tmpl, errr := template.New("template").Parse(string(assets.TmplPageErrorEngine)); errr == nil {
-		w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 		w.WriteHeader(http.StatusInternalServerError)
+		w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 		w.Header().Set("Content-Type", "text/html")
 		tmpl.Execute(w, consts.TmplData{
 			System: GetTmplSystemData(),
@@ -97,9 +97,24 @@ func SystemErrorPage(w http.ResponseWriter, err error) {
 		})
 		return
 	}
-	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 	w.WriteHeader(http.StatusInternalServerError)
+	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 	w.Header().Set("Content-Type", "text/html")
 	w.Write([]byte("<h1>Critical engine error</h1>"))
 	w.Write([]byte("<h2>" + err.Error() + "</h2>"))
 }
+
+func SystemErrorPage404(w http.ResponseWriter) {
+	tmpl, err := template.New("template").Parse(string(assets.TmplPageError404))
+	if err != nil {
+		SystemErrorPageEngine(w, err)
+		return
+	}
+	w.WriteHeader(http.StatusNotFound)
+	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+	w.Header().Set("Content-Type", "text/html")
+	tmpl.Execute(w, consts.TmplData{
+		System: GetTmplSystemData(),
+		Data:   nil,
+	})
+}