Skip to content

Commit 8f59aef

Browse files
committed
Cleanup a bit after migrating to the new project.
1) project-id changed 2) location changed 3) try not creating the template for each request, just do it at startup. 4) attempt some query cleanup, make the creation of the read query a bit more straightforward.
1 parent 5a45e58 commit 8f59aef

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

Historical-ROA/main.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ type Creds struct {
7373
}
7474

7575
var (
76-
client *bigquery.Client
77-
gcreds Creds
76+
client *bigquery.Client
77+
gcreds Creds
78+
mainTmpl = template.Must(template.New("main").Parse("./index.html"))
7879
)
7980

8081
const (
8182
roaURL = "https://hosted-routinator.rarc.net/json"
82-
projectID = "historical-roas"
83-
projectLocation = "us-east4"
83+
projectID = "public-routing-data-backup"
84+
projectLocation = "us-central"
8485
)
8586

8687
func main() {
@@ -110,7 +111,7 @@ func main() {
110111
// open bigquery connection
111112
client, err = bigquery.NewClient(context.Background(), gcreds.ProjectID)
112113
if err != nil {
113-
log.Fatalln(err)
114+
log.Fatalf("unable to connect to the bigquery DB: %v", err)
114115
}
115116

116117
client.Location = projectLocation
@@ -135,14 +136,9 @@ func mainPage(w http.ResponseWriter, r *http.Request) {
135136
ctx := context.Background()
136137
w.Header().Add("strict-transport-security", "max-age=2629800")
137138

138-
tmpl, err := template.ParseFiles("./index.html")
139-
if err != nil {
140-
log.Errorln(err)
141-
return
142-
}
143-
144139
if r.Method != http.MethodPost {
145-
tmpl.Execute(w, nil)
140+
log.Info("Not a POST, show default page.")
141+
mainTmpl.Execute(w, nil)
146142
return
147143
}
148144

@@ -155,7 +151,8 @@ func mainPage(w http.ResponseWriter, r *http.Request) {
155151
if input.ParseCIDR != "" {
156152
_, n, err := net.ParseCIDR(input.Prefix)
157153
if err != nil {
158-
tmpl.Execute(w, nil)
154+
log.Infof("parseCIDR failed for %q: %v", input.Prefix, err)
155+
mainTmpl.Execute(w, nil)
159156
return
160157
}
161158
input.Prefix = n.String()
@@ -174,19 +171,20 @@ func mainPage(w http.ResponseWriter, r *http.Request) {
174171

175172
log.Traceln(input)
176173

177-
var query *bigquery.Query
174+
baseQuery := `SELECT asn, prefix, mask, maxlen, ta, inserttimes
175+
FROM historical-roas.historical.roas_arr
176+
WHERE true=true
177+
`
178178
switch {
179179
case hasASN && !hasPrefix:
180-
query = client.Query(`SELECT asn, prefix, mask, maxlen, ta, inserttimes FROM historical-roas.historical.roas_arr
181-
WHERE asn = @asn`)
182-
180+
baseQuery = baseQuery + "AND asn = @asn"
183181
case !hasASN && hasPrefix:
184-
query = client.Query(`SELECT asn, prefix, mask, maxlen, ta, inserttimes FROM historical-roas.historical.roas_arr
185-
WHERE prefix = @prefix AND mask = @mask`)
182+
baseQuery = baseQuery + "AND prefix = @prefix AND mask = @mask"
186183
case hasASN && hasPrefix:
187-
query = client.Query(`SELECT asn, prefix, mask, maxlen, ta, inserttimes FROM historical-roas.historical.roas_arr
188-
WHERE asn = @asn AND prefix = @prefix AND mask = @mask`)
184+
baseQuery = baseQuery + "AND asn = @asn AND prefix = @prefix AND mask = @mask"
189185
}
186+
query := client.Query(baseQuery)
187+
190188
query.Parameters = []bigquery.QueryParameter{
191189
{
192190
Name: "asn",
@@ -201,20 +199,24 @@ func mainPage(w http.ResponseWriter, r *http.Request) {
201199
Value: inputStore.Subnet,
202200
},
203201
}
202+
204203
job, err := query.Run(ctx)
205204
if err != nil {
205+
log.Infof("error running the main-page query: %q: %v", baseQuery, err)
206206
ErrorHandler(w, r, 500, "Error with query", err)
207207
return
208208
}
209209

210210
status, _ := job.Wait(ctx)
211211
if err := status.Err(); err != nil {
212+
log.Infof("error while waiting on job completion: %v", err)
212213
ErrorHandler(w, r, 500, "Error with query", err)
213214
return
214215
}
215216

216217
it, err := job.Read(ctx)
217218
if err != nil {
219+
log.Infof("error while reading from the job: %v", err)
218220
ErrorHandler(w, r, 500, "Error with query", err)
219221
return
220222
}
@@ -226,6 +228,7 @@ func mainPage(w http.ResponseWriter, r *http.Request) {
226228
break
227229
}
228230
if err != nil {
231+
log.Infof("failed getting the next row from BQ: %v", err)
229232
ErrorHandler(w, r, 500, "Error with query", err)
230233
continue
231234
}
@@ -236,6 +239,8 @@ func mainPage(w http.ResponseWriter, r *http.Request) {
236239
intime = append(intime, t.(time.Time))
237240
}
238241

242+
// SELECT asn, prefix, mask, maxlen, ta, inserttimes FROM...
243+
// Find a better way to unwrap this, if possible.
239244
var results = pb.ResultsFromDB{
240245
ASN: row[0].(string), // this
241246
Prefix: row[1].(string), // is

0 commit comments

Comments
 (0)