Add basic html/js to search for and display some books.
[quanweb.git] / main / handler.go
index 255f19397a6fd6db357bae249a3d882f360e32f7..8686f2b3a870f051e8a1c096af01db4cde04c79e 100644 (file)
@@ -48,6 +48,28 @@ func handler(w http.ResponseWriter, r *http.Request) {
   }
 }
 
+/*
+func handleApp(w http.ResponseWriter, r *http.Request) {
+  fmt.Println("handleApp():", r.URL.Path)
+
+  // Security check:  prevent walking up the directory
+  pos := strings.Index(r.Url.Path, "../")
+  if (-1) == pos {
+    fmt.Fprintln(w, "Paths containing \"../\" are not permitted:", r.URL.Path)
+    return
+  }
+
+  fileName := "../app" + r.URL.Path
+  _, err := os.Stat(fileName)
+  if nil != err { 
+    fmt.Fprintln(w, "Failed to find file:", fileName, err)
+    return
+  }
+
+  http.ServeFile(w, r, "../app/" + r.URL.Path[1:])
+}
+*/
+
 func handleDownload(w http.ResponseWriter, r *http.Request) {
   path := r.URL.Path[1:]
   tok := strings.Split(path, "/")
@@ -90,8 +112,8 @@ func handleInfo(w http.ResponseWriter, r *http.Request) {
   idParam := idParams[0]
   idStrings := strings.Split(idParam, ",")
   ids := make([]int, len(idStrings))
+  var err error
   for i, v := range(idStrings) {
-    var err error
     ids[i], err = strconv.Atoi(v)
     if nil != err {
       ids[i] = 0
@@ -101,7 +123,6 @@ func handleInfo(w http.ResponseWriter, r *http.Request) {
   books := queryBooksByIds(ids)
 
   var jsonValue []byte
-  var err error
   jsonValue, err = json.Marshal(books)
   if nil != err {
     fmt.Fprintln(w, "ERROR!", err)
@@ -111,6 +132,9 @@ func handleInfo(w http.ResponseWriter, r *http.Request) {
 }
 
 func handleSearch(w http.ResponseWriter, r *http.Request) {
+  var err error
+  fmt.Println("DEBUG:  handleSearch():  " + r.URL.Path)
+
   fields := []Field{Author, Title, Series}
 
   terms := make([]SearchTerm, len(fields))
@@ -120,8 +144,9 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
     paramName := fv.String()
     paramValues := r.Form[paramName]
     for _, pv := range(paramValues) {
+      fmt.Println("DEBUG:  handleSearch():  ", paramName, "=", pv)
       if count >= len(terms) {
-        fmt.Printf("WARNING:  limit of %v search terms exceeded.  One or more terms ignored.")
+        fmt.Printf("WARNING:  limit of %d search terms exceeded.  One or more terms ignored.", len(terms))
         break
       }
       terms[count] = SearchTerm{Attribute:fv, Text:pv}
@@ -140,3 +165,4 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
     w.Write(jsonValue)
   }
 }
+