Skip to content

Commit

Permalink
Merge pull request #345 from na2na-p/topic/spotify-callback-endpoint
Browse files Browse the repository at this point in the history
SpotifyのOAuthコールバック用バックエンドの作成
  • Loading branch information
2na2-p[bot] authored Jan 9, 2024
2 parents cea0a49 + 04f56b5 commit a7fa85a
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 0 deletions.
37 changes: 37 additions & 0 deletions infra/terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.DS_Store

### Terraform template
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
25 changes: 25 additions & 0 deletions infra/terraform/cloudflare/envs/main/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions infra/terraform/cloudflare/envs/main/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
locals {
service = "jetdisc"
env = "main"
}

module "app" {
providers = {
cloudflare = cloudflare
}

source = "../../modules/app"
service = local.service
env = local.env
account_id = var.account_id
zone_id = var.zone_id
base_domain = var.base_domain
}
3 changes: 3 additions & 0 deletions infra/terraform/cloudflare/envs/main/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
19 changes: 19 additions & 0 deletions infra/terraform/cloudflare/envs/main/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
variable "cloudflare_api_token" {
description = "Cloudflare APIトークン"
type = string
}

variable "account_id" {
description = "Cloudflare Account ID"
type = string
}

variable "zone_id" {
description = "Cloudflare Zone ID"
type = string
}

variable "base_domain" {
description = "Base domain"
type = string
}
10 changes: 10 additions & 0 deletions infra/terraform/cloudflare/envs/main/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "1.6.6"

required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
8 changes: 8 additions & 0 deletions infra/terraform/cloudflare/modules/app/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module "worker" {
source = "./modules/workers"
worker_script_path = "./scripts/worker.js"
account_id = var.account_id
worker_name = "${var.service}-${var.env}"
zone_id = var.zone_id
base_domain = var.base_domain
}
23 changes: 23 additions & 0 deletions infra/terraform/cloudflare/modules/app/modules/workers/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
locals {
worker_script_dir = "${path.module}/scripts"
}

resource "cloudflare_worker_script" "worker" {
account_id = var.account_id
name = var.worker_name
content = file("${local.worker_script_dir}/worker.js")
}

resource "cloudflare_worker_route" "name" {
pattern = "${var.worker_name}.${var.base_domain}"
zone_id = var.zone_id
script_name = cloudflare_worker_script.worker.name
}

resource "cloudflare_worker_domain" "custom_domain" {
hostname = "${var.worker_name}.${var.base_domain}"
account_id = var.account_id
service = var.worker_name
zone_id = var.zone_id
depends_on = [cloudflare_worker_script.worker]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
const url = new URL(request.url);
const code = url.searchParams.get('code');

if (!code) {
return new Response('No code found in URL.', { status: 400 });
}

// HTMLレスポンスを生成
const htmlContent = `
<html>
<head>
<title>Spotify Auth Code</title>
</head>
<body>
<h1>Your Spotify Authentication Code</h1>
<p>Copy and paste this code back into your Discord chat:</p>
<div>
<code>${code}</code>
</div>
<button onclick="copyToClipboard()">Copy to Clipboard</button>
</body>
<script>
function copyToClipboard() {
const code = document.querySelector("code");
navigator.clipboard.writeText(code.textContent);
}
</script>
</html>
`;

return new Response(htmlContent, {
headers: { 'Content-Type': 'text/html' },
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "worker_name" {
description = "Name of Cloudflare Worker"
}

variable "worker_script_path" {
description = "Path to Cloudflare Worker script"
type = string
default = "./scripts/worker.js"
}

variable "account_id" {
description = "Cloudflare Account ID"
type = string
}

variable "zone_id" {
description = "Cloudflare Zone ID"
type = string
}

variable "base_domain" {
description = "Base domain"
type = string
}
10 changes: 10 additions & 0 deletions infra/terraform/cloudflare/modules/app/modules/workers/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "1.6.6"

required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
24 changes: 24 additions & 0 deletions infra/terraform/cloudflare/modules/app/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "service" {
type = string
description = "used for resource name and tag prefix"
}

variable "env" {
type = string
description = "used for resource name and tag prefix"
}

variable "account_id" {
description = "Cloudflare Account ID"
type = string
}

variable "zone_id" {
description = "Cloudflare Zone ID"
type = string
}

variable "base_domain" {
description = "Base domain"
type = string
}
10 changes: 10 additions & 0 deletions infra/terraform/cloudflare/modules/app/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "1.6.6"

required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}

0 comments on commit a7fa85a

Please sign in to comment.