Skip to content

Commit

Permalink
feat(generator): generate anycable.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 9, 2024
1 parent d3bb4d4 commit a7b9285
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Generate `anycable.toml` in `anycable:setup` generator. ([@palkan][])

## 1.5.3 (2024-09-12)

- Set upper limit on supported Rails versions. ([@palkan][])
Expand Down
13 changes: 5 additions & 8 deletions lib/generators/anycable/setup/setup_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def configs
template "anycable.yml"
end

template "anycable.toml"

update_cable_yml
end

Expand Down Expand Up @@ -168,6 +170,7 @@ def install_for_docker
ANYCABLE_BROADCAST_ADAPTER: http
ANYCABLE_RPC_HOST: anycable:50051
ANYCABLE_DEBUG: ${ANYCABLE_DEBUG:-true}
ANYCABLE_SECRET: "anycable-local-secret"
anycable:
<<: *rails
Expand Down Expand Up @@ -208,6 +211,7 @@ def install_for_docker
ANYCABLE_BROADCAST_ADAPTER: http
ANYCABLE_RPC_HOST: http://rails:3000/_anycable
ANYCABLE_DEBUG: ${ANYCABLE_DEBUG:-true}
ANYCABLE_SECRET: "anycable-local-secret"
─────────────────────────────────────────
YML
end
Expand Down Expand Up @@ -266,18 +270,11 @@ def update_procfile(file_name)
end
end
unless contents.match?(/^ws:\s/)
append_file file_name, "ws: bin/anycable-go #{anycable_go_options}", force: true
append_file file_name, "ws: bin/anycable-go", force: true
end
end
end

def anycable_go_options
opts = ["--port=8080"]
opts << "--broadcast_adapter=http" unless redis?
opts << "--rpc_host=http://localhost:3000/_anycable" if http_rpc?
opts.join(" ")
end

def file_exists?(name)
in_root do
return File.file?(name)
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/anycable/setup/templates/Procfile.dev.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ web: bin/rails s
<%- unless http_rpc? -%>
anycable: bundle exec anycable
<%- end -%>
ws: bin/anycable-go <%= anycable_go_options %>
ws: bin/anycable-go
83 changes: 83 additions & 0 deletions lib/generators/anycable/setup/templates/anycable.toml.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# AnyCable server configuration (development).
#
# Read more at https://docs.anycable.io/anycable-go/configuration

# Public mode disables connection authentication, pub/sub streams and broadcasts verification
# public = false

# The application secret key
secret = "anycable-local-secret"

# Broadcasting adapters for app-to-clients messages
<%- if redis? -%>
broadcast_adapters = ["http", "redisx"]
<%- elsif nats? -%>
broadcast_adapters = ["http", "nats"]
<%- else -%>
broadcast_adapters = ["http"]
<%- end -%>

# Pub/sub adapter for inter-node communication
<%- if redis? -%>
pubsub_adapter = "redis"
<%- elsif nats? -%>
pubsub_adapter = "nats"
<%- else -%>
# pubsub_adapter = "redis" # or "nats"
<%- end -%>

[server]
host = "localhost"
port = 8080

[logging]
debug = true

# Read more about broker: https://docs.anycable.io/anycable-go/reliable_streams
[broker]
adapter = "memory"
history_ttl = 300
history_limit = 100
sessions_ttl = 300

[rpc]
<%- if http_rpc? -%>
host = "http://localhost:3000/_anycable"
<%- else -%>
host = "localhost:50051"
<%- end -%>
# Specify HTTP headers that must be proxied to the RPC service
proxy_headers = ["cookie"]
# RPC concurrency (max number of concurrent RPC requests)
concurrency = 28

# Read more about AnyCable JWT: https://docs.anycable.io/anycable-go/jwt_identification
[jwt]
# param = "jid"
# force = true

# Read more about AnyCable signed streams: https://docs.anycable.io/anycable-go/signed_streams
[streams]
# Enable public (unsigned) streams
# public = true
# Enable whispering support for pub/sub streams
# whisper = true
pubsub_channel = "$pubsub"
# turbo = true
# cable_ready = true

