Skip to content

Commit 1120f3c

Browse files
committed
fix readme, use caddy_anycable correctly and set PORT by target-port
1 parent 7a09cc3 commit 1120f3c

5 files changed

Lines changed: 26 additions & 22 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Caddy Thruster
1+
# Caddy Rails
22

33
CaddyRails is a reverse proxy module for Ruby on Rails designed to integrate with Caddy, facilitating features like reverse proxying, automatic HTTPS, compression, and more.
44

55
## Features
66

77
- **Reverse Proxy:** Simplifies forwarding requests to your application.
88
- **Automatic HTTPS:** Automatically manages SSL/TLS certificates. (Currently in progress)
9-
- **Compression:** Supports Gzip, Brotli and Zstd for reducing data transfer sizes.
9+
- **Compression:** Supports Gzip, and Zstd for reducing data transfer sizes.
1010
- **Access Logging:** Enables detailed logging of incoming requests.
1111
- **Connection Timeouts:** Customizable read, write, and idle timeouts for connections.
1212
- **Debugging:** Provides extensive debug logs to troubleshoot issues.
@@ -32,14 +32,14 @@ The tool sets up a reverse proxy automatically.
3232
```
3333

3434
### Command Line Arguments
35-
- `--target-port`: The port that your server should run on. ProxyRunner will set the PORT environment variable to this value. Default: `3000`.
35+
- `--target-port`: The port that your server should run on. caddy-server will set this value to the PORT environment variable. Default: `3000`.
3636
- `--http-port`: The port to listen on for HTTP traffic. Default: `80`.
3737
- `--https-port`: The port to listen on for HTTPS traffic. Default: `443`.
3838
- `-l, --listen`: The address to which to bind the listener. Default: `localhost`.
39-
- `--ssl-domain`: The domain name to use for SSL provisioning. If not set, SSL will be disabled. (in progress)
39+
- `--ssl-domain`: The domain name to use for SSL provisioning. If not set, SSL will be disabled.
4040
- `-v, --debug`: Enable verbose debug logs.
4141
- `--access-log`: Enable the access log. Default: `true`.
42-
- `--no-compress`: Disable Brotli, Zstandard and Gzip compression
42+
- `--no-compress`: Disable Zstandard and Gzip compression
4343
- `--http-idle-timeout`: The maximum time a client can be idle before the connection is closed. Default: `60s`.
4444
- `--http-read-timeout`: The maximum time a client can take to send the request headers. Default: `30s`.
4545
- `--http-write-timeout`: The maximum time during which the client must read the response. Default: `30s`.
@@ -67,7 +67,6 @@ This command creates a Caddyfile in the specified directory, tailoring it with o
6767
- `--access-log`: Enable the access log. Default: `true`
6868
- `--ssl-domain`: The domain name for SSL. If empty, SSL is disabled
6969
- `--backend-port`: THe port that the backend service listens on. Default is `8080`
70-
- `--https-enable`: Enable HTTPS configuration. Default `false`
7170
- `--compression-enable`: Enable response compression using gzip and zstd
7271
- `--anycable-enable`: Enable anycable
7372

cmd/caddy_rails/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"flag"
55
"fmt"
66
caddycmd "github.com/caddyserver/caddy/v2/cmd"
7-
_ "github.com/evilmartians/caddy_anycable"
87
_ "github.com/evilmartians/caddy_rails/internal/app"
98
_ "github.com/evilmartians/caddy_rails/internal/commands"
109
"github.com/evilmartians/caddy_rails/version"

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22.1
44

55
require (
66
github.com/caddyserver/caddy/v2 v2.8.4
7-
github.com/caddyserver/certmagic v0.21.3
7+
github.com/evilmartians/caddy_anycable v0.0.0-20240603141509-6f07f068c620
88
github.com/spf13/cobra v1.8.0
99
github.com/stretchr/testify v1.9.0
1010
go.uber.org/zap v1.27.0
@@ -14,7 +14,6 @@ require (
1414
github.com/FZambia/sentinel v1.1.1 // indirect
1515
github.com/anycable/anycable-go v1.5.1 // indirect
1616
github.com/bufbuild/protocompile v0.13.0 // indirect
17-
github.com/evilmartians/caddy_anycable v0.0.0-20240603141509-6f07f068c620
1817
github.com/fullstorydev/grpchan v1.1.1 // indirect
1918
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
2019
github.com/gomodule/redigo v1.9.2 // indirect
@@ -53,6 +52,7 @@ require (
5352
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
5453
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
5554
github.com/beorn7/perks v1.0.1 // indirect
55+
github.com/caddyserver/certmagic v0.21.3 // indirect
5656
github.com/caddyserver/zerossl v0.1.3 // indirect
5757
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
5858
github.com/cespare/xxhash v1.1.0 // indirect

internal/commands/serve_rails.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ sets up a reverse proxy to forward requests to it.`,
2525
cmd.Flags().Bool("phased-restart", false, "Perform a phased restart of the server")
2626
cmd.Flags().Bool("anycable-enabled", false, "Activate AnyCable")
2727
cmd.Flags().String("server-type", "puma", "The type of server (puma or unicorn) to control")
28-
cmd.Flags().Int("target-port", 3000, "The port that your server should run on. ProxyRunner will set PORT to this value when starting your server.")
28+
cmd.Flags().Int("target-port", 3000, "The port that your server should run on. caddy-rails will set this values to the PORT env variable when starting your server.")
2929
cmd.Flags().Int("http-port", 80, "The port to listen on for HTTP traffic.")
3030
cmd.Flags().Int("https-port", 443, "The port to listen on for HTTPS traffic.")
3131
cmd.Flags().StringP("listen", "l", "localhost", "The address to which to bind the listener")
@@ -101,6 +101,9 @@ func loadConfigIfNeeded() bool {
101101
}
102102

