diff src/kaigo/horori/searcher/server/searcher.go @ 47:169936fed61b

small changes.
author pyon@macmini
date Tue, 21 Apr 2020 22:43:55 +0900
parents 6ec28d3c3e00
children ca00c4a85b98
line wrap: on
line diff
--- a/src/kaigo/horori/searcher/server/searcher.go	Sat Apr 18 21:10:29 2020 +0900
+++ b/src/kaigo/horori/searcher/server/searcher.go	Tue Apr 21 22:43:55 2020 +0900
@@ -1,10 +1,12 @@
  /*
-  Last Change: 2020-04-17 金 17:04:25.
+  Last Change: 2020-04-21 火 13:59:52.
  */
 
 package main
 
 import (
+    "bytes"
+    "compress/gzip"
 	"encoding/csv"
 	"fmt"
 	"io"
@@ -86,12 +88,13 @@
 	// Http-Handler
 	http.HandleFunc("/h/",  hhs_handler)      // Get /h/0800012345 -> name:addr:20200101#20210701#...
 	http.HandleFunc("/hn/", hhsnm_handler)    // Get /h/0800012345:0800098765:... -> name1:name2:...
-	http.HandleFunc("/ht/", hhstm_handler)    // Get /ht -> 20200314
+	http.HandleFunc("/ht/", hhstm_handler)   // Get /ht/ -> 2020-03-14 12:34
 	http.HandleFunc("/ha/", hhsdb_handler)    // Get /ha/ -> hhsdb.csv for Mover
 	http.HandleFunc("/i/",  image_handler)    // Get /i/20200110/0800012345.tgz
 	http.HandleFunc("/r/",  recent_handler)   // Get /r/0800012345:0800067890:0800099999:... -> 0800012345,name1,20200101:0800067890,name2,20210405:...
 	http.HandleFunc("/d/",  index_handler)    // Get /d/20xx -> 20xx0401:2020xx0408:... , /d/20xx0401 -> 0800012345:0800098765:...
-	http.HandleFunc("/u/",  uphhsdb_handler)  // Get /u/ -> ?
+	http.HandleFunc("/u/",  uphhsdb_handler)  // POST /u/
+	http.HandleFunc("/ui/", upimage_handler)  // POST /ui/20200401/0800012345.tgz
 
 	log.Fatal(http.ListenAndServe(server, nil))
 }
@@ -178,9 +181,13 @@
 	w.Write([]byte(strings.Join(buf, ":")))
 }
 
-/* Get /ht > 20200314 */
+/* Get /ht/ -> 2020-03-14 12:34 */
 func hhstm_handler(w http.ResponseWriter, r *http.Request) {
 	date := ""
+	if fi, err := os.Stat(hhsdb); err == nil {
+		t := fi.ModTime()
+		date = t.Format("2006-01-02 15:04")
+	}
 	w.Write([]byte(date))
 }
 
@@ -239,21 +246,78 @@
 	w.Write([]byte(buf[1:]))
 }
 
-/* /u */
+/* POST /u/ */
 func uphhsdb_handler(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodPost {
+		http.NotFound(w, r)
+		return
+	}
+
 	server_root := filepath.Dir(os.Args[0])
-	file := filepath.Join(server_root, "db", hhsdb)
+	file := filepath.Join(server_root, hhsdb)
 	f, err := os.Create(file)
 	if err != nil {
 		http.NotFound(w, r)
 		return
 	}
+	defer f.Close()
+
+    b, err := ioutil.ReadAll(r.Body)
+    r.Body.Close()
+	if err != nil {
+		http.NotFound(w, r)
+		return
+	}
+
+	br := bytes.NewReader(b)
+    zr, err := gzip.NewReader(br)
+	if err != nil {
+		http.NotFound(w, r)
+        return
+	}
+	n, err := io.Copy(f, zr)
+
+	if err := zr.Close(); err != nil {
+		http.NotFound(w, r)
+        return
+	}
+
+	w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
+
+	loadDB();
+}
+
+/* POST /ui/20200401/0800012345.tgz */
+func upimage_handler(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodPost {
+		http.NotFound(w, r)
+		return
+	}
+
+	uri := r.URL.Path[len("/ui/"):]
+	ymd := uri[0:8]
+	tgz := uri[9:]
+
+	server_root := filepath.Dir(os.Args[0])
+	dir := filepath.Join(server_root, "images", ymd)
+
+	if _, err := os.Stat(dir); os.IsNotExist(err) {
+		os.Mkdir(dir, 0644)
+	}
+
+	file := filepath.Join(server_root, "images", ymd, tgz)
+	f, err := os.Create(file)
+	if err != nil {
+		http.NotFound(w, r)
+		return
+	}
+	defer f.Close()
+
 	n, err := io.Copy(f, r.Body)
 	if err != nil {
 		http.NotFound(w, r)
 		return
 	}
-	f.Close()
 	w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
 }