-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (179 loc) · 4.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"database/sql"
_ "embed"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
_ "github.com/mattn/go-sqlite3"
)
const appName = "zippia"
var Version = "v0.3.41"
//go:embed var/last-modified.txt
var lastModified []byte
//go:embed var/banner.txt
var banner []byte
//go:embed var/kenall.json
var kenall []byte
//go:embed var/jigyosyo.json
var jigyosyo []byte
func main() {
host := flag.String("host", "127.0.0.1", "host part of bind address.")
port := flag.String("port", "5000", "port part of bind address.")
showVersion := flag.Bool("v", false, "show version.")
showVersionFull := flag.Bool("vv", false, "show version and last modified date.")
flag.Parse()
if *showVersion {
fmt.Printf("%s %s\n", appName, strings.TrimSuffix(Version, "\n"))
return
}
if *showVersionFull {
fmt.Printf(
"%s %s\nZip code was last modified on \"%s\"\n",
appName,
strings.TrimSuffix(Version, "\n"),
strings.TrimSuffix(string(lastModified), "\n"),
)
return
}
db, err := initialize()
if err != nil {
log.Fatal(err)
}
defer db.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
zip := r.URL.Query().Get("zip")
zip = strings.Replace(zip, "-", "", 1)
stmt, err := db.Prepare("SELECT zip, pref, city, town, pref_kana, city_kana, town_kana, office, office_kana FROM address WHERE zip = ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
rows, err := stmt.Query(zip)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
list := []Address{}
for rows.Next() {
var row Address
err = rows.Scan(&row.Zip, &row.Pref, &row.City, &row.Town, &row.PrefKana, &row.CityKana, &row.TownKana, &row.Office, &row.OfficeKana)
if err != nil {
log.Println(err)
break
}
list = append(list, row)
}
body, err := json.Marshal(list)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(body))
})
addr := fmt.Sprintf("%s:%s", *host, *port)
log.Println(fmt.Sprintf(`start the Japanese zip code search API server.
%s
version %s
Zip code was last modified on "%s"
`, string(banner), string(Version), string(lastModified)))
log.Printf("http server started on http://%s\n", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
type Address struct {
Id int `json:"-"`
Zip string `json:"zip"`
Pref string `json:"pref"`
City string `json:"city"`
Town string `json:"town"`
Office string `json:"office"`
PrefKana string `json:"pref_kana"`
CityKana string `json:"city_kana"`
TownKana string `json:"town_kana"`
OfficeKana string `json:"office_kana"`
}
func initialize() (*sql.DB, error) {
log.Println("initializing database...")
db, err := sql.Open("sqlite3", "file:zip.db?cache=shared&mode=memory")
if err != nil {
return nil, err
}
initialTableSQL := `
DROP INDEX IF EXISTS zipcode;
DROP TABLE IF EXISTS address;
CREATE TABLE IF NOT EXISTS address (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
zip TEXT NOT NULL,
pref TEXT,
city TEXT,
town TEXT,
office TEXT,
pref_kana TEXT,
city_kana TEXT,
town_kana TEXT,
office_kana TEXT
);
CREATE INDEX zipcode ON address (zip);
`
_, err = db.Exec(initialTableSQL)
if err != nil {
return nil, err
}
for _, bytes := range [][]byte{kenall, jigyosyo} {
var list []Address
if err := json.Unmarshal(bytes, &list); err != nil {
return nil, err
}
const bufSize = 100
buf := []Address{}
for _, address := range list {
buf = append(buf, address)
if len(buf) >= bufSize {
err = flush(db, &buf)
if err != nil {
return nil, err
}
}
}
if len(buf) > 0 {
err = flush(db, &buf)
if err != nil {
return nil, err
}
}
}
db.SetConnMaxLifetime(-1)
log.Println("database initialization is completed!")
return db, nil
}
func flush(db *sql.DB, buf *[]Address) error {
insertSQL, args := createBulkInsertQuery(*buf)
*buf = []Address{}
stmt, err := db.Prepare(insertSQL)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(args...)
if err != nil {
return err
}
return nil
}
func createBulkInsertQuery(list []Address) (string, []interface{}) {
placeholder := []string{}
args := []interface{}{}
for _, row := range list {
placeholder = append(placeholder, "(?,?,?,?,?,?,?,?,?)")
args = append(args, row.Zip, row.Pref, row.City, row.Town, row.PrefKana, row.CityKana, row.TownKana, row.Office, row.OfficeKana)
}
query := fmt.Sprintf("INSERT INTO address (zip, pref, city, town, pref_kana, city_kana, town_kana, office, office_kana) VALUES %s", strings.Join(placeholder, ","))
return query, args
}