Browse Source

Shop API, cache for products XML

Vova Tkach 5 years ago
parent
commit
06bfeadabc
2 changed files with 18 additions and 5 deletions
  1. 1 0
      .gitignore
  2. 17 5
      modules/module_api.go

+ 1 - 0
.gitignore

@@ -27,6 +27,7 @@
 /hosts/*
 !/hosts/localhost
 /hosts/*/htdocs/products
+/hosts/*/htdocs/products.xml
 
 # Default host mysql config file
 /hosts/localhost/config/mysql.json

+ 17 - 5
modules/module_api.go

@@ -29,11 +29,23 @@ func (this *Modules) RegisterModule_Api() *Module {
 					return
 				}
 
-				// XML
-				wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-				wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
-				wrap.W.WriteHeader(http.StatusOK)
-				wrap.W.Write([]byte(this.api_GenerateXml(wrap)))
+				target_file := wrap.DHtdocs + string(os.PathSeparator) + "products.xml"
+				if !utils.IsFileExists(target_file) {
+					// XML
+					data := []byte(this.api_GenerateXml(wrap))
+
+					// Save file
+					if file, err := os.Create(target_file); err == nil {
+						file.Write(data)
+					}
+
+					wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+					wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
+					wrap.W.WriteHeader(http.StatusOK)
+					wrap.W.Write(data)
+				} else {
+					http.ServeFile(wrap.W, wrap.R, target_file)
+				}
 			} else {
 				wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 				wrap.W.WriteHeader(http.StatusNotFound)