-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbase.go
265 lines (199 loc) · 7.52 KB
/
dropbase.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/sethgrid/pester"
"github.com/spf13/cobra"
)
// http response switcher for debugging
func response(route string, res *http.Response) {
switch res.StatusCode {
case 200:
log.Printf("successful revalidation on "+route+", status: %s", res.Status)
case 404:
log.Printf("could not reach API on "+route+", status: %s, errror: %s", res.Status, res.Body)
case 500:
log.Printf("API on "+route+" has an internal server error, status: %s, errror: %s", res.Status, res.Body)
default:
log.Printf("response did not return a 200, 404, or 500 status, status: %s, errror: %s", res.Status, res.Body)
}
}
// send API request with values to ISR routes
func send(routes []string, values url.Values) {
for _, route := range routes {
res, err := pester.PostForm(route+"/api/revalidate", values)
if err != nil {
log.Printf("error posting to "+route+", error: %s", err)
}
defer res.Body.Close()
response(route, res)
}
}
// get category from database
func getCategory(app *pocketbase.PocketBase, target string) string {
collection, err := app.Dao().FindCollectionByNameOrId("categories")
if err != nil {
log.Printf("an error occured. err: %s", err)
}
result, err := app.Dao().FindFirstRecordByData(collection.Name, "name", target)
if err != nil {
log.Print(err)
}
categoryPermalink := result.GetString("permalink")
return categoryPermalink
}
func main() {
app := pocketbase.New()
var routes []string
err := godotenv.Load(".env")
if err != nil {
log.Printf("an error occured. err: %s", err)
}
var cached_product *models.Record
var cached_product_categories []string
var cached_category *models.Record
app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
cached_product_categories = []string{}
cached_category = nil
cached_product = nil
// cache product & it's categories if the update request collection is products
if e.Record.Collection().Name == "products" {
product, err := app.Dao().FindRecordById(e.Record.Collection().Name, e.Record.Id, nil)
if err != nil {
log.Printf("an error occured. err: %s", err)
}
cached_product = product
for _, category := range e.Record.GetStringSlice("category") {
cached_product_categories = append(cached_product_categories, getCategory(app, category))
}
}
// cache category if the update request collection is categories
if e.Record.Collection().Name == "categories" {
category, err := app.Dao().FindRecordById(e.Record.Collection().Name, e.Record.Id, nil)
if err != nil {
log.Printf("an error occured. err: %s", err)
}
cached_category = category
}
return nil
})
app.OnRecordAfterUpdateRequest().Add(func(record *core.RecordUpdateEvent) error {
values := url.Values{}
values.Add("api_key", os.Getenv(("API_KEY")))
// if the update request collection is products
if record.Record.Collection().Name == "products" {
var product_categories = []string{}
for _, category := range record.Record.GetStringSlice("category") {
product_categories = append(product_categories, getCategory(app, category))
}
// send new & old product permalinks & their categories
values.Add("new_product", record.Record.GetString("permalink"))
values.Add("new_categories", strings.Join(product_categories, ","))
values.Add("old_product", cached_product.GetString("permalink"))
values.Add("old_categories", strings.Join(cached_product_categories, ","))
send(routes, values)
}
// if the update request collection is categories
if record.Record.Collection().Name == "categories" {
// send new & old category permalinks & their categories
values.Add("old_category", cached_category.GetString("permalink"))
values.Add("new_category", record.Record.GetString("permalink"))
send(routes, values)
}
return nil
})
app.OnRecordAfterCreateRequest().Add(func(record *core.RecordCreateEvent) error {
values := url.Values{}
values.Add("api_key", os.Getenv(("API_KEY")))
// if the creation request collection is products
if record.Record.Collection().Name == "products" {
var product_categories = []string{}
for _, category := range record.Record.GetStringSlice("category") {
product_categories = append(product_categories, getCategory(app, category))
}
// send newly created product permalink & it's categories
values.Add("new_product", record.Record.GetString("permalink"))
values.Add("new_categories", strings.Join(product_categories, ","))
send(routes, values)
}
// if the creation request collection is categories
if record.Record.Collection().Name == "categories" {
// send newly created category permalink
values.Add("new_category", record.Record.GetString("permalink"))
send(routes, values)
}
return nil
})
app.OnRecordBeforeDeleteRequest().Add(func(record *core.RecordDeleteEvent) error {
cached_product_categories = []string{}
cached_category = nil
cached_product = nil
// cache product & it's categories if the delete request collection is products
if record.Record.Collection().Name == "products" {
product, err := app.Dao().FindRecordById(record.Record.Collection().Name, record.Record.Id, nil)
if err != nil {
log.Printf("an error occured. err: %s", err)
}
cached_product = product
for _, category := range record.Record.GetStringSlice("category") {
cached_product_categories = append(cached_product_categories, getCategory(app, category))
}
}
// cache category if the delete request collection is categories
if record.Record.Collection().Name == "categories" {
category, err := app.Dao().FindRecordById(record.Record.Collection().Name, record.Record.Id, nil)
if err != nil {
log.Printf("an error occured. err: %s", err)
}
cached_category = category
}
return nil
})
app.OnRecordAfterDeleteRequest().Add(func(record *core.RecordDeleteEvent) error {
values := url.Values{}
values.Add("api_key", os.Getenv(("API_KEY")))
// if the delete request collection is products
if record.Record.Collection().Name == "products" {
// send old deleted product permalink & it's categories
values.Add("old_product", cached_product.GetString("permalink"))
values.Add("old_categories", strings.Join(cached_product_categories, ","))
send(routes, values)
}
// if the delete request collection is cateogries
if record.Record.Collection().Name == "categories" {
// send old deleted category permalink
values.Add("old_category", cached_category.GetString("permalink"))
send(routes, values)
}
return nil
})
// create a custom start command
var startCmd = &cobra.Command{
Use: "start",
Short: "a custom start command to target ISR routes using flags",
Run: func(command *cobra.Command, args []string) {
server, _ := command.Flags().GetString("http")
ISR_Routes, _ := command.Flags().GetStringSlice("routes")
routes = append(routes, ISR_Routes...)
app.RootCmd.SetArgs([]string{"serve", "--http", server, "--encryptionEnv=" + os.Getenv("ENCRYPTION_KEY")})
if err := app.Start(); err != nil {
log.Fatal(err)
}
},
}
app.RootCmd.AddCommand(startCmd)
// set flags for start command
startCmd.Flags().String("http", "0.0.0.0:8090", "custom http address & port")
startCmd.Flags().StringSlice("routes", []string{}, "routes for incremental static regeneration")
startCmd.MarkFlagRequired("routes")
if err := app.Start(); err != nil {
log.Fatal(err)
}
}