Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Efficient ActionCable replacement to use web-sockets
gem 'anycable-rails', '~> 0.6', github: "anycable/anycable-rails", branch: "feature/rack-middleware"

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

Expand Down
20 changes: 20 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
GIT
remote: https://github.com/anycable/anycable-rails.git
revision: 59ebcbf9033725637b08dee5cbd5e26891dfc354
branch: feature/rack-middleware
specs:
anycable-rails (0.6.4)
anycable (~> 0.6.0)
rails (>= 5)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -42,6 +51,10 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
anycable (0.6.3)
anyway_config (~> 1.4.2)
grpc (~> 1.17)
anyway_config (1.4.4)
arel (9.0.0)
bindex (0.8.1)
bootsnap (1.4.4)
Expand All @@ -55,6 +68,12 @@ GEM
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
google-protobuf (3.10.1)
googleapis-common-protos-types (1.0.4)
google-protobuf (~> 3.0)
grpc (1.25.0)
google-protobuf (~> 3.8)
googleapis-common-protos-types (~> 1.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jbuilder (2.9.1)
Expand Down Expand Up @@ -153,6 +172,7 @@ PLATFORMS
ruby

DEPENDENCIES
anycable-rails (~> 0.6)!
bootsnap (>= 1.1.0)
byebug
jbuilder (~> 2.5)
Expand Down
3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server: bin/rails server
web: bin/heroku-web
release: bundle exec rails db:migrate
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
server: bin/rails server
assets: bin/webpack-dev-server
anycable: bundle exec anycable --server-command "anycable-go --port 3334"
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ If you are looking for code that reflects application at the end of any part, ta
- [part-2 branch](https://github.com/demiazz/evil_chat/tree/part-2) for the Part 2;
- [part-3 branch](https://github.com/demiazz/evil_chat/tree/part-3) for the Part 3.

# Installation
# Development installation

## When using localhost machine

```sh
cp config/database.yml.example config/database.yml

bundle install
bin/rails db:migrate
yarn install
brew install hivemind
brew install anycable-go
hivemind Procfile.dev
```
$ cp config/database.yml.example config/database.yml

$ bin/rails credentials:edit
## When using Docker Compose

$ bundle install
$ bin/rails db:migrate
$ yarn install
$ brew install hivemind
$ hivemind Procfile.dev
```sh
brew install bibendi/dip/dip
eval "$(dip console)"
provision
rails s
```
2 changes: 1 addition & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = request.session.fetch("username", nil)
self.current_user = session.fetch("username", nil)
reject_unauthorized_connection unless current_user
end
end
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>EvilChat</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= action_cable_meta_tag %>

<%= stylesheet_pack_tag 'application', media: 'all' %>
</head>
Expand Down
7 changes: 7 additions & 0 deletions bin/heroku-web
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

if [ "$ANYCABLE_DEPLOYMENT" == "true" ]; then
bundle exec anycable --server-command="anycable-go"
else
bundle exec rails server -p $PORT -b 0.0.0.0
fi
30 changes: 30 additions & 0 deletions config/anycable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file contains per-environment settings for AnyCable.
#
# Since AnyCable config is based on anyway_config (https://github.com/palkan/anyway_config), all AnyCable settings
# can be set or overridden through the corresponding environment variables.
# E.g., `rpc_host` is overridden by ANYCABLE_RPC_HOST, `debug` by ANYCABLE_DEBUG etc.
#
# Note that AnyCable recognizes REDIS_URL env variable for Redis pub/sub adapter. If you want to
# use another Redis instance for AnyCable, provide ANYCABLE_REDIS_URL variable.
#
# Read more about AnyCable configuration here: https://docs.anycable.io/#/ruby/configuration
#
default: &default
# Turn on/off access logs ("Started..." and "Finished...")
access_logs_disabled: false
# This is the host and the port to run AnyCable RPC server on.
# You must configure your WebSocket server to connect to it, e.g.:
# $ anycable-go --rpc-host="<rpc hostname>:50051"
rpc_host: "127.0.0.1:50051"
# Whether to enable gRPC level logging or not
log_grpc: false
# Use the same channel name for WebSocket server, e.g.:
# $ anycable-go --redis-channel="__anycable__"
redis_channel: "__anycable__"

development:
<<: *default
redis_url: "redis://localhost:6379/1"

production:
<<: *default
11 changes: 6 additions & 5 deletions config/cable.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Make it possible to switch adapters by passing the CABLE_ADAPTER env variable.
# For example, you can use it fallback to the standard Action Cable in staging/review
# environments (by setting `CABLE_ADAPTER=redis`).
development:
adapter: async
adapter: <%= ENV.fetch("CABLE_ADAPTER", "any_cable") %>

test:
adapter: async
adapter: test

production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: evil_chat_production
adapter: <%= ENV.fetch("CABLE_ADAPTER", "any_cable") %>
5 changes: 5 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down Expand Up @@ -51,4 +53,7 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

# Specify AnyCable WebSocket server URL to use by JS client
config.action_cable.url = ENV.fetch("CABLE_URL", "ws://localhost:3334/cable").presence
end
6 changes: 5 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand All @@ -16,7 +18,7 @@

# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
config.require_master_key = true
# config.require_master_key = true

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
Expand All @@ -35,6 +37,8 @@
# Mount Action Cable outside main process or domain
# config.action_cable.mount_path = nil
# config.action_cable.url = 'wss://example.com/cable'
# Specify AnyCable WebSocket server URL to use by JS client
config.action_cable.url = ENV["CABLE_URL"].presence
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
Expand Down
1 change: 0 additions & 1 deletion config/master.key.back

This file was deleted.

26 changes: 25 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ services:
EDITOR: vim
RAILS_ENV: ${RAILS_ENV:-development}
DATABASE_URL: postgres://postgres:postgres@postgres:5432
REDIS_URL: redis://redis:6379/
REDIS_URL: redis://redis:6379/0
ANYCABLE_REDIS_URL: redis://redis:6379/0
ANYCABLE_RPC_HOST: 0.0.0.0:50051
WEBPACKER_DEV_SERVER_HOST: webpacker
DISABLE_SPRING: 1
depends_on:
Expand All @@ -55,6 +57,10 @@ services:
condition: service_healthy
webpacker:
condition: service_started
anycable-ws:
condition: service_started
anycable-rpc:
condition: service_started

webpacker:
<<: *app
Expand All @@ -69,6 +75,24 @@ services:
RAILS_ENV: ${RAILS_ENV:-development}
WEBPACKER_DEV_SERVER_HOST: 0.0.0.0

anycable-ws:
image: anycable/anycable-go:v0.6.4
ports:
- '3334:3334'
environment:
PORT: 3334
ANYCABLE_REDIS_URL: redis://redis:6379/0
ANYCABLE_RPC_HOST: anycable-rpc:50051
depends_on:
- anycable-rpc
- redis

anycable-rpc:
<<: *backend
command: bundle exec anycable
ports:
- '50051'

postgres:
image: postgres:12
volumes:
Expand Down