Skip to content

Commit

Permalink
Add: Allow disabling discord
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Apr 16, 2024
1 parent c841c3f commit f269491
Show file tree
Hide file tree
Showing 22 changed files with 503 additions and 442 deletions.
7 changes: 0 additions & 7 deletions Makefile

This file was deleted.

15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ task "configure" do
)
puts "Built .env"
end

task "install" do
sh "cd frontend && pnpm install"
sh "cd backend && bundle install"
sh "cd sub-audio && poetry install"
sh "cd sub-chart && pnpm install"
end

task "format" do
sh "cd frontend && pnpm run lint:fix"
sh "cd backend && bundle exec rubocop -a"
sh "cd sub-audio && poetry run black ."
sh "cd sub-chart && pnpm run lint:fix"
sh "cd sub-image && cargo fmt"
end
3 changes: 2 additions & 1 deletion backend/app/controllers/api/charts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def show
code: "ok",
chart:
chart.to_frontend(
with_resources: params[:with_resources] != "false"
with_resources: params[:with_resources] != "false",
with_variants: true
)
}
else
Expand Down
2 changes: 1 addition & 1 deletion backend/app/controllers/frontend_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ def require_login!
throw :unauthorized unless current_user
end
def require_discord!
throw :unauthorized unless current_user.check_discord
throw :unauthorized unless $discord.enabled? && current_user.check_discord
end
end
2 changes: 1 addition & 1 deletion backend/app/models/chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def variants(include_private: false)
include_private ? _variants : _variants.where(visibility: :public)
end

