Browse Source

Serve robots.txt and styles.css files from template folder

Vova Tkach 6 years ago
parent
commit
70d942d339
1 changed files with 25 additions and 0 deletions
  1. 25 0
      main.go

+ 25 - 0
main.go

@@ -141,6 +141,14 @@ func main() {
 			return
 		}
 
+		// Robots.txt and styles.css from template dir
+		if ServeTemplateFile(w, r, "robots.txt", vhost_dir_template) {
+			return
+		}
+		if ServeTemplateFile(w, r, "styles.css", vhost_dir_template) {
+			return
+		}
+
 		// Session
 		sess := session.New(w, r, vhost_dir_tmp)
 		defer sess.Close()
@@ -156,3 +164,20 @@ func main() {
 		s.SetKeepAlivesEnabled(consts.ParamKeepAlive)
 	})
 }
+
+func ServeTemplateFile(w http.ResponseWriter, r *http.Request, file string, dir string) bool {
+	if r.URL.Path == "/"+file {
+		f, err := os.Open(dir + string(os.PathSeparator) + file)
+		if err == nil {
+			defer f.Close()
+			st, err := os.Stat(dir + string(os.PathSeparator) + file)
+			if err == nil {
+				if !st.Mode().IsDir() {
+					http.ServeFile(w, r, dir+string(os.PathSeparator)+file)
+					return true
+				}
+			}
+		}
+	}
+	return false
+}