Skip to content

Commit b0b802f

Browse files
committed
Add deployment
1 parent 63913da commit b0b802f

File tree

14 files changed

+597
-287
lines changed

14 files changed

+597
-287
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env.prod
2+
dist/

.goreleaser.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
project_name: starquery
2+
3+
before:
4+
hooks:
5+
- go mod tidy
6+
7+
builds:
8+
- env:
9+
- CGO_ENABLED=0
10+
goos: [linux]
11+
goarch: [amd64]
12+
main: ./cmd/starquery
13+
14+
nfpms:
15+
- id: deb
16+
maintainer: [email protected]
17+
package_name: starquery
18+
formats: [deb]
19+
contents:
20+
- src: starquery.service
21+
dst: /usr/lib/systemd/system/starquery.service

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# starquery
2+
3+
An API that near-realtime tracks whether a user has starred a GitHub repository.
4+
5+
## Deployment
6+
7+
The API is live at `starquery.coder.com`. A Cloudflare Tunnel is used for DDoS protection made with [this guide](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/). The config is:
8+
9+
```yaml
10+
# Located in /home/kyle/.cloudflared/config.yml
11+
url: http://localhost:8080
12+
tunnel: 7e5e3b0d-4eb3-4aff-9924-e5f6efebcc2d
13+
credentials-file: /home/kyle/.cloudflared/7e5e3b0d-4eb3-4aff-9924-e5f6efebcc2d.json
14+
```
15+
16+
`cloudflared` is ran in `screen -S cloudflared`:
17+
18+
```
19+
cloudflared tunnel run 7e5e3b0d-4eb3-4aff-9924-e5f6efebcc2d
20+
```
21+
22+
`/run/starquery/environ` must have `WEBHOOK_SECRET`, but here's a template:
23+
24+
```env
25+
REDIS_URL=127.0.0.1:6379
26+
BIND_ADDRESS=127.0.0.1:8080
27+
# use cdrci account
28+
GITHUB_TOKEN=
29+
```

cloudflared.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
url: http://localhost:8080
2+
tunnel: 7e5e3b0d-4eb3-4aff-9924-e5f6efebcc2d
3+
credentials-file: /home/kyle/.cloudflared/7e5e3b0d-4eb3-4aff-9924-e5f6efebcc2d.json

cmd/starquery/main.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"log/slog"
67
"net/http"
78
"os"
89

910
"github.com/coder/starquery"
1011
"github.com/coder/starquery/kv"
12+
"golang.org/x/oauth2"
1113
)
1214

1315
func main() {
@@ -20,12 +22,38 @@ func main() {
2022
}
2123

2224
func run(ctx context.Context, logger *slog.Logger) error {
23-
err := http.ListenAndServe(":8085", starquery.New(ctx, starquery.Options{
24-
Client: http.DefaultClient,
25-
KV: kv.NewMemory(),
26-
Logger: logger,
27-
Repos: []starquery.Repo{{"coder", "coder"}},
28-
WebhookSecret: "potato",
25+
bindAddress, ok := os.LookupEnv("BIND_ADDRESS")
26+
if !ok {
27+
bindAddress = "127.0.0.1:8080"
28+
}
29+
githubToken, ok := os.LookupEnv("GITHUB_TOKEN")
30+
if !ok {
31+
logger.Warn("missing GITHUB_TOKEN, unauthenticated requests will be rate-limited")
32+
}
33+
34+
redisURL, ok := os.LookupEnv("REDIS_URL")
35+
var store kv.Store
36+
if !ok {
37+
logger.Warn("missing REDIS_URL, using in-memory store")
38+
store = kv.NewMemory()
39+
} else {
40+
store = kv.NewRedis(redisURL)
41+
}
42+
43+
webhookSecret, ok := os.LookupEnv("WEBHOOK_SECRET")
44+
if !ok {
45+
return errors.New("missing WEBHOOK_SECRET")
46+
}
47+
48+
err := http.ListenAndServe(bindAddress, starquery.New(ctx, starquery.Options{
49+
Client: oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken})),
50+
KV: store,
51+
Logger: logger,
52+
Repos: []starquery.Repo{{
53+
Owner: "coder",
54+
Name: "coder",
55+
}},
56+
WebhookSecret: webhookSecret,
2957
}))
3058
return err
3159
}

deploy.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
cd "$(dirname "$0")"
5+
6+
goreleaser release --snapshot --clean
7+
gcloud compute scp --project "coder-starquery" ./dist/*.deb kyle@starquery:~/starquery.deb
8+
gcloud compute ssh --project "coder-starquery" kyle@starquery -- "sudo dpkg -i ~/starquery.deb && sudo systemctl daemon-reload && sudo service starquery restart"

github/ipfilter.go

Lines changed: 0 additions & 117 deletions
This file was deleted.

github/ipfilter_test.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ module github.com/coder/starquery
33
go 1.22.2
44

55
require (
6-
github.com/coder/retry v1.5.1
6+
github.com/coder/redjet v0.7.3
77
github.com/google/go-github/v52 v52.0.0
8+
github.com/stretchr/testify v1.8.4
9+
golang.org/x/oauth2 v0.7.0
810
)
911

1012
require (
1113
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
1214
github.com/cloudflare/circl v1.1.0 // indirect
13-
github.com/coder/redjet v0.7.3 // indirect
15+
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/golang/protobuf v1.5.2 // indirect
1517
github.com/google/go-querystring v1.1.0 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
1619
golang.org/x/crypto v0.7.0 // indirect
1720
golang.org/x/net v0.9.0 // indirect
18-
golang.org/x/oauth2 v0.7.0 // indirect
1921
golang.org/x/sys v0.7.0 // indirect
2022
google.golang.org/appengine v1.6.7 // indirect
2123
google.golang.org/protobuf v1.28.0 // indirect
24+
gopkg.in/yaml.v3 v3.0.1 // indirect
2225
)

go.sum

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GK
55
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
66
github.com/coder/redjet v0.7.3 h1:4Ckm+aMkBhlW8RP1sy+J4sHFRvODq0uQwSzTpvsrjow=
77
github.com/coder/redjet v0.7.3/go.mod h1:5Wv3clyEfwKzy3Tz85NEhVBUM5uoPJQm6NCGAcEc9vg=
8-
github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=
9-
github.com/coder/retry v1.5.1/go.mod h1:blHMk9vs6LkoRT9ZHyuZo360cufXEhrxqvEzeMtRGoY=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1111
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1212
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
1313
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
1414
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1515
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
16+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
17+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1618
github.com/google/go-github/v52 v52.0.0 h1:uyGWOY+jMQ8GVGSX8dkSwCzlehU3WfdxQ7GweO/JP7M=
1719
github.com/google/go-github/v52 v52.0.0/go.mod h1:WJV6VEEUPuMo5pXqqa2ZCZEdbQqua4zAk2MZTIo+m+4=
1820
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
1921
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
22+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
23+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
25+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
26+
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
27+
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
2028
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2129
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
2230
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
@@ -45,3 +53,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
4553
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
4654
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
4755
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
56+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
57+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
58+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
59+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)