Skip to content

Commit ee773d9

Browse files
committed
feat: #35 add 404 handler to caddy
1 parent 22182f8 commit ee773d9

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

app/Models/Deployment.php

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
1616
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
1717
use Illuminate\Database\Query\Builder as QueryBuilder;
18+
use Illuminate\Support\Facades\Blade;
1819
use InvalidArgumentException;
1920

2021
class Deployment extends Model
@@ -208,6 +209,26 @@ public function asNodeTasks(): array
208209
foreach ($caddy['apps']['http']['servers'] as $name => $value) {
209210
$caddy['apps']['http']['servers'][$name]['listen'] = array_unique($value['listen']);
210211
$caddy['apps']['http']['servers'][$name]['routes'] = $this->sortRoutes($value['routes']);
212+
213+
$caddy['apps']['http']['servers'][$name]['routes'][] = [
214+
'match' => [
215+
[
216+
'host' => ['*'],
217+
'path' => ['/*'],
218+
]
219+
],
220+
'handle' => [
221+
[
222+
'handler' => 'static_response',
223+
'status_code' => '404',
224+
'headers' => [
225+
'X-Powered-By' => ['https://ptah.sh'],
226+
'Content-Type' => ['text/html; charset=utf-8'],
227+
],
228+
'body' => file_get_contents(resource_path('support/caddy/404.html')),
229+
]
230+
]
231+
];
211232
}
212233

213234
$caddyTask[] = [

resources/support/caddy/404.html

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>404 Not Found</title>
5+
<style>
6+
body {
7+
text-align: center;
8+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
9+
height: 100vh;
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
justify-content: space-evenly;
14+
}
15+
16+
.home-btn {
17+
display: inline-block;
18+
background-color: #007bff;
19+
color: #fff;
20+
padding: 10px 20px;
21+
border-radius: 5px;
22+
text-decoration: none;
23+
margin-top: 20px;
24+
}
25+
26+
.home-btn:hover {
27+
background-color: #0069d9;
28+
}
29+
30+
.powered-by {
31+
margin-top: 20px;
32+
color: #999;
33+
}
34+
</style>
35+
36+
<script>
37+
window.addEventListener('DOMContentLoaded', () => {
38+
if (window.location.pathname === "/") {
39+
document.getElementById('goHome').remove();
40+
}
41+
})
42+
</script>
43+
</head>
44+
<body>
45+
<div>
46+
<h1>404 Not Found</h1>
47+
<p>The requested URL was not found on this server.</p>
48+
49+
<p>Please contact the server administrator if you believe this is an error.</p>
50+
51+
<a id="goHome" class="home-btn" href="/">Return to the Home Page</a>
52+
</div>
53+
54+
<p class="powered-by"><small>Powered by <a href="https://ptah.sh">ptah.sh</a></small></p>
55+
</body>
56+
</html>

0 commit comments

Comments
 (0)