Mercurial > mercurial > hgweb_golang.cgi
diff src/kaigo/horori/searcher/server/searcher.go @ 51:4e14902379da
first release.
author | pyon@macmini |
---|---|
date | Fri, 01 May 2020 23:45:22 +0900 |
parents | 8384ca56f1b4 |
children | 7396e7407abd |
line wrap: on
line diff
--- a/src/kaigo/horori/searcher/server/searcher.go Fri Apr 24 22:22:12 2020 +0900 +++ b/src/kaigo/horori/searcher/server/searcher.go Fri May 01 23:45:22 2020 +0900 @@ -1,5 +1,5 @@ /* - Last Change: 2020-04-23 木 14:43:54. + Last Change: 2020-05-01 金 10:46:51. */ package main @@ -20,6 +20,7 @@ "sort" "strconv" "strings" + "time" ) type hhs struct { @@ -56,6 +57,7 @@ hhash map[string]hhs iymdhash map[string]string iyhash map[string]string + logfile string ) func init() { @@ -63,6 +65,7 @@ hhsdb = "hhsdb.csv" indexdb = "index.csv" pwdb = "passwd" + logfile = "searcher.log" } func main() { @@ -169,6 +172,15 @@ return nil } +func write_errlog(no int, msg string) { + log := filepath.Join(server_root, logfile) + t := time.Now().Format("2006-01-02 15:04") + msg = fmt.Sprintf("%s [%02d] %s\n", t, no, msg) + f, _ := os.OpenFile(log, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + f.Write([]byte(msg)) + f.Close() +} + /** HTTP HANDLERS **/ /* Get /h/0800012345 -> name:addr:20200101#20210701#... */ func hhs_handler(w http.ResponseWriter, r *http.Request) { @@ -230,6 +242,7 @@ f, err := os.Open(file) if err != nil { + write_errlog(1, "cannot open " + file) http.NotFound(w, r) return } @@ -275,13 +288,14 @@ /* POST /u/ */ func uphhsdb_handler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { + write_errlog(10, "not post method") http.NotFound(w, r) return } - file := filepath.Join(server_root, hhsdb) - f, err := os.Create(file) + f, err := os.Create(hhsdb) if err != nil { + write_errlog(11, "cannot create " + hhsdb) http.NotFound(w, r) return } @@ -290,6 +304,7 @@ b, err := ioutil.ReadAll(r.Body) r.Body.Close() if err != nil { + write_errlog(12, "cannot read req-body") http.NotFound(w, r) return } @@ -297,12 +312,14 @@ br := bytes.NewReader(b) zr, err := gzip.NewReader(br) if err != nil { + write_errlog(13, "gzip-reader error") http.NotFound(w, r) return } n, err := io.Copy(f, zr) if err := zr.Close(); err != nil { + write_errlog(14, "gzip-reader error") http.NotFound(w, r) return } @@ -315,6 +332,7 @@ /* POST /ui/20200401/0800012345.tgz */ func upimage_handler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { + write_errlog(20, "not post method") http.NotFound(w, r) return } @@ -332,6 +350,7 @@ file := filepath.Join(server_root, "images", ymd, tgz) f, err := os.Create(file) if err != nil { + write_errlog(21, "cannot create " + file) http.NotFound(w, r) return } @@ -339,6 +358,7 @@ n, err := io.Copy(f, r.Body) if err != nil { + write_errlog(22, "cannot copy req-body") http.NotFound(w, r) return } @@ -410,6 +430,7 @@ func pw_handler(w http.ResponseWriter, r *http.Request) { b, err := ioutil.ReadFile(pwdb) if err != nil { + write_errlog(30, "cannot read passwd-file") http.NotFound(w, r) return }