Mercurial > mercurial > hgweb_golang.cgi
comparison src/kaigo/horori/searcher/server/searcher.go @ 65:0369656be06c default tip
many changes.
| author | pyon@macmini |
|---|---|
| date | Fri, 20 May 2022 06:30:34 +0900 |
| parents | 7396e7407abd |
| children |
comparison
equal
deleted
inserted
replaced
| 64:ad5c30ee5cf1 | 65:0369656be06c |
|---|---|
| 1 /* | 1 /* |
| 2 Last Change: 2020-06-23 火 15:48:49. | 2 Last Change: 2022-05-16 月 09:34:18. |
| 3 */ | 3 */ |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 9 "bytes" | 9 "bytes" |
| 10 "compress/gzip" | 10 "compress/gzip" |
| 11 "encoding/csv" | 11 "encoding/csv" |
| 12 "fmt" | 12 "fmt" |
| 13 "io" | 13 "io" |
| 14 "io/ioutil" | |
| 15 "log" | 14 "log" |
| 16 "net" | 15 "net" |
| 17 "net/http" | 16 "net/http" |
| 18 "os" | 17 "os" |
| 19 "path/filepath" | 18 "path/filepath" |
| 99 http.HandleFunc("/hn/", hhsnm_handler) // Get /hn/0800012345:0800098765:... -> name1:name2:... | 98 http.HandleFunc("/hn/", hhsnm_handler) // Get /hn/0800012345:0800098765:... -> name1:name2:... |
| 100 http.HandleFunc("/ht/", hhstm_handler) // Get /ht/ -> 2020-03-14 12:34 (2020-04-02) | 99 http.HandleFunc("/ht/", hhstm_handler) // Get /ht/ -> 2020-03-14 12:34 (2020-04-02) |
| 101 http.HandleFunc("/ha/", hhsdb_handler) // Get /ha/ -> hhsdb.csv for Mover | 100 http.HandleFunc("/ha/", hhsdb_handler) // Get /ha/ -> hhsdb.csv for Mover |
| 102 http.HandleFunc("/i/", image_handler) // Get /i/20200110/0800012345.tgz | 101 http.HandleFunc("/i/", image_handler) // Get /i/20200110/0800012345.tgz |
| 103 http.HandleFunc("/r/", recent_handler) // Get /r/0800012345:0800067890:0800099999:... -> 0800012345,name1,20200101:0800067890,name2,20210405:... | 102 http.HandleFunc("/r/", recent_handler) // Get /r/0800012345:0800067890:0800099999:... -> 0800012345,name1,20200101:0800067890,name2,20210405:... |
| 104 http.HandleFunc("/d/", index_handler) // Get /d/20xx -> 20xx0401:20xx0408:... , /d/20xx0401 -> 0800012345:0800098765:... | 103 http.HandleFunc("/d/", index_handler) // Get /d/ -> index.csv, /d/20xx -> 20xx0401:20xx0408:... , /d/20xx0401 -> 0800012345:0800098765:... |
| 105 http.HandleFunc("/dt/", indextm_handler) // Get /dt/ -> 2020-03-14 12:34 (2020-04-02) | 104 http.HandleFunc("/dt/", indextm_handler) // Get /dt/ -> 2020-03-14 12:34 (2020-04-02) |
| 106 http.HandleFunc("/hd/", hhsdbidx_handler)// Get /hd/ -> 20010401,0800012345,name1\n20010401,0300011111,name2\n... | 105 http.HandleFunc("/hd/", hhsdbidx_handler)// Get /hd/ -> 20010401,0800012345,name1\n20010401,0300011111,name2\n... |
| 107 http.HandleFunc("/ud/", upidx_handler) // Get /ud/20200402 | 106 http.HandleFunc("/ud/", upidx_handler) // Get /ud/20200402 |
| 108 http.HandleFunc("/u/", uphhsdb_handler) // POST /u/ | 107 http.HandleFunc("/u/", uphhsdb_handler) // POST /u/ |
| 109 http.HandleFunc("/ui/", upimage_handler) // POST /ui/20200401/0800012345.tgz | 108 http.HandleFunc("/ui/", upimage_handler) // POST /ui/20200401/0800012345.tgz |
| 117 func loadDB() error { | 116 func loadDB() error { |
| 118 hhash = make(map[string]hhs) | 117 hhash = make(map[string]hhs) |
| 119 iymdhash = make(map[string]string) | 118 iymdhash = make(map[string]string) |
| 120 iyhash = make(map[string]string) | 119 iyhash = make(map[string]string) |
| 121 | 120 |
| 122 b, err := ioutil.ReadFile(hhsdb) | 121 b, err := os.ReadFile(hhsdb) |
| 123 if err != nil { | 122 if err != nil { |
| 124 return err | 123 return err |
| 125 } | 124 } |
| 126 r := csv.NewReader(strings.NewReader(string(b))) | 125 r := csv.NewReader(strings.NewReader(string(b))) |
| 127 for { | 126 for { |
| 138 Addr: record[4], //Sex: record[5], | 137 Addr: record[4], //Sex: record[5], |
| 139 } | 138 } |
| 140 hhash[record[0]] = h | 139 hhash[record[0]] = h |
| 141 } | 140 } |
| 142 | 141 |
| 143 b, err = ioutil.ReadFile(indexdb) | 142 b, err = os.ReadFile(indexdb) |
| 144 if err != nil { | 143 if err != nil { |
| 145 return err | 144 return err |
| 146 } | 145 } |
| 147 r = csv.NewReader(strings.NewReader(string(b))) | 146 r = csv.NewReader(strings.NewReader(string(b))) |
| 148 for { | 147 for { |
| 230 w.Write([]byte(date)) | 229 w.Write([]byte(date)) |
| 231 } | 230 } |
| 232 | 231 |
| 233 /* Get /ha/ -> hhsdb.csv for Mover */ | 232 /* Get /ha/ -> hhsdb.csv for Mover */ |
| 234 func hhsdb_handler(w http.ResponseWriter, r *http.Request) { | 233 func hhsdb_handler(w http.ResponseWriter, r *http.Request) { |
| 235 b, _ := ioutil.ReadFile(hhsdb) | 234 b, _ := os.ReadFile(hhsdb) |
| 236 w.Write(b) | 235 w.Write(b) |
| 237 } | 236 } |
| 238 | 237 |
| 239 /* Get /i/20200110/0800012345.tgz */ | 238 /* Get /i/20200110/0800012345.tgz */ |
| 240 func image_handler(w http.ResponseWriter, r *http.Request) { | 239 func image_handler(w http.ResponseWriter, r *http.Request) { |
| 271 buf = append(buf, s) | 270 buf = append(buf, s) |
| 272 } | 271 } |
| 273 w.Write([]byte(strings.Join(buf, ":"))) | 272 w.Write([]byte(strings.Join(buf, ":"))) |
| 274 } | 273 } |
| 275 | 274 |
| 276 /* Get /d/20xx -> 20xx0401:20xx0408:... , /d/20xx0401 -> 0800012345:0800098765:... */ | 275 /* Get /d/ -> index.csv, /d/20xx -> 20xx0401:20xx0408:... , /d/20xx0401 -> 0800012345:0800098765:... */ |
| 277 func index_handler(w http.ResponseWriter, r *http.Request) { | 276 func index_handler(w http.ResponseWriter, r *http.Request) { |
| 277 ymd := r.URL.Path[len("/d/"):] | |
| 278 | |
| 279 if len(ymd) == 0 { | |
| 280 b, _ := os.ReadFile(indexdb) | |
| 281 w.Write(b) | |
| 282 return | |
| 283 } | |
| 284 | |
| 278 var buf string | 285 var buf string |
| 279 ymd := r.URL.Path[len("/d/"):] | |
| 280 if len(ymd) == 4 { | 286 if len(ymd) == 4 { |
| 281 buf = iyhash[ymd] | 287 buf = iyhash[ymd] |
| 282 } | 288 } else if len(ymd) == 8 { |
| 283 if len(ymd) == 8 { | |
| 284 buf = iymdhash[ymd] | 289 buf = iymdhash[ymd] |
| 285 } | 290 } |
| 286 w.Write([]byte(buf[1:])) | 291 w.Write([]byte(buf[1:])) |
| 287 } | 292 } |
| 288 | 293 |
| 300 http.NotFound(w, r) | 305 http.NotFound(w, r) |
| 301 return | 306 return |
| 302 } | 307 } |
| 303 defer f.Close() | 308 defer f.Close() |
| 304 | 309 |
| 305 b, err := ioutil.ReadAll(r.Body) | 310 b, err := io.ReadAll(r.Body) |
| 306 r.Body.Close() | 311 r.Body.Close() |
| 307 if err != nil { | 312 if err != nil { |
| 308 write_errlog(12, "cannot read req-body") | 313 write_errlog(12, "cannot read req-body") |
| 309 http.NotFound(w, r) | 314 http.NotFound(w, r) |
| 310 return | 315 return |
| 390 } | 395 } |
| 391 | 396 |
| 392 /* Get /hd/ -> 20010401,0800012345,name1\n20010401,0300011111,name2\n... */ | 397 /* Get /hd/ -> 20010401,0800012345,name1\n20010401,0300011111,name2\n... */ |
| 393 func hhsdbidx_handler(w http.ResponseWriter, r *http.Request) { | 398 func hhsdbidx_handler(w http.ResponseWriter, r *http.Request) { |
| 394 s := "" | 399 s := "" |
| 395 b, err := ioutil.ReadFile(indexdb) | 400 b, err := os.ReadFile(indexdb) |
| 396 if err != nil { | 401 if err != nil { |
| 397 return | 402 return |
| 398 } | 403 } |
| 399 rd := csv.NewReader(strings.NewReader(string(b))) | 404 rd := csv.NewReader(strings.NewReader(string(b))) |
| 400 for { | 405 for { |
| 424 } | 429 } |
| 425 } | 430 } |
| 426 f.Close() | 431 f.Close() |
| 427 | 432 |
| 428 imgdir := filepath.Join(server_root, "images", date) | 433 imgdir := filepath.Join(server_root, "images", date) |
| 429 files, _ := ioutil.ReadDir(imgdir) | 434 files, _ := os.ReadDir(imgdir) |
| 430 for _, file := range files { | 435 for _, file := range files { |
| 431 i := file.Name()[0:10] + "," + date | 436 i := file.Name()[0:10] + "," + date |
| 432 buf = append(buf, i) | 437 buf = append(buf, i) |
| 433 } | 438 } |
| 434 sort.Sort(sort.Reverse(sort.StringSlice(buf))) | 439 sort.Sort(sort.Reverse(sort.StringSlice(buf))) |
| 435 | 440 |
| 436 os.Remove(indexdb) | 441 os.Remove(indexdb) |
| 437 | 442 |
| 438 s := strings.Join(buf, "\n") | 443 s := strings.Join(buf, "\n") |
| 439 ioutil.WriteFile(indexdb, []byte(s), 0644) | 444 os.WriteFile(indexdb, []byte(s), 0644) |
| 440 | 445 |
| 441 loadDB(); | 446 loadDB(); |
| 442 w.Write([]byte("update index done.")) | 447 w.Write([]byte("update index done.")) |
| 443 } | 448 } |
| 444 | 449 |
| 449 os.RemoveAll(dir) | 454 os.RemoveAll(dir) |
| 450 } | 455 } |
| 451 | 456 |
| 452 /* Get /pw/ -> id1:pw1:id2:pw2:... */ | 457 /* Get /pw/ -> id1:pw1:id2:pw2:... */ |
| 453 func pw_handler(w http.ResponseWriter, r *http.Request) { | 458 func pw_handler(w http.ResponseWriter, r *http.Request) { |
| 454 b, err := ioutil.ReadFile(pwdb) | 459 b, err := os.ReadFile(pwdb) |
| 455 if err != nil { | 460 if err != nil { |
| 456 write_errlog(30, "cannot read passwd-file") | 461 write_errlog(30, "cannot read passwd-file") |
| 457 http.NotFound(w, r) | 462 http.NotFound(w, r) |
| 458 return | 463 return |
| 459 } | 464 } |
