diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000000..9fb32281bc --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,71 @@ +#syntax=docker/dockerfile:1 +ARG VARIANT=3.2 +FROM mcr.microsoft.com/vscode/devcontainers/ruby:${VARIANT} AS base + +# Update args in docker-compose.yaml to set the UID/GID of the "vscode" user. +# ARG USER_UID=1000 +# ARG USER_GID=$USER_UID +# RUN if [ "$USER_GID" != "1000" ] || [ "$USER_UID" != "1000" ]; then \ +# groupmod --gid $USER_GID vscode \ +# && usermod --uid $USER_UID --gid $USER_GID vscode \ +# && chmod -R $USER_UID:$USER_GID /home/vscode; \ +# fi + +ARG NODE_VERSION=18.14.2 + +ENV BUNDLE_PATH="/usr/local/bundle" +ENV GEM_HOME="/usr/local/bundle/gems" +ENV GEM_PATH="/usr/local/bundle/gems" + +RUN mkdir /app +WORKDIR /app +RUN mkdir -p tmp/pids + +RUN apt-get update && apt-get install -y --no-install-recommends curl gnupg2 && \ + echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list && \ + curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ + curl -sL https://deb.nodesource.com/setup_lts.x | bash - && \ + apt-get install -y --no-install-recommends nodejs yarn && \ + npm --version && \ + apt-get clean && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* + + +RUN curl --location --silent https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ + && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \ + && apt-get update && apt-get install google-chrome-stable -y --no-install-recommends postgresql-client && \ + apt-get clean && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* + +# Install gh +RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ + && sudo apt update \ + && sudo apt install gh + + +FROM base AS build-deps + +RUN apt-get update && apt-get install -y --no-install-recommends git build-essential libpq-dev && \ + apt-get clean && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* + + +FROM build-deps AS gems + +RUN gem install bundler -v 2.3.11 + +COPY Gemfile Gemfile.lock ./ + +RUN bundle install && rm -rf vendor/bundle/ruby/*/cache + +FROM base AS node_modules + +COPY package.json yarn.lock ./ + +RUN yarn install --check-files + +FROM base + + +COPY --from=gems /app /app +COPY --from=gems /usr/local/bundle /usr/local/bundle +COPY --from=node_modules /app/node_modules /app/node_modules +COPY . ./ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000000..e1865dc0da --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,64 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose +{ + "name": "Miru Web Codespace", + // Update the 'dockerComposeFile' list if you have more compose files or use different names. + // The .devcontainer/docker-compose.yml file contains any overrides you need/want to make. + "dockerComposeFile": "docker-compose.yml", + // The 'service' property is the name of the service for the container that VS Code should + // use. Update this value and .devcontainer/docker-compose.yml to the real service name. + "service": "app", + // The optional 'workspaceFolder' property is the path VS Code should open by default when + // connected. This is typically a file mount in .devcontainer/docker-compose.yml + "workspaceFolder": "/app", + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + "customizations": { + "vscode": { + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "Shopify.ruby-lsp", + "misogi.ruby-rubocop", + "davidpallinder.rails-test-runner", + "eamodio.gitlens", + "github.copilot", + "mrmlnc.vscode-duplicate" + ], + "settings": { + "terminal.integrated.shell.linux": "/bin/zsh" + } + } + }, + // Use 'forwardPorts' to make a list of ports inside the container available locally. + "forwardPorts": [ + 3000, + 3035, + 9200 + ], + // Uncomment the next line if you want start specific services in your Docker Compose config. + // "runServices": [], + // Uncomment the next line if you want to keep your containers running after VS Code shuts down. + // "shutdownAction": "none", + // Uncomment the next line to run commands after the container is created. + // "postCreateCommand": "", + "postAttachCommand": "bundle exec rails s -p 3000 -b 0.0.0.0 && bundle exec rails db:prepare", + // Configure tool-specific properties. + // "customizations": {}, + // Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root. + "remoteEnv": { + "APP_BASE_URL": "${localEnv:CODESPACE_NAME}-3000.${localEnv:GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}", + }, + + "remoteUser": "root", + "portsAttributes": { + "3000": { + "label": "Rails Server" + }, + "3035": { + "label": "Webpack Dev Server" + }, + "9200": { + "label": "Elasticsearch" + } + } +} \ No newline at end of file diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml new file mode 100644 index 0000000000..f08fb4a390 --- /dev/null +++ b/.devcontainer/docker-compose.yml @@ -0,0 +1,126 @@ +version: "3.9" +services: + app: + build: + context: .. + dockerfile: .devcontainer/Dockerfile + # command: bash -c "bundle install && bundle exec rails db:prepare && bundle exec rails s -p 3000 -b 0.0.0.0" || sleep infinity + # Overrides default command so things don't shut down after the process ends. + command: sleep infinity + ports: + - "3000:3000" + image: miru-web:latest + working_dir: /app + volumes: + - ../:/app + - gem_cache:/usr/local/bundle/gems + - node_modules:/app/node_modules + environment: + - WEBPACKER_DEV_SERVER_HOST=0.0.0.0 + - DATABASE_URL=postgres://postgres:postgres@database:5432/ + - DB_HOST=database + - DB_USER=postgres + - DB_PASS=postgres + - DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL="true" + - POSTGRES_PASSWORD="postgres" + - POSTGRES_USER="postgres" + - REDIS_URL=redis://redis:6379/1 + - ELASTICSEARCH_URL=http://elastic:x2Sy5FK51zu@elasticsearch:9200 + - PIDFILE=/tmp/pids/server.pid + depends_on: + - database + - redis + - elasticsearch + tmpfs: + - /tmp/pids/ + # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. + # network_mode: service:database + # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + + database: + image: postgres:13.8 + restart: unless-stopped + # ports: + # - "5432:5432" + volumes: + - postgres:/var/lib/postgresql/data + environment: + POSTGRES_USER: postgres + POSTGRES_DB: postgres + POSTGRES_PASSWORD: postgres + # Your config/database.yml should use the user and password you set here, + # and host "db" (as that's the name of this service). You can use whatever + # database name you want. Use `bin/rails db:prepare` to create the database. + # + # Example: + # + # development: + # <<: *default + # host: db + # username: postgres + # password: postgres + # database: myapp_development + + # Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + redis: + image: redis:6.2.7 + command: redis-server + restart: always + ports: + - "6379:6379" + volumes: + - redis:/data + + webpack: + build: + context: .. + dockerfile: .devcontainer/Dockerfile + command: bin/webpacker-dev-server + image: miru-web:latest + ports: + - "3035:3035" + environment: + - WEBPACKER_DEV_SERVER_HOST=0.0.0.0 + - DATABASE_URL=postgres://postgres:postgres@database:5432/ + - DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL="true" + - POSTGRES_PASSWORD="postgres" + - POSTGRES_USER="postgres" + - REDIS_URL=redis://redis:6379/1 + - ELASTICSEARCH_URL=http://elastic:x2Sy5FK51zu@elasticsearch:9200 + depends_on: + - database + + sidekiq: + build: + context: .. + dockerfile: .devcontainer/Dockerfile + command: bundle exec sidekiq -C config/sidekiq.yml + volumes: + - ..:/app + - gem_cache:/usr/local/bundle/gems + - node_modules:/app/node_modules + environment: + - DATABASE_URL=postgres://postgres:postgres@database:5432/ + - REDIS_URL=redis://redis:6379/1 + depends_on: + - redis + - database + - app + + elasticsearch: + image: elasticsearch:7.13.3 + environment: + discovery.type: single-node + network.host: 0.0.0.0 + xpack.security.enabled: "true" + ELASTIC_PASSWORD: "x2Sy5FK51zu" + ports: + - "9200:9200" + +volumes: + gem_cache: + postgres: + node_modules: + redis: diff --git a/.vscode/settings.json b/.vscode/settings.json index f02d9b380e..f1b56ac572 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,22 +1,24 @@ { - "[ruby]": { - "editor.autoClosingBrackets": "beforeWhitespace", - "editor.formatOnSave": true, - "editor.defaultFormatter": "rebornix.ruby" - }, - "files.trimTrailingWhitespace": true, - "files.trimFinalNewlines": true, + "[ruby]": { + "editor.autoClosingBrackets": "beforeWhitespace", "editor.formatOnSave": true, - "yaml.format.enable": true, - "ruby.useBundler": true, //run non-lint commands with bundle exec - "ruby.useLanguageServer": true, // use the internal language server (see below) - "ruby.lint": { - "rubocop": { - "useBundler": true, - "lint": true, // enable lint cops - "rails": true // requires rubocop-rails gem for RuboCop >= 0.72.0 - } - }, - "ruby.format": "rubocop", - "ruby.intellisense": "rubyLocate" - } + "editor.defaultFormatter": "rebornix.ruby", + "editor.tabSize": 2, + }, + "files.trimTrailingWhitespace": true, + "files.trimFinalNewlines": true, + "editor.formatOnSave": true, + "yaml.format.enable": true, + "ruby.useBundler": true, //run non-lint commands with bundle exec + "ruby.useLanguageServer": true, // use the internal language server (see below) + "ruby.lint": { + "rubocop": { + "useBundler": true, + "lint": true, // enable lint cops + "rails": true // requires rubocop-rails gem for RuboCop >= 0.72.0 + } + }, + "ruby.format": "rubocop", + "ruby.intellisense": "rubyLocate", + "files.eol": "\n" +} \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index b6a77df184..bca62ea927 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,7 +1,7 @@ default: &default adapter: postgresql encoding: unicode - host: localhost + host: <%= ENV.fetch("DB_HOST") { "localhost" } %> pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> user: <%= ENV.fetch("DB_USER") { "" } %> password: <%= ENV.fetch("DB_PASS") { "" } %> diff --git a/config/environments/development.rb b/config/environments/development.rb index 88ecb3524a..5a5312d5d4 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -90,4 +90,14 @@ end config.serve_static_assets = true + + if ENV["CODESPACES"] == "true" + codespace_domain = ENV["GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"] + codespace_host = "#{ENV['CODESPACE_NAME']}-3000.#{codespace_domain}" + config.hosts << codespace_host + + config.action_dispatch.default_headers = { + "X-Frame-Options" => "ALLOW-FROM #{codespace_domain}" + } + end end