This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
techbook.go
158 lines (128 loc) · 3.32 KB
/
techbook.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
package infra
import (
"context"
"github.com/juntaki/techbook-qrcode/src/infra/models"
"io/ioutil"
"log"
"net/http"
"time"
"cloud.google.com/go/datastore"
"github.com/juntaki/techbook-qrcode/src/domain"
"gopkg.in/gorp.v2"
)
var cached domain.TechBook
var validUntil time.Time
type TechBook struct {
URL string
}
type TechBookRepositoryDatastoreImpl struct {
db *datastore.Client
}
func NewTechBookRepositoryDatastoreImpl(db *datastore.Client) domain.TechBookRepository {
return &TechBookRepositoryDatastoreImpl{
db: db,
}
}
func (i *TechBookRepositoryDatastoreImpl) SetTechBookURL(ctx context.Context, TechBookURL string) error {
key := datastore.NameKey("TechBook", "master", nil)
_, err := i.db.Put(ctx, key, &TechBook{
URL: TechBookURL,
})
log.Printf("Set URL %s", TechBookURL)
validUntil = time.Now()
log.Printf("Cache invalidate. Valid until now = %s", validUntil)
if err != nil {
return err
}
return nil
}
func (i *TechBookRepositoryDatastoreImpl) GetTechBookURL(ctx context.Context) (string, error) {
key := datastore.NameKey("TechBook", "master", nil)
val := TechBook{}
err := i.db.Get(ctx, key, &val)
if err != nil {
return "", err
}
return val.URL, nil
}
func (i *TechBookRepositoryDatastoreImpl) GetTechBook(ctx context.Context) (domain.TechBook, error) {
if validUntil.After(time.Now()) && len(cached) > 0 {
log.Printf("Return from cache. valid until: %s", validUntil)
return cached, nil
}
log.Printf("Get Techbook from URL")
key := datastore.NameKey("TechBook", "master", nil)
dst := TechBook{}
err := i.db.Get(ctx, key, &dst)
if err != nil {
return nil, err
}
res, err := http.Get(dst.URL)
if err != nil {
return nil, err
}
defer res.Body.Close()
ret, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
cached = ret
validUntil = time.Now().Add(6 * time.Hour)
log.Printf("Cache is valid until %s", validUntil)
return ret, nil
}
type TechBookRepositorySQLImpl struct {
db *gorp.DbMap
}
func NewTechBookRepositorySQLImpl(db *gorp.DbMap) domain.TechBookRepository {
return &TechBookRepositorySQLImpl{
db: db,
}
}
func (i *TechBookRepositorySQLImpl) SetTechBookURL(ctx context.Context, TechBookURL string) error {
t := &models.Techbook{
Id: 0,
CurrentUrl: TechBookURL,
}
_,_ = i.db.Delete(t)
err := i.db.Insert(t)
log.Printf("Set URL %s", TechBookURL)
validUntil = time.Now()
log.Printf("Cache invalidate. Valid until now = %s", validUntil)
if err != nil {
return err
}
return nil
}
func (i *TechBookRepositorySQLImpl) GetTechBookURL(ctx context.Context) (string, error) {
t := models.Techbook{}
_,err := i.db.Select(t, "select * from techbook")
if err != nil {
return "", err
}
return t.CurrentUrl, nil
}
func (i *TechBookRepositorySQLImpl) GetTechBook(ctx context.Context) (domain.TechBook, error) {
if validUntil.After(time.Now()) && len(cached) > 0 {
log.Printf("Return from cache. valid until: %s", validUntil)
return cached, nil
}
log.Printf("Get Techbook from URL")
dst, err := i.GetTechBookURL(ctx)
if err != nil {
return nil, err
}
res, err := http.Get(dst)
if err != nil {
return nil, err
}
defer res.Body.Close()
ret, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
cached = ret
validUntil = time.Now().Add(6 * time.Hour)
log.Printf("Cache is valid until %s", validUntil)
return ret, nil
}