45
|
1 /*
|
|
2 Last Change: 2020-04-16 木 17:15:49.
|
|
3 */
|
|
4
|
|
5 package main
|
|
6
|
|
7 import (
|
|
8 "encoding/csv"
|
|
9 "fmt"
|
|
10 "io"
|
|
11 "io/ioutil"
|
|
12 "log"
|
|
13 "net"
|
|
14 "net/http"
|
|
15 "os"
|
|
16 "path/filepath"
|
|
17 "strconv"
|
|
18 "strings"
|
|
19 )
|
|
20
|
|
21 type hhs struct {
|
|
22 No string
|
|
23 //Birth string
|
|
24 Name string
|
|
25 //Kana string
|
|
26 Addr string
|
|
27 //Sex string
|
|
28 Ccn []string
|
|
29 }
|
|
30
|
|
31 func (h *hhs) GetData() string {
|
|
32 s := strings.Join(h.Ccn, "#")
|
|
33 s = strings.Join([]string{h.Name, h.Addr, s}, ":")
|
|
34 return s
|
|
35 }
|
|
36
|
|
37 func (h *hhs) GetRecent() string {
|
|
38 ccn := ""
|
|
39 if len(h.Ccn) > 0 {
|
|
40 ccn = h.Ccn[0]
|
|
41 }
|
|
42 return strings.Join([]string{h.No, h.Name, ccn}, ",")
|
|
43 }
|
|
44
|
|
45 var (
|
|
46 server string
|
|
47 port string
|
|
48 hhsdb string
|
|
49 img_root string
|
|
50 hhash map[string]hhs
|
|
51 iymdhash map[string]string
|
|
52 iyhash map[string]string
|
|
53 )
|
|
54
|
|
55 func init() {
|
|
56 port = ":3910"
|
|
57 hhsdb = "hhsdb.csv"
|
|
58 img_root = "./images"
|
|
59 hhash = make(map[string]hhs)
|
|
60 iymdhash = make(map[string]string)
|
|
61 iyhash = make(map[string]string)
|
|
62 }
|
|
63
|
|
64 func main() {
|
|
65
|
|
66 // setting IP-Address & Port
|
|
67 addrs, err := net.InterfaceAddrs()
|
|
68 if err != nil {
|
|
69 log.Fatal(err)
|
|
70 }
|
|
71 for _, a := range addrs {
|
|
72 if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
73 if strings.HasPrefix(ipnet.IP.String(), "169.254") {
|
|
74 continue
|
|
75 }
|
|
76 if ipnet.IP.To4() != nil {
|
|
77 server = ipnet.IP.String() + port
|
|
78 }
|
|
79 }
|
|
80 }
|
|
81
|
|
82 if err := loadDB(); err != nil {
|
|
83 log.Fatal(err)
|
|
84 }
|
|
85
|
|
86 // Http-Handler
|
|
87 http.HandleFunc("/h/", hhs_handler)
|
|
88 http.HandleFunc("/ha/", hhsdb_handler)
|
|
89 http.HandleFunc("/u/", uphhsdb_handler)
|
|
90 http.HandleFunc("/i/", image_handler)
|
|
91 http.HandleFunc("/r/", recent_handler)
|
|
92 http.HandleFunc("/d/", index_handler)
|
|
93
|
|
94 log.Fatal(http.ListenAndServe(server, nil))
|
|
95 }
|
|
96
|
|
97 func loadDB() error {
|
|
98 b, err := ioutil.ReadFile(hhsdb)
|
|
99 if err != nil {
|
|
100 return err
|
|
101 }
|
|
102 r := csv.NewReader(strings.NewReader(string(b)))
|
|
103 for {
|
|
104 record, err := r.Read()
|
|
105 if err == io.EOF {
|
|
106 break
|
|
107 }
|
|
108 if err != nil {
|
|
109 return err
|
|
110 }
|
|
111 h := hhs{
|
|
112 No: record[0],
|
|
113 //Birth: record[1],
|
|
114 Name: record[2],
|
|
115 //Kana: record[3],
|
|
116 Addr: record[4],
|
|
117 //Sex: record[5],
|
|
118 }
|
|
119 hhash[record[0]] = h
|
|
120 }
|
|
121
|
|
122 b, err = ioutil.ReadFile("index.csv")
|
|
123 if err != nil {
|
|
124 return err
|
|
125 }
|
|
126 r = csv.NewReader(strings.NewReader(string(b)))
|
|
127 for {
|
|
128 record, err := r.Read()
|
|
129 if err == io.EOF {
|
|
130 break
|
|
131 }
|
|
132 if err != nil {
|
|
133 return err
|
|
134 }
|
|
135 h := hhash[record[0]]
|
|
136 ccn := h.Ccn
|
|
137 h.Ccn = append(ccn, record[1])
|
|
138 hhash[record[0]] = h
|
|
139
|
|
140 iymdhash[record[1]] += ":" + record[0]
|
|
141 }
|
|
142
|
|
143 for ymd, _ := range iymdhash {
|
|
144 y := ymd[0:4]
|
|
145 if ymd[4:6] == "01" || ymd[4:6] == "02" || ymd[4:6] == "03" {
|
|
146 yy, _ := strconv.Atoi(y)
|
|
147 y = fmt.Sprintf("%d", yy - 1)
|
|
148 }
|
|
149 iyhash[y] += ":" + ymd
|
|
150 }
|
|
151
|
|
152 return nil
|
|
153 }
|
|
154
|
|
155 /* Get /h/0800012345 -> name:addr:ymd1#ymd2#... */
|
|
156 func hhs_handler(w http.ResponseWriter, r *http.Request) {
|
|
157 hno := r.URL.Path[len("/h/"):]
|
|
158 s := ""
|
|
159 if h, ok := hhash[hno]; ok {
|
|
160 s = h.GetData()
|
|
161 }
|
|
162 w.Write([]byte(s))
|
|
163 }
|
|
164
|
|
165 /* Get /ha/ -> hhsdb.csv for Mover */
|
|
166 func hhsdb_handler(w http.ResponseWriter, r *http.Request) {
|
|
167 b, _ := ioutil.ReadFile(hhsdb)
|
|
168 w.Write(b)
|
|
169 }
|
|
170
|
|
171 /* Get /i/20200110/0800012345.tgz */
|
|
172 func image_handler(w http.ResponseWriter, r *http.Request) {
|
|
173 file := r.URL.Path[len("/i/"):]
|
|
174 file = filepath.Join(img_root, filepath.FromSlash(file))
|
|
175
|
|
176 f, err := os.Open(file)
|
|
177 if err != nil {
|
|
178 http.NotFound(w, r)
|
|
179 return
|
|
180 }
|
|
181 defer f.Close()
|
|
182
|
|
183 fi, _ := f.Stat()
|
|
184
|
|
185 w.Header().Set("Content-Type", "rsearcher/octet-stream")
|
|
186 w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size()))
|
|
187 io.Copy(w, f)
|
|
188 }
|
|
189
|
|
190 /* Get /r/0800012345:0800067890:0800099999:... */
|
|
191 func recent_handler(w http.ResponseWriter, r *http.Request) {
|
|
192 hnolist := strings.Split(r.URL.Path[len("/r/"):], ":")
|
|
193 var buf []string
|
|
194 for _, hno := range hnolist {
|
|
195 s := hno + ",,"
|
|
196 if h, ok := hhash[hno]; ok {
|
|
197 s = h.GetRecent()
|
|
198 if h.No == "" {
|
|
199 s = hno + s
|
|
200 }
|
|
201 }
|
|
202 buf = append(buf, s)
|
|
203 }
|
|
204 w.Write([]byte(strings.Join(buf, ":")))
|
|
205 }
|
|
206
|
|
207 /* Get /d/20xx -> 20xx0401:2020xx0408:... , /d/20xx0401 -> 0800012345:0800098765:... */
|
|
208 func index_handler(w http.ResponseWriter, r *http.Request) {
|
|
209 var buf string
|
|
210 ymd := r.URL.Path[len("/d/"):]
|
|
211 if len(ymd) == 4 {
|
|
212 buf = iyhash[ymd]
|
|
213 }
|
|
214 if len(ymd) == 8 {
|
|
215 buf = iymdhash[ymd]
|
|
216 }
|
|
217 w.Write([]byte(buf[1:]))
|
|
218 }
|
|
219
|
|
220 func uphhsdb_handler(w http.ResponseWriter, r *http.Request) {
|
|
221 server_root := filepath.Dir(os.Args[0])
|
|
222 file := filepath.Join(server_root, "db", hhsdb)
|
|
223 f, err := os.Create(file)
|
|
224 if err != nil {
|
|
225 http.NotFound(w, r)
|
|
226 return
|
|
227 }
|
|
228 n, err := io.Copy(f, r.Body)
|
|
229 if err != nil {
|
|
230 http.NotFound(w, r)
|
|
231 return
|
|
232 }
|
|
233 f.Close()
|
|
234 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
|
|
235 }
|
|
236
|