forked from rodrigopedra/laravel-cloud
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathCaddyBalancerConfiguration.php
100 lines (88 loc) · 2.07 KB
/
CaddyBalancerConfiguration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace App;
use Illuminate\Support\Str;
class CaddyBalancerConfiguration
{
/**
* The balancer instance.
*
* @var \App\Balancer
*/
public $balancer;
/**
* The stack instance.
*
* @var \App\Stack
*/
public $stack;
/**
* The domain name.
*
* @var string
*/
public $domain;
/**
* The addresses the balancer should proxy to.
*
* @var array
*/
public $proxyTo;
/**
* Create a new Caddy configuration instance.
*
* @param \App\Balancer $balancer
* @param \App\Stack $balancer
* @param string $domain
* @param array $proxyTo
* @return void
*/
public function __construct(Balancer $balancer, Stack $stack, $domain, array $proxyTo)
{
$this->stack = $stack;
$this->domain = $domain;
$this->proxyTo = $proxyTo;
$this->balancer = $balancer;
}
/**
* Render the Caddy configuration block.
*
* @return string
*/
public function render()
{
return view($this->script(), [
'canonicalDomain' => $this->stack->canonicalDomain($this->domain),
'domain' => $this->domain,
'tls' => $this->tls(),
'proxyTo' => $this->proxyTo,
])->render();
}
/**
* Get the script that should be used to build the configuration.
*
* @return string
*/
protected function script()
{
return ! $this->stack->isCanonicalDomain($this->domain) || Str::contains($this->domain, ':80')
? 'scripts.caddy-configuration.redirect'
: 'scripts.caddy-configuration.proxy';
}
/**
* Get the TLS configuration block for the script.
*
* @return string
*/
protected function tls()
{
if (Str::contains($this->domain, ':80')) {
return 'tls off';
}
if ($this->balancer->selfSignsCertificates()) {
return 'tls self_signed';
}
return 'tls {
max_certs 1
}';
}
}