Skip to content

Commit

Permalink
add /cache endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kurosame committed May 4, 2024
1 parent b17bf0b commit 8d1cbee
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"gcloud",
"godotenv",
"hashicorp",
"hasura",
"joho",
"kurosame",
"rssapp",
"secretmanager",
"secretmanagerpb"
]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ SLACK_USER_OAUTH_TOKEN # Slack user oauth token
SLACK_BOT_OAUTH_TOKEN # Slack bot oauth token
SLACK_CHANNEL_ID # Slack channel id (for posting)
SLACK_CHANNEL_ID_RSS # Slack channel id (for RSS)
RSSAPP_ID_TWITTER # RSS.app id (for media-twitter)
RSSAPP_ID_LIKE # RSS.app id (for news-like)
```

Deploy to the Cloud Functions are as follows
Expand Down
59 changes: 59 additions & 0 deletions bots/rss/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package rss

import (
"context"
"fmt"
"log"
"net/http"
"os"

secretmanager "cloud.google.com/go/secretmanager/apiv1"
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
"github.com/hasura/go-graphql-client"
)

func ClearCache(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()

clientSm, err := secretmanager.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
defer clientSm.Close()

req := &secretmanagerpb.AccessSecretVersionRequest{
Name: fmt.Sprintf("projects/%s/secrets/twitter-rss-filter/versions/latest", os.Getenv("GOOGLE_PROJECT_NUMBER")),
}

res, err := clientSm.AccessSecretVersion(ctx, req)
if err != nil {
log.Fatal(err)
}

clientGql := graphql.NewClient("https://rss.app/gql", nil).WithRequestModifier(func(r *http.Request) {
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Cookie", fmt.Sprintf("token=%s", string(res.Payload.Data)))
})

var m struct {
ClearFeedCache graphql.ID `graphql:"clearFeedCache(id: $id)"`
}
varTwitter := map[string]interface{}{
"id": graphql.ID(os.Getenv("RSSAPP_ID_TWITTER")),
}
varLike := map[string]interface{}{
"id": graphql.ID(os.Getenv("RSSAPP_ID_LIKE")),
}

resTwitter, err := clientGql.MutateRaw(ctx, m, varTwitter, graphql.OperationName("clearFeedCache"))
if err != nil {
log.Fatal(err)
}
print(string(resTwitter))

resLike, err := clientGql.MutateRaw(ctx, m, varLike, graphql.OperationName("clearFeedCache"))
if err != nil {
log.Fatal(err)
}
print(string(resLike))
}
1 change: 1 addition & 0 deletions bots/rss/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
http.HandleFunc("/", rss.FilterTwitterRSS)
http.HandleFunc("/kw", rss.AddKeyword)
http.HandleFunc("/token", rss.SetToken)
http.HandleFunc("/cache", rss.ClearCache)

log.Fatal(http.ListenAndServe(":8080", nil))
}
6 changes: 4 additions & 2 deletions bots/rss/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/kurosame/bots-go/bots/rss

go 1.19
go 1.21

require (
cloud.google.com/go/datastore v1.15.0
cloud.google.com/go/secretmanager v1.11.1
github.com/GoogleCloudPlatform/functions-framework-go v1.5.3
github.com/hasura/go-graphql-client v0.12.1
github.com/joho/godotenv v1.4.0
github.com/slack-go/slack v0.11.0
)
Expand All @@ -21,7 +22,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
Expand All @@ -46,4 +47,5 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
nhooyr.io/websocket v1.8.10 // indirect
)
9 changes: 7 additions & 2 deletions bots/rss/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ github.com/google/s2a-go v0.1.4 h1:1kZ/sQM3srePvKs3tXAvQzo66XfcReoqFpIpIccE7Oc=
github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.2.4 h1:uGy6JWR/uMIILU8wbf+OkstIrNiMjGpEIyhx8f6W7s4=
github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
Expand All @@ -190,6 +190,8 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hasura/go-graphql-client v0.12.1 h1:tL+BCoyubkYYyaQ+tJz+oPe/pSxYwOJHwe5SSqqi6WI=
github.com/hasura/go-graphql-client v0.12.1/go.mod h1:F4N4kR6vY8amio3gEu3tjSZr8GPOXJr3zj72DKixfLE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
Expand Down Expand Up @@ -438,6 +440,7 @@ golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxb
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down Expand Up @@ -656,6 +659,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
2 changes: 2 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ module "rss" {
SLACK_BOT_OAUTH_TOKEN = var.SLACK_BOT_OAUTH_TOKEN
SLACK_CHANNEL_ID = var.SLACK_CHANNEL_ID
SLACK_CHANNEL_ID_RSS = var.SLACK_CHANNEL_ID_RSS
RSSAPP_ID_TWITTER = var.RSSAPP_ID_TWITTER
RSSAPP_ID_LIKE = var.RSSAPP_ID_LIKE
}
36 changes: 33 additions & 3 deletions terraform/modules/rss/functions.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
resource "google_cloudfunctions_function" "twitter_rss_filter" {
name = "twitter-rss-filter"
runtime = "go119"
runtime = "go121"
available_memory_mb = 128
timeout = 540
source_archive_bucket = google_storage_bucket.this.name
source_archive_object = google_storage_bucket_object.this.name
docker_registry = "ARTIFACT_REGISTRY"
trigger_http = true
entry_point = "FilterTwitterRSS"
service_account_email = google_service_account.this.email

environment_variables = {
GOOGLE_PROJECT_ID = var.GOOGLE_PROJECT_ID
Expand All @@ -28,14 +29,15 @@ resource "google_cloudfunctions_function_iam_member" "twitter_rss_filter" {

resource "google_cloudfunctions_function" "twitter_rss_filter_add_keyword" {
name = "twitter-rss-filter-add-keyword"
runtime = "go119"
runtime = "go121"
available_memory_mb = 128
timeout = 60
source_archive_bucket = google_storage_bucket.this.name
source_archive_object = google_storage_bucket_object.this.name
docker_registry = "ARTIFACT_REGISTRY"
trigger_http = true
entry_point = "AddKeyword"
service_account_email = google_service_account.this.email

environment_variables = {
GOOGLE_PROJECT_ID = var.GOOGLE_PROJECT_ID
Expand All @@ -52,14 +54,15 @@ resource "google_cloudfunctions_function_iam_member" "twitter_rss_filter_add_key

resource "google_cloudfunctions_function" "twitter_rss_filter_set_token" {
name = "twitter-rss-filter-set-token"
runtime = "go119"
runtime = "go121"
available_memory_mb = 128
timeout = 60
source_archive_bucket = google_storage_bucket.this.name
source_archive_object = google_storage_bucket_object.this.name
docker_registry = "ARTIFACT_REGISTRY"
trigger_http = true
entry_point = "SetToken"
service_account_email = google_service_account.this.email

environment_variables = {
GOOGLE_PROJECT_NUMBER = var.GOOGLE_PROJECT_NUMBER
Expand All @@ -73,3 +76,30 @@ resource "google_cloudfunctions_function_iam_member" "twitter_rss_filter_set_tok
role = "roles/cloudfunctions.invoker"
member = "serviceAccount:${google_service_account.this.email}"
}

resource "google_cloudfunctions_function" "twitter_rss_filter_clear_cache" {
name = "twitter-rss-filter-clear-cache"
runtime = "go121"
available_memory_mb = 128
timeout = 60
source_archive_bucket = google_storage_bucket.this.name
source_archive_object = google_storage_bucket_object.this.name
docker_registry = "ARTIFACT_REGISTRY"
trigger_http = true
entry_point = "ClearCache"
service_account_email = google_service_account.this.email

environment_variables = {
GOOGLE_PROJECT_NUMBER = var.GOOGLE_PROJECT_NUMBER
RSSAPP_ID_TWITTER = var.RSSAPP_ID_TWITTER
RSSAPP_ID_LIKE = var.RSSAPP_ID_LIKE
}
}

resource "google_cloudfunctions_function_iam_member" "twitter_rss_filter_clear_cache" {
project = google_cloudfunctions_function.twitter_rss_filter_clear_cache.project
region = google_cloudfunctions_function.twitter_rss_filter_clear_cache.region
cloud_function = google_cloudfunctions_function.twitter_rss_filter_clear_cache.name
role = "roles/cloudfunctions.invoker"
member = "serviceAccount:${google_service_account.this.email}"
}
12 changes: 12 additions & 0 deletions terraform/modules/rss/iam.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
resource "google_service_account" "this" {
account_id = "rss-invoker"
}

resource "google_project_iam_member" "this" {
for_each = toset([
"roles/datastore.owner",
"roles/secretmanager.secretAccessor",
"roles/secretmanager.secretVersionAdder"
])

project = var.GOOGLE_PROJECT_ID
role = each.key
member = "serviceAccount:${google_service_account.this.email}"
}
8 changes: 8 additions & 0 deletions terraform/modules/rss/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ variable "SLACK_CHANNEL_ID" {
variable "SLACK_CHANNEL_ID_RSS" {
type = string
}

variable "RSSAPP_ID_TWITTER" {
type = string
}

variable "RSSAPP_ID_LIKE" {
type = string
}
8 changes: 8 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ variable "SLACK_CHANNEL_ID" {
variable "SLACK_CHANNEL_ID_RSS" {
type = string
}

variable "RSSAPP_ID_TWITTER" {
type = string
}

variable "RSSAPP_ID_LIKE" {
type = string
}

0 comments on commit 8d1cbee

Please sign in to comment.