rails_caddy_dev 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -15
- data/bin/rails_caddy_dev +5 -4
- data/lib/rails_caddy_dev/update_config.rb +9 -18
- data/lib/rails_caddy_dev/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d16f9c9a33775c67c8b401c336720da85f5f4c4fa339f5d31d49791d099984f9
|
|
4
|
+
data.tar.gz: 4c271464f5ad808dd8a60ef93d3e6aeafe722270752caaaa62cf635cbddbfe71
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bd9c245265f8f913940ac2f593de24ecf9e14293f3328b75c347810b6bb57507e63f0825075a7085184cfc129dbc0866481123ba650db3a4db5f94192c121a9e
|
|
7
|
+
data.tar.gz: 2c7e83d684ed7cec4e76d9a141b2512af2a7f8876c729a4abc0b5e88f75647b4017985a3305e41978e0487a90ae60e7f4be56a23b74c19aa49f100baf3518014
|
data/README.md
CHANGED
|
@@ -53,35 +53,24 @@ This will configure Caddy to proxy `myapp.localhost` to your Rails server, acces
|
|
|
53
53
|
|---|---|---|
|
|
54
54
|
| `DEVCADDY` | Enables Caddy configuration (must be set) | — |
|
|
55
55
|
| `PROJECT_NAME` | Used to derive the `.localhost` hostname (required) | — |
|
|
56
|
-
| `PORT` | Rails server port to proxy to | `3000` |
|
|
56
|
+
| `PORT` | Rails server port to proxy to | Auto-detected available port (falls back to `3000`) |
|
|
57
57
|
| `CADDY_HOST` | Caddy admin API host | `localhost` |
|
|
58
58
|
| `CADDY_PORT` | Caddy admin API port | `2019` |
|
|
59
59
|
|
|
60
60
|
### Dynamic Port Allocation
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
If `PORT` is not set, the gem automatically detects an available TCP port and uses it. This means you can run multiple Rails servers without port conflicts — no manual configuration needed.
|
|
63
|
+
|
|
64
|
+
You can also use the standalone executable to find an available port yourself:
|
|
63
65
|
|
|
64
66
|
```bash
|
|
65
67
|
export PORT=$(bundle exec rails_caddy_dev port)
|
|
66
68
|
```
|
|
67
69
|
|
|
68
|
-
RailsCaddyDev will then configure Caddy to proxy to the dynamically allocated port, and start your Rails server on that port.
|
|
69
|
-
|
|
70
70
|
### Subdomains
|
|
71
71
|
|
|
72
72
|
Subdomains are also supported due to a wildcard Caddy route that will be configured. For example, if you set `PROJECT_NAME=myapp`, then `api.myapp.localhost` will also proxy to your Rails server.
|
|
73
73
|
|
|
74
|
-
### Usage with Mise
|
|
75
|
-
|
|
76
|
-
If you're using [Mise](https://github.com/joelmoss/mise), you can integrate RailsCaddyDev by setting the `PORT` environment variable dynamically using the CLI command provided by RailsCaddyDev. Add this to your Mise config (eg. `mise.toml`):
|
|
77
|
-
|
|
78
|
-
```toml
|
|
79
|
-
[env]
|
|
80
|
-
PORT = { value = "${env.PORT:-{{exec(command='bundle exec rails rails_caddy_dev:available_port')}}}", tools = true }
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
Now the `PORT` environment variable will be set to an available port each time you start your Mise environment, allowing you to run multiple Rails servers without port conflicts.
|
|
84
|
-
|
|
85
74
|
## Development
|
|
86
75
|
|
|
87
76
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/bin/rails_caddy_dev
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
2
3
|
|
|
3
4
|
if ARGV[0] == 'port'
|
|
4
|
-
require 'socket'
|
|
5
|
-
puts
|
|
5
|
+
require 'socket'
|
|
6
|
+
puts(Addrinfo.tcp('', 0).bind { |s| s.local_address.ip_port })
|
|
6
7
|
exit 0
|
|
7
8
|
else
|
|
8
|
-
puts
|
|
9
|
+
puts 'Usage: `rails_caddy_dev port` to get an available port number.'
|
|
9
10
|
exit 1
|
|
10
|
-
end
|
|
11
|
+
end
|
|
@@ -6,30 +6,17 @@
|
|
|
6
6
|
# `<project_name>.localhost` to the locally running Rails server.
|
|
7
7
|
#
|
|
8
8
|
# This file is intended to be run as part of the `rails server` startup process, and will only
|
|
9
|
-
# execute in the development environment, and when the `DEVCADDY` environment variable is set
|
|
10
|
-
# should be required from the application's 'bin/rails' file., making it look something like this:
|
|
11
|
-
#
|
|
12
|
-
# #!/usr/bin/env ruby
|
|
13
|
-
#
|
|
14
|
-
# APP_PATH = File.expand_path("../config/application", __dir__)
|
|
15
|
-
# require_relative "../config/boot"
|
|
16
|
-
#
|
|
17
|
-
# if ENV['RAILS_ENV'] == 'development' && ENV.key?('DEVCADDY') &&
|
|
18
|
-
# (ARGV.first == 's' || ARGV.first == 'server')
|
|
19
|
-
# require 'rails_caddy_dev/update_config'
|
|
20
|
-
# end
|
|
21
|
-
#
|
|
22
|
-
# require "rails/commands"
|
|
23
|
-
#
|
|
9
|
+
# execute in the development environment, and when the `DEVCADDY` environment variable is set.#
|
|
24
10
|
#
|
|
25
11
|
# The script checks if a Caddy config for the project already exists by querying the admin API. If
|
|
26
12
|
# it does, it sends a PATCH request to update it; if not, it attempts to append a new route to the
|
|
27
13
|
# existing config or initialize a new config structure if necessary.
|
|
28
14
|
#
|
|
29
|
-
# The script
|
|
15
|
+
# The script accepts the following environment variables:
|
|
30
16
|
# - PROJECT_NAME: Used to derive subdomain hostnames (e.g. "my_project" → my_project.localhost).
|
|
31
17
|
# This is required to ensure unique routing for each project.
|
|
32
|
-
# - PORT: Rails server port (
|
|
18
|
+
# - PORT: Rails server port (defaults to an available port if not set, or 3000 if port detection
|
|
19
|
+
# fails)
|
|
33
20
|
# - CADDY_HOST: Caddy admin API host (default: localhost)
|
|
34
21
|
# - CADDY_PORT: Caddy admin API port (default: 2019)
|
|
35
22
|
#
|
|
@@ -38,10 +25,14 @@
|
|
|
38
25
|
|
|
39
26
|
require 'net/http'
|
|
40
27
|
require 'json'
|
|
28
|
+
require 'socket'
|
|
41
29
|
|
|
42
30
|
return if ENV.fetch('RAILS_ENV', 'development') != 'development' || !ENV.key?('DEVCADDY')
|
|
43
31
|
|
|
44
|
-
PORT = ENV.fetch('PORT'
|
|
32
|
+
PORT = ENV['PORT'] = ENV.fetch('PORT') do
|
|
33
|
+
Addrinfo.tcp('', 0).bind { |s| s.local_address.ip_port }&.to_s || '3000'
|
|
34
|
+
end
|
|
35
|
+
|
|
45
36
|
CADDY_HOST = ENV.fetch('CADDY_HOST', 'localhost')
|
|
46
37
|
CADDY_PORT = ENV.fetch('CADDY_PORT', '2019').to_i
|
|
47
38
|
PROJECT_NAME = ENV.fetch('PROJECT_NAME')&.downcase
|