103103
func runUpstreamProcess(fs caddycmd.Flags, pidFile string) (int, error) {
104+
// Set PORT to be inherited by the upstream process.
105+
os.Setenv("PORT", fmt.Sprintf("%d", fs.Int("target-port")))
106+
104107
upstream := utils.NewUpstreamProcess(fs.Arg(0), fs.Args()[1:], true, pidFile)
105108
exitCode, err := upstream.Run()
106109
if err != nil {

internal/utils/handlers.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,36 @@ func createEncodeRoute() caddyhttp.Route {
6161

6262
func createAnyCableRoute() caddyhttp.Route {
6363
anyCableHandler := caddy_anycable.AnyCableHandler{}
64-
6564
anyCableOptions := os.Getenv("ANYCABLE_OPT")
6665
if anyCableOptions != "" {
6766
options := strings.Split(anyCableOptions, " ")
6867
anyCableHandler.Options = options
6968
}
7069

71-
if err := anyCableHandler.Provision(caddy.Context{}); err != nil {
72-
log.Fatalf("Failed to provision anycable handler: %v", err)
73-
}
74-
7570
return caddyhttp.Route{
7671
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(anyCableHandler, "handler", "anycable", nil)},
72+
MatcherSetsRaw: []caddy.ModuleMap{
73+
{
74+
"path": caddyconfig.JSON(caddyhttp.MatchPath{"/cable*"}, nil),
75+
},
76+
},
7777
}
7878
}
7979

8080
func createReverseProxyRoute(fs cmd.Flags) caddyhttp.Route {
8181
reverseProxyHandler := reverseproxy.Handler{
82-
TransportRaw: caddyconfig.JSONModuleObject(reverseproxy.HTTPTransport{}, "protocol", "http", nil),
8382
Upstreams: reverseproxy.UpstreamPool{
8483
{Dial: fmt.Sprintf("%s:%d", fs.String("listen"), fs.Int("target-port"))},
8584
},
8685
Headers: &headers.Handler{
8786
Request: &headers.HeaderOps{
88-
Set: map[string][]string{"Host": {"{http.reverse_proxy.upstream.hostport}"}},
87+
Set: map[string][]string{
88+
"Host": {"{http.reverse_proxy.upstream.hostport}"},
89+
},
8990
},
9091
},
9192
}
93+
9294
return caddyhttp.Route{
9395
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(reverseProxyHandler, "handler", "reverse_proxy", nil)},
9496
}
@@ -162,19 +164,20 @@ func createCaddyConfig(httpApp caddyhttp.App, debug bool) *caddy.Config {
162164
func createGroupedRoutes(fs cmd.Flags) caddyhttp.Route {
163165
routes := caddyhttp.RouteList{}
164166

167+
if fs.Bool("anycable-enabled") {
168+
routes = append(routes, createAnyCableRoute())
169+
}
170+
171+
routes = append(routes, createReverseProxyRoute(fs))
172+
165173
if !fs.Bool("no-compress") {
166174
routes = append(routes, createEncodeRoute())
167175
}
168176

169177
routes = append(routes,
170-
createReverseProxyRoute(fs),
171178
createFileServerRoute(fs),
172179
)
173180

174-
if fs.Bool("anycable-enabled") {
175-
routes = append(routes, createAnyCableRoute())
176-
}
177-
178181
subroute := caddyhttp.Subroute{Routes: routes}
179182

180183
return caddyhttp.Route{

0 commit comments

Comments
 (0)