Ini lebih merupakan bukti konsep daripada proyek yang sepenuhnya berfungsi. Ini mencoba mereplikasi struktur API Caddy JSON untuk bekerja melalui kelas PHP yang dapat rantai.
Saat ini hanya ada sebagian kecil perintah yang tersedia dari Caddy 2.0 yang mencakup kasus penggunaan saya saat ini yang dibutuhkan.
composer require mattvb91/caddy-phpContoh dasar server HTTP dengan respons statis:
$ caddy = new Caddy ();
$ caddy -> addApp (
( new Http ())-> addServer (
' server1 ' , ( new Http Server ())-> addRoute (
( new Route ())-> addHandle (
new StaticResponse ( ' Hello world ' , 200 )
)
))
);
$ caddy -> load ();Ini akan menghasilkan konfigurasi caddy berikut:
{
"admin" : {
"disabled" : false ,
"listen" : " :2019 "
},
"apps" : {
"http" : {
"servers" : {
"server1" : {
"listen" : [
" :80 "
],
"routes" : [
{
"handle" : [
{
"handler" : " static_response " ,
"body" : " Hello world " ,
"status_code" : 200
}
]
}
]
}
}
}
}
}curl -v localhost
-----
< HTTP/1.1 200 OK
< Server: Caddy
Hello world Jika Anda mengelola nama host secara dinamis (dalam database) dan tidak dapat membangun konfigurasi dengan daftar nama host yang ada karena Anda perlu mengelolanya saat runtime Anda dapat melakukan hal berikut:
Bagian penting dalam contoh ini adalah pengidentifikasi host_group_name yang kemudian digunakan untuk menambahkan / menghapus domain ke host ini.
$ caddy = new Caddy ();
$ caddy -> addApp (
( new Http ())-> addServer (
' server1 ' , ( new Http Server ())-> addRoute (
( new Route ())-> addHandle (
new StaticResponse ( ' host test ' , 200 )
)-> addMatch (( new Host ( ' host_group_name ' ))
-> setHosts ([ ' localhost ' ])
)
)-> addRoute (( new Route ())
-> addHandle ( new StaticResponse ( ' Not found ' , 404 ))
-> addMatch (( new Host ( ' notFound ' ))
-> setHosts ([ ' *.localhost ' ])
)
))
);
$ caddy -> load ();Sekarang nanti dalam skrip atau acara di sistem Anda, Anda bisa mendapatkan objek konfigurasi caddy Anda dan memposting domain baru ke dalamnya di bawah rute itu:
$ caddy -> addHostname ( ' host_group_name ' , ' new.localhost ' )
$ caddy -> addHostname ( ' host_group_name ' , ' another.localhost ' )curl -v new.localhost
> GET / HTTP/1.1
> Host: new.localhost
>
< HTTP/1.1 200 OK
curl -v another.localhost
> GET / HTTP/1.1
> Host: another.localhost
>
< HTTP/1.1 200 OK $ caddy -> syncHosts ( ' host_group_name ' ); //Sync from caddy current hostname list
$ caddy -> removeHostname ( ' host_group_name ' , ' new.localhost ' );
$ caddy -> removeHostname ( ' host_group_name ' , ' another.localhost ' );curl -v new.localhost
> GET / HTTP/1.1
> Host: new.localhost
>
< HTTP/1.1 404 Not Found
curl -v another.localhost
> GET / HTTP/1.1
> Host: another.localhost
>
< HTTP/1.1 404 Not Found Mari kita ambil kasus di mana Anda ingin memiliki frontend simpul dan php backend mengambil permintaan pada rute /api/* . Dalam hal ini contoh dipecah menjadi 2 proxy terbalik dengan pencocokan rute untuk memfilter /api/* ke hulu PHP.
Ini mengasumsikan 3 host (Caddy, Node, PHP) adalah semua wadah Docker dan dapat diakses dengan nama kontainer dalam jaringan Docker yang sama, jadi Anda mungkin harus menyesuaikan nama host Anda sesuai kebutuhan.
use mattvb91 CaddyPhp Caddy ;
use mattvb91 CaddyPhp Config Apps Http ;
use mattvb91 CaddyPhp Config Apps Http Server ;
use mattvb91 CaddyPhp Config Apps Http Server Route ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle ReverseProxy ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle ReverseProxy Transport FastCGI ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle ReverseProxy Upstream ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Handle Subroute ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Match Host ;
use mattvb91 CaddyPhp Config Apps Http Server Routes Match Path ;
$ apiReverseProxy = ( new ReverseProxy ())
-> addUpstream (( new Upstream ())
-> setDial ( ' laravel-api:9000 ' )
)-> addTransport (( new FastCGI ())
-> setRoot ( ' /app/public/index.php ' )
-> setSplitPath ([ '' ])
);
$ apiMatchPath = ( new Path ())
-> setPaths ([
' /api/* ' ,
]);
$ backendAPIRoute = ( new Route ())
-> addHandle ( $ apiReverseProxy )
-> addMatch ( $ apiMatchPath );
$ route = new Route ();
$ route -> addHandle (( new Subroute ())
-> addRoute ( $ backendAPIRoute )
-> addRoute (( new Route ())
-> addHandle (( new ReverseProxy ())
-> addUpstream (( new Upstream ())
-> setDial ( ' nextjs:3000 ' )
)
)
)
)-> addMatch (( new Host ())
-> setHosts ([
' localhost ' ,
])
)-> setTerminal ( true );
$ caddy = new Caddy ();
$ caddy -> addApp (( new Http ())
-> addServer ( ' myplatform ' , ( new Server ())
-> addRoute ( $ route )
)
);
$ caddy -> load ();Ini akan memposting konfigurasi caddy berikut:
{
"admin" : {
"disabled" : false ,
"listen" : " :2019 "
},
"apps" : {
"http" : {
"servers" : {
"myplatform" : {
"listen" : [
" :80 "
],
"routes" : [
{
"handle" : [
{
"handler" : " subroute " ,
"routes" : [
{
"handle" : [
{
"handler" : " reverse_proxy " ,
"transport" : {
"protocol" : " fastcgi " ,
"root" : " /app/public/index.php " ,
"split_path" : [
" "
]
},
"upstreams" : [
{
"dial" : " laravel-api:9000 "
}
]
}
],
"match" : [
{
"path" : [
" /api/* "
]
}
]
},
{
"handle" : [
{
"handler" : " reverse_proxy " ,
"upstreams" : [
{
"dial" : " nextjs:3000 "
}
]
}
]
}
]
}
],
"match" : [
{
"host" : [
" localhost "
]
}
],
"terminal" : true
}
]
}
}
}
}
}curl -v localhost
< HTTP/1.1 200 OK
< Content-Type: text/html ; charset=utf-8
< Server: Caddy
< X-Powered-By: Next.js
< Transfer-Encoding: chunked
<
< ! DOCTYPE html><html > ....curl -v localhost/api/testroute
< HTTP/1.1 200 OK
< Content-Type: application/json
< Server: Caddy
< X-Powered-By: PHP/8.1.7
<
{ " status " :200}
Lihatlah tes untuk lebih banyak contoh.