def to_frontend(user: nil, with_resources: true, with_variants: true)
def to_frontend(user: nil, with_resources: true, with_variants: false)
resources = with_resources ? self.resources : {}
likes = likes_count
{
Expand Down
1 change: 0 additions & 1 deletion backend/config/credentials.yml.enc

This file was deleted.

2 changes: 1 addition & 1 deletion backend/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ default: &default
password: postgres

database: <%= ENV['POSTGRES_DATABASE'] %>
host: <%= ENV['HOSTS_PG'] %>
host: <%= ENV['POSTGRES_HOST'] %>
port: <%= ENV['POSTGRES_PORT'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
checkout_timeout: <%= ENV.fetch("RAILS_DB_TIMEOUT") { 5 } %>
Expand Down
34 changes: 18 additions & 16 deletions backend/config/initializers/discord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ class Disabled
def method_missing(*_args)
raise "Discord disabled"
end
end
if Rails.env.test?
$discord = Disabled.new
# Disable
else
$discord = DiscordRequest.new(bot_token: ENV["DISCORD_BOT_TOKEN"])

info =
begin
$discord.get("/users/@me")
rescue StandardError
Rails.logger.error("Discord: Failed to log in, disabling")
$discord = Disabled.new
end
def enabled?
false
end
end
if bot_token = ENV["DISCORD_BOT_TOKEN"]
$discord = DiscordRequest.new(bot_token:)

Rails.logger.info(
"Discord: Logged in as #{info["username"]}##{info["discriminator"]}"
)
begin
info = $discord.get("/users/@me")
Rails.logger.info(
"Discord: Logged in as #{info["username"]}##{info["discriminator"]}"
)
rescue StandardError
Rails.logger.error("Discord: Failed to log in")
raise
end
else
Rails.logger.info("Discord: Disabled")
$discord = Disabled.new
end
4 changes: 4 additions & 0 deletions backend/lib/discord_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def inspect
"#<#{self.class.name} #{@bot ? "Bot" : "User"}>"
end

def enabled?
true
end

private

def request(method, path, **options)
Expand Down
13 changes: 8 additions & 5 deletions config.dev.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# yaml-language-server: $schema=./config.schema.yml
# Configuration template for development environment.

admin_handle: "1073"
host: "http://192.168.1.2:3000"
secret_key_base: "e381a4e3818fe38288e381bfe381a1e38283e38293e381afe3818be3828fe38184e38184"
hosts:
frontend: "http://localhost:3000"
backend: "http://localhost:3001"
sub_audio: "http://localhost:3002"
sub_image: "http://localhost:3003"
sub_chart: "http://localhost:3004"
backend: "http://localhost:3000"
frontend: "http://localhost:3100"
sub_chart: "http://localhost:3201"
sub_audio: "http://localhost:3202"
sub_image: "http://localhost:3203"
s3:
user: "minio"
password: "secretkey"
Expand Down
34 changes: 34 additions & 0 deletions config.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# yaml-language-server: $schema=./config.schema.yml
# Configuration template for production environment.

admin_handle: "1073"
host: "https://example.com"
secret_key_base: "e381a4e3818fe38288e381bfe381a1e38283e38293e381afe3818be3828fe38184e38184"
hosts:
backend: "http://backend:3000"
frontend: "http://frontend:3100"
sub_chart: "http://sub-chart:3201"
sub_audio: "http://sub-audio:3202"
sub_image: "http://sub-image:3203"
s3:
user: "access_id"
password: "access_token"
bucket: "chcy"
region: "us-east-1"
endpoint: "https://hogehogefugafuga.r2.cloudflarestorage.com"
public: true
public_root: "https://cdn.example.com/"
redis_url: "redis://redis:6379/0"
postgres:
user: "postgres"
password: "postgres"
database: "chcy"
host: "postgres"
port: 5432
discord:
guild_id: "123456789012345678"
client_id: "123456789012345678"
client_secret: "cl1ent53cr3t"
bot_token: "D1dY0uR34llyTh1nk.1W0uldPutTh3B0tT0k3nH3r3.10l"
warning_channel_id: "123456789012345678"

3 changes: 3 additions & 0 deletions config.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ properties:
host:
type: "string"
description: "The host that the chart cyanvas will be hosted on."
secret_key_base:
type: "string"
description: "Secret key base for the Rails application."
hosts:
type: "object"
description: "Hosts configuration."
Expand Down
12 changes: 5 additions & 7 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- sub-audio
- sub-image
volumes:
- ./${ENV_FILE}:/app/.env
- ./.env:/app/.env
profiles:
- prod
healthcheck:
Expand All @@ -23,11 +23,9 @@ services:
build:
context: .
dockerfile: ./frontend/Dockerfile
args:
- ENV_FILE=${ENV_FILE}

volumes:
- ./${ENV_FILE}:/app/.env
- ./.env:/app/.env
profiles:
- prod
healthcheck:
Expand All @@ -42,7 +40,7 @@ services:
dockerfile: ./sub-chart/Dockerfile

volumes:
- ./${ENV_FILE}:/app/.env
- ./.env:/app/.env
profiles:
- prod
healthcheck:
Expand All @@ -56,7 +54,7 @@ services:
context: .
dockerfile: ./sub-audio/Dockerfile
volumes:
- ./${ENV_FILE}:/app/.env
- ./.env:/app/.env
profiles:
- prod
healthcheck:
Expand All @@ -71,7 +69,7 @@ services:
dockerfile: ./sub-image/Dockerfile

volumes:
- ./${ENV_FILE}:/app/.env
- ./.env:/app/.env
profiles:
- prod
healthcheck:
Expand Down
17 changes: 8 additions & 9 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
image: "ghcr.io/sevenc-nanashi/chart_cyanvas/frontend:latest"
pull_policy: always
volumes:
- ./.env.prod:/app/.env
- ./.env:/app/.env
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3100/"]
interval: 30s
Expand All @@ -16,8 +16,7 @@ services:
image: "ghcr.io/sevenc-nanashi/chart_cyanvas/backend:latest"
pull_policy: always
volumes:
- ./.env.prod:/app/.env
- ./rails_master.key:/app/config/master.key
- ./.env:/app/.env
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/"]
interval: 30s
Expand All @@ -29,7 +28,7 @@ services:
pull_policy: always
restart: always
volumes:
- ./.env.prod:/app/.env
- ./.env:/app/.env
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3202/"]
interval: 30s
Expand All @@ -41,7 +40,7 @@ services:
pull_policy: always
restart: always
volumes:
- ./.env.prod:/app/.env
- ./.env:/app/.env
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3203/"]
interval: 30s
Expand All @@ -53,7 +52,7 @@ services:
pull_policy: always
restart: always
volumes:
- ./.env.prod:/app/.env
- ./.env:/app/.env
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3201/"]
interval: 30s
Expand Down Expand Up @@ -82,9 +81,9 @@ services:
timeout: 10s
retries: 3
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: ${PG_DATABASE}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}

nginx:
image: "nginx:1.23.3-alpine"
Expand Down
2 changes: 1 addition & 1 deletion frontend/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
locales: ["en", "ja"],
defaultLocale: "en",
localeDetection: true,
localeDetection: false,
defaultNS: "common",
logger: () => {
null
Expand Down
4 changes: 4 additions & 0 deletions frontend/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ export const parseIntOrFallback = (base: string, fallback: number) => {
if (isNaN(retvar)) return fallback
return retvar
}

export const isDiscordEnabled = () => {
return !!publicRuntimeConfig.discordEnabled
}
Loading

0 comments on commit f269491

Please sign in to comment.