@@ -104,50 +104,82 @@ public function asNodeTasks(): array
104
104
105
105
$ this ->latestDeployments ()->each (function ($ deployment ) use (&$ caddyHandlers ) {
106
106
foreach ($ deployment ->data ->processes as $ process ) {
107
- $ caddyHandlers [] = collect ($ process ->caddy )->map (fn (Caddy $ caddy ) => [
108
- 'apps ' =>[
109
- 'http ' => [
110
- 'servers ' => [
111
- "listen_ {$ caddy ->publishedPort }" => [
112
- 'listen ' => [
113
- "0.0.0.0: {$ caddy ->publishedPort }" ,
107
+ $ caddyHandlers [] = collect ($ process ->caddy )->map (function (Caddy $ caddy ) use ($ deployment , $ process ) {
108
+ $ routes = [];
109
+
110
+ $ routes [] = [
111
+ 'match ' => [
112
+ [
113
+ 'host ' => [$ caddy ->domain ],
114
+ 'path ' => [$ caddy ->path ],
115
+ ],
116
+ ],
117
+ 'handle ' => [
118
+ [
119
+ 'handler ' => 'reverse_proxy ' ,
120
+ 'headers ' => [
121
+ 'request ' => [
122
+ 'set ' => $ this ->getForwardedHeaders ($ caddy ),
114
123
],
115
- 'routes ' => [
116
- [
117
- 'match ' => [
118
- [
119
- 'host ' => [$ caddy ->domain ],
120
- 'path ' => [$ caddy ->path ],
121
- ],
122
- ],
123
- 'handle ' => [
124
- [
125
- 'handler ' => 'reverse_proxy ' ,
126
- 'headers ' => [
127
- 'request ' => [
128
- 'set ' => $ this ->getForwardedHeaders ($ caddy ),
129
- ],
130
- 'response ' => [
131
- 'set ' => [
132
- 'X-Powered-By ' => ['https://ptah.sh ' ],
133
- ],
134
- ],
135
- ],
136
- 'transport ' => $ this ->getTransportOptions ($ caddy , $ process ),
137
- 'upstreams ' => [
138
- [
139
- 'dial ' => "{$ process ->name }. {$ deployment ->data ->internalDomain }: {$ caddy ->targetPort }" ,
140
- ]
141
- ]
142
- ]
143
- ]
124
+ 'response ' => [
125
+ 'set ' => [
126
+ 'X-Powered-By ' => ['https://ptah.sh ' ],
144
127
],
128
+ ],
129
+ ],
130
+ 'transport ' => $ this ->getTransportOptions ($ caddy , $ process ),
131
+ 'upstreams ' => [
132
+ [
133
+ 'dial ' => "{$ process ->name }. {$ deployment ->data ->internalDomain }: {$ caddy ->targetPort }" ,
145
134
]
146
135
]
147
136
]
148
137
]
149
- ]
150
- ])->toArray ();
138
+ ];
139
+
140
+ foreach ($ process ->redirectRules as $ redirectRule ) {
141
+ $ regexpName = dockerize_name ($ redirectRule ->id );
142
+
143
+ $ pathTo = preg_replace ("/ \\$(\d+)/ " , "{http.regexp. $ regexpName.$1} " , $ redirectRule ->pathTo );
144
+
145
+ $ routes [] = [
146
+ 'match ' => [
147
+ [
148
+ 'host ' => [$ redirectRule ->domainFrom ],
149
+ 'path_regexp ' => [
150
+ 'name ' => $ regexpName ,
151
+ 'pattern ' => $ redirectRule ->pathFrom
152
+ ],
153
+ ]
154
+ ],
155
+ 'handle ' => [
156
+ [
157
+ 'handler ' => 'static_response ' ,
158
+ 'status_code ' => (string ) $ redirectRule ->statusCode ,
159
+ 'headers ' => [
160
+ 'X-Powered-By ' => ['https://ptah.sh ' ],
161
+ 'Location ' => ["{http.request.scheme}:// {$ redirectRule ->domainTo }{$ pathTo }" ],
162
+ ],
163
+ ]
164
+ ]
165
+ ];
166
+ }
167
+
168
+ return [
169
+ 'apps ' => [
170
+ 'http ' => [
171
+ 'servers ' => [
172
+ "listen_ {$ caddy ->publishedPort }" => [
173
+ 'listen ' => [
174
+ "0.0.0.0: {$ caddy ->publishedPort }" ,
175
+ ],
176
+ 'routes ' => $ routes ,
177
+ ]
178
+ ]
179
+ ]
180
+ ]
181
+ ];
182
+ })->toArray ();
151
183
}
152
184
});
153
185
@@ -250,6 +282,14 @@ protected function sortRoutes($routes): array
250
282
251
283
protected function getRouteWeights ($ route ): array
252
284
{
285
+ // Let's prioritize regexp routes to be first to execute
286
+ if (isset ($ route ['match ' ][0 ]['path_regexp ' ])) {
287
+ return [
288
+ 'segments ' => 0 ,
289
+ 'wildcards ' => -100 ,
290
+ ];
291
+ }
292
+
253
293
$ path = $ route ['match ' ][0 ]['path ' ][0 ];
254
294
255
295
$ pathSegments = count (explode ('/ ' , $ path )) * -1 ;
0 commit comments