comparison 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
comparison
equal deleted inserted replaced
50:638e1ad05cae 51:4e14902379da
1 /* 1 /*
2 Last Change: 2020-04-23 木 14:43:54. 2 Last Change: 2020-05-01 金 10:46:51.
3 */ 3 */
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
18 "os" 18 "os"
19 "path/filepath" 19 "path/filepath"
20 "sort" 20 "sort"
21 "strconv" 21 "strconv"
22 "strings" 22 "strings"
23 "time"
23 ) 24 )
24 25
25 type hhs struct { 26 type hhs struct {
26 No string 27 No string
27 //Birth string 28 //Birth string
54 pwdb string 55 pwdb string
55 server_root string 56 server_root string
56 hhash map[string]hhs 57 hhash map[string]hhs
57 iymdhash map[string]string 58 iymdhash map[string]string
58 iyhash map[string]string 59 iyhash map[string]string
60 logfile string
59 ) 61 )
60 62
61 func init() { 63 func init() {
62 port = ":3910" 64 port = ":3910"
63 hhsdb = "hhsdb.csv" 65 hhsdb = "hhsdb.csv"
64 indexdb = "index.csv" 66 indexdb = "index.csv"
65 pwdb = "passwd" 67 pwdb = "passwd"
68 logfile = "searcher.log"
66 } 69 }
67 70
68 func main() { 71 func main() {
69 server_root = filepath.Dir(os.Args[0]) 72 server_root = filepath.Dir(os.Args[0])
70 hhsdb = filepath.Join(server_root, hhsdb) 73 hhsdb = filepath.Join(server_root, hhsdb)
167 } 170 }
168 171
169 return nil 172 return nil
170 } 173 }
171 174
175 func write_errlog(no int, msg string) {
176 log := filepath.Join(server_root, logfile)
177 t := time.Now().Format("2006-01-02 15:04")
178 msg = fmt.Sprintf("%s [%02d] %s\n", t, no, msg)
179 f, _ := os.OpenFile(log, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
180 f.Write([]byte(msg))
181 f.Close()
182 }
183
172 /** HTTP HANDLERS **/ 184 /** HTTP HANDLERS **/
173 /* Get /h/0800012345 -> name:addr:20200101#20210701#... */ 185 /* Get /h/0800012345 -> name:addr:20200101#20210701#... */
174 func hhs_handler(w http.ResponseWriter, r *http.Request) { 186 func hhs_handler(w http.ResponseWriter, r *http.Request) {
175 hno := r.URL.Path[len("/h/"):] 187 hno := r.URL.Path[len("/h/"):]
176 s := "" 188 s := ""
228 file := r.URL.Path[len("/i/"):] 240 file := r.URL.Path[len("/i/"):]
229 file = filepath.Join(server_root, "images", filepath.FromSlash(file)) 241 file = filepath.Join(server_root, "images", filepath.FromSlash(file))
230 242
231 f, err := os.Open(file) 243 f, err := os.Open(file)
232 if err != nil { 244 if err != nil {
245 write_errlog(1, "cannot open " + file)
233 http.NotFound(w, r) 246 http.NotFound(w, r)
234 return 247 return
235 } 248 }
236 defer f.Close() 249 defer f.Close()
237 250
273 } 286 }
274 287
275 /* POST /u/ */ 288 /* POST /u/ */
276 func uphhsdb_handler(w http.ResponseWriter, r *http.Request) { 289 func uphhsdb_handler(w http.ResponseWriter, r *http.Request) {
277 if r.Method != http.MethodPost { 290 if r.Method != http.MethodPost {
278 http.NotFound(w, r) 291 write_errlog(10, "not post method")
279 return 292 http.NotFound(w, r)
280 } 293 return
281 294 }
282 file := filepath.Join(server_root, hhsdb) 295
283 f, err := os.Create(file) 296 f, err := os.Create(hhsdb)
284 if err != nil { 297 if err != nil {
298 write_errlog(11, "cannot create " + hhsdb)
285 http.NotFound(w, r) 299 http.NotFound(w, r)
286 return 300 return
287 } 301 }
288 defer f.Close() 302 defer f.Close()
289 303
290 b, err := ioutil.ReadAll(r.Body) 304 b, err := ioutil.ReadAll(r.Body)
291 r.Body.Close() 305 r.Body.Close()
292 if err != nil { 306 if err != nil {
307 write_errlog(12, "cannot read req-body")
293 http.NotFound(w, r) 308 http.NotFound(w, r)
294 return 309 return
295 } 310 }
296 311
297 br := bytes.NewReader(b) 312 br := bytes.NewReader(b)
298 zr, err := gzip.NewReader(br) 313 zr, err := gzip.NewReader(br)
299 if err != nil { 314 if err != nil {
315 write_errlog(13, "gzip-reader error")
300 http.NotFound(w, r) 316 http.NotFound(w, r)
301 return 317 return
302 } 318 }
303 n, err := io.Copy(f, zr) 319 n, err := io.Copy(f, zr)
304 320
305 if err := zr.Close(); err != nil { 321 if err := zr.Close(); err != nil {
322 write_errlog(14, "gzip-reader error")
306 http.NotFound(w, r) 323 http.NotFound(w, r)
307 return 324 return
308 } 325 }
309 326
310 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n))) 327 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
313 } 330 }
314 331
315 /* POST /ui/20200401/0800012345.tgz */ 332 /* POST /ui/20200401/0800012345.tgz */
316 func upimage_handler(w http.ResponseWriter, r *http.Request) { 333 func upimage_handler(w http.ResponseWriter, r *http.Request) {
317 if r.Method != http.MethodPost { 334 if r.Method != http.MethodPost {
335 write_errlog(20, "not post method")
318 http.NotFound(w, r) 336 http.NotFound(w, r)
319 return 337 return
320 } 338 }
321 339
322 uri := r.URL.Path[len("/ui/"):] 340 uri := r.URL.Path[len("/ui/"):]
330 } 348 }
331 349
332 file := filepath.Join(server_root, "images", ymd, tgz) 350 file := filepath.Join(server_root, "images", ymd, tgz)
333 f, err := os.Create(file) 351 f, err := os.Create(file)
334 if err != nil { 352 if err != nil {
353 write_errlog(21, "cannot create " + file)
335 http.NotFound(w, r) 354 http.NotFound(w, r)
336 return 355 return
337 } 356 }
338 defer f.Close() 357 defer f.Close()
339 358
340 n, err := io.Copy(f, r.Body) 359 n, err := io.Copy(f, r.Body)
341 if err != nil { 360 if err != nil {
361 write_errlog(22, "cannot copy req-body")
342 http.NotFound(w, r) 362 http.NotFound(w, r)
343 return 363 return
344 } 364 }
345 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n))) 365 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
346 } 366 }
408 428
409 /* Get /pw/ -> id1:pw1:id2:pw2:... */ 429 /* Get /pw/ -> id1:pw1:id2:pw2:... */
410 func pw_handler(w http.ResponseWriter, r *http.Request) { 430 func pw_handler(w http.ResponseWriter, r *http.Request) {
411 b, err := ioutil.ReadFile(pwdb) 431 b, err := ioutil.ReadFile(pwdb)
412 if err != nil { 432 if err != nil {
433 write_errlog(30, "cannot read passwd-file")
413 http.NotFound(w, r) 434 http.NotFound(w, r)
414 return 435 return
415 } 436 }
416 w.Write([]byte(strings.ReplaceAll(string(b), "\n", ":"))) 437 w.Write([]byte(strings.ReplaceAll(string(b), "\n", ":")))
417 } 438 }