Il s'agit davantage d'une preuve de concept plutôt que d'un projet entièrement fonctionnel. Cela essaie de reproduire la structure de l'API JSON Caddy pour travailler à travers des classes PHP chaînables.
Pour le moment, il n'y a qu'un petit sous-ensemble de commandes disponibles auprès de Caddy 2.0 qui couvrait mon cas d'utilisation actuellement nécessaire.
composer require mattvb91/caddy-phpUn exemple de base d'un serveur HTTP avec une réponse statique:
$ caddy = new Caddy ();
$ caddy -> addApp (
( new Http ())-> addServer (
' server1 ' , ( new Http Server ())-> addRoute (
( new Route ())-> addHandle (
new StaticResponse ( ' Hello world ' , 200 )
)
))
);
$ caddy -> load ();Cela se traduira par la configuration de caddy suivante:
{
"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 Si vous gérez dynamiquement les noms d'hôtes (dans une base de données) et que vous ne pouvez pas créer la configuration avec une liste des noms d'hôtes existants car vous devez les gérer au moment de l'exécution, vous pouvez effectuer ce qui suit:
La partie importante de cet exemple est l'identifiant host_group_name qui est ultérieur pour ajouter / supprimer des domaines à cet hôte.
$ 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 ();Maintenant, plus tard dans un script ou un événement sur votre système, vous pouvez obtenir votre objet de configuration Caddy et y publier un nouveau domaine sous cette route:
$ 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 Prenons un cas où vous souhaitez avoir un frontend de nœud et un backend PHP prenant des demandes sur l'itinéraire /api/* . Dans ce cas, l'exemple se décompose en 2 proxy inverses avec un match pour filtrer le /api/* vers le php en amont.
Cela suppose que les 3 hôtes (Caddy, Node, PHP) sont tous des conteneurs Docker et accessibles par nom de conteneur dans le même réseau Docker, vous devrez donc peut-être ajuster vos noms d'hôtes selon les besoins.
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 ();Cela affichera la configuration de caddy suivante:
{
"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}
Jetez un œil aux tests pour plus d'exemples.