-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
188 lines (161 loc) · 4.42 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"github.com/upamune/amazing"
)
type service struct {
client *amazing.Amazing
cacheDir string
}
func main() {
var port, cacheDir string
var awsAccess, awsSecret, awsTag, awsDomain string
flag.StringVar(&awsAccess, "access", "", "aws access id")
flag.StringVar(&awsSecret, "secret", "", "aws secretkey")
flag.StringVar(&awsTag, "tag", "", "amazon tag")
flag.StringVar(&awsDomain, "domain", "JP", "amazon domain")
flag.StringVar(&port, "port", "8080", "port number")
flag.StringVar(&cacheDir, "cache-dir", "", "for json cache")
flag.Parse()
client, err := amazing.NewAmazing(awsDomain, awsTag, awsAccess, awsSecret)
if err != nil {
log.Fatal(err)
}
service := &service{
client: client,
cacheDir: cacheDir,
}
http.HandleFunc("/hc", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/", service.amazonHandler)
log.Printf("Starting Server at %s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
type Item struct {
ASIN string
Brand string
Creator string
Manufacturer string
Publisher string
ReleaseDate string
Studio string
Title string
URL string
SmallImage string
MediumImage string
LargeImage string
}
var ErrNotFoundFile = errors.New("file not found")
func (s *service) getItemFromCache(itemID string) ([]byte, error) {
filename := s.getFileName(itemID)
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, ErrNotFoundFile
}
return b, nil
}
func (s *service) getFileName(itemID string) string {
return fmt.Sprintf("%s/%s", s.cacheDir, itemID)
}
func (s *service) saveItemToCache(item *Item) error {
f, err := os.OpenFile(s.getFileName(item.ASIN), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(item)
}
func (s *service) amazonHandler(w http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid form: %v", err)))
return
}
itemID := req.FormValue("item_id")
if itemID == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid item id: %s", itemID)))
return
}
// キャッシュされていたらキャッシュを返す
if s.cacheDir != "" {
json, err := s.getItemFromCache(itemID)
if err != nil {
if err != ErrNotFoundFile {
log.Printf("failed get a cache: %s", err)
}
} else {
// キャッシュが見つかった時
w.Header().Set("Content-Type", "application/json")
w.Write(json)
log.Printf("hit cache: %s", itemID)
return
}
}
params := url.Values{
"IdType": []string{"ASIN"},
"ItemId": []string{itemID},
"Operation": []string{"ItemLookup"},
"ResponseGroup": []string{"Large"},
}
res, err := s.client.ItemLookup(params)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("failed to get item infomation: %v", err)))
return
}
item, err := resToItem(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("failed to get item from response: %v", err)))
return
}
b, err := json.Marshal(item)
if err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("failed to marshal item to json: %v", err)))
return
}
// キャッシュする
if s.cacheDir != "" {
if err := s.saveItemToCache(item); err != nil {
log.Printf("failed to save a cache: %v", err)
}
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func resToItem(res *amazing.AmazonItemLookupResponse) (*Item, error) {
items := res.AmazonItems.Items
if len(items) == 0 {
return nil, errors.New("empty amazon items")
}
aitem := items[0]
item := &Item{
ASIN: aitem.ASIN,
Brand: aitem.ItemAttributes.Brand,
Creator: aitem.ItemAttributes.Creator,
Manufacturer: aitem.ItemAttributes.Manufacturer,
Publisher: aitem.ItemAttributes.Publisher,
ReleaseDate: aitem.ItemAttributes.ReleaseDate,
Studio: aitem.ItemAttributes.Studio,
Title: aitem.ItemAttributes.Title,
URL: aitem.DetailPageURL,
SmallImage: aitem.SmallImage.URL,
MediumImage: aitem.MediumImage.URL,
LargeImage: aitem.LargeImage.URL,
}
return item, nil
}