[redis]
<%- if redis? -%>
url = "redis://localhost:6379"
<%- else -%>
# url = "redis://localhost:6379"
<%- end -%>

<%- if nats? -%>
[nats]
servers = "nats://127.0.0.1:4222"
<%- end -%>

[http_broadcast]
port = 8090
path = "/_broadcast"
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ default: &default
# Whether to enable gRPC level logging or not
log_grpc: false
<%- if redis? -%>
# Use Redis to broadcast messages to AnyCable server
broadcast_adapter: redis
# Use Redis Streams to broadcast messages to AnyCable server
broadcast_adapter: redisx
<%- elsif nats? -%>
# Use NATS to broadcast messages to AnyCable server
broadcast_adapter: nats
<%- else -%>
# Use HTTP broadcaster
broadcast_adapter: http
http_broadcast_url: "http://localhost:8090/_anycable"
<%- end -%>
<%- if redis? -%>
# You can use REDIS_URL env var to configure Redis URL.
Expand All @@ -37,6 +38,8 @@ default: &default
# Read more about AnyCable RPC: <%= DOCS_ROOT %>/anycable-go/rpc
http_rpc_mount_path: "/_anycable"
<%- end -%>
# Must be the same as in your AnyCable server config
secret: "anycable-local-secret"

development:
<<: *default
Expand All @@ -51,3 +54,4 @@ test:
production:
<<: *default
websocket_url: ~
secret: ~
16 changes: 10 additions & 6 deletions spec/generators/setup/setup_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
expect(file("config/cable.yml")).to exist
expect(file("config/cable.yml")).to contain(%(adapter: <%= ENV.fetch("ACTION_CABLE_ADAPTER", "any_cable") %>))
expect(file("config/anycable.yml")).to contain("broadcast_adapter: http")
expect(file("anycable.toml")).to contain('broadcast_adapters = ["http"]')
end

context "when cable.yml is present" do
Expand Down Expand Up @@ -84,10 +85,12 @@
)
end

it "anycable.yml use redis broadcast adapter" do
it "uses redisx broadcast adapter" do
subject
expect(file("config/anycable.yml")).not_to contain("broadcast_adapter: http")
expect(file("config/anycable.yml")).to contain("broadcast_adapter: redis")
expect(file("config/anycable.yml")).to contain("broadcast_adapter: redisx")
expect(file("anycable.toml")).to contain('broadcast_adapters = ["http", "redisx"]')
expect(file("anycable.toml")).to contain('pubsub_adapter = "redis"')
end
end

Expand All @@ -97,6 +100,7 @@
it "anycable.yml use redis broadcast adapter" do
subject
expect(file("config/anycable.yml")).to contain(%( http_rpc_mount_path: "/_anycable"))
expect(file("anycable.toml")).to contain('host = "http://localhost:3000/_anycable"')
end
end
end
Expand Down Expand Up @@ -126,7 +130,7 @@
expect(subject)
.to contain("anycable: bundle exec anycable")
expect(subject)
.to contain("ws: bin/anycable-go --port=8080 --broadcast_adapter=http")
.to contain("ws: bin/anycable-go")
end
end

Expand All @@ -137,7 +141,7 @@
expect(subject)
.to contain("anycable: bundle exec anycable")
expect(subject)
.to contain("ws: bin/anycable-go --port=8080 --broadcast_adapter=http")
.to contain("ws: bin/anycable-go")
end

context "when redis is in the deps" do
Expand All @@ -156,7 +160,7 @@
expect(subject)
.to contain("anycable: bundle exec anycable")
expect(subject)
.to contain("ws: bin/anycable-go --port=8080\n")
.to contain("ws: bin/anycable-go")
end
end

Expand All @@ -167,7 +171,7 @@
expect(subject)
.not_to contain("anycable: bundle exec anycable")
expect(subject)
.to contain("ws: bin/anycable-go --port=8080 --broadcast_adapter=http --rpc_host=http://localhost:3000/_anycable\n")
.to contain("ws: bin/anycable-go")
end
end
end
Expand Down

0 comments on commit a7b9285

Please sign in to comment.