Updated dependencies, improved config
* Configuration now key-value map with friendly accessors. * Configuration file maps 1:1
This commit is contained in:
		@@ -6,13 +6,8 @@ use Symfony\Component\Yaml\Yaml;
 | 
			
		||||
 | 
			
		||||
class Configuration
 | 
			
		||||
{
 | 
			
		||||
    private ?string $publicUrl = null;
 | 
			
		||||
    private array $config = [];
 | 
			
		||||
 | 
			
		||||
    private ?string $jwtSecret = null;
 | 
			
		||||
 | 
			
		||||
    private bool $allowAnonymousSubscribe = false;
 | 
			
		||||
 | 
			
		||||
    private array $listeners = [];
 | 
			
		||||
 | 
			
		||||
    public static function createDefault(): Configuration
 | 
			
		||||
    {
 | 
			
		||||
@@ -30,81 +25,77 @@ class Configuration
 | 
			
		||||
        $data = file_get_contents($file);
 | 
			
		||||
        $yaml = Yaml::parse($data);
 | 
			
		||||
 | 
			
		||||
        if (isset($yaml['security'])) {
 | 
			
		||||
            $security = $yaml['security'];
 | 
			
		||||
            if (isset($security['jwt_secret']))
 | 
			
		||||
                $config->setJwtSecret($security['jwt_secret']);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (isset($yaml['subscribe'])) {
 | 
			
		||||
            $subscribe = $yaml['subscribe'];
 | 
			
		||||
            if (isset($subscribe['allow_anonymous']))
 | 
			
		||||
                $config->setAllowAnonymousSubscribe(boolval($subscribe['allow_anonymous']));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (isset($yaml['listeners'])) {
 | 
			
		||||
            foreach ($yaml['listeners'] as $listener) {
 | 
			
		||||
                if (!is_array($listener)) {
 | 
			
		||||
                    throw new \Exception("Bad listener config");
 | 
			
		||||
        $unwrap = null; // mute IDE complains about $unwrap not defined
 | 
			
		||||
        $unwrap = function (array $array, Configuration $target, array $path=[]) use (&$unwrap) {
 | 
			
		||||
            foreach ($array as $key=>$value) {
 | 
			
		||||
                if (is_array($value)) {
 | 
			
		||||
                    $unwrap($value, $target, [ ...$path, $key ]);
 | 
			
		||||
                } else {
 | 
			
		||||
                    $key = join(".", [ ...$path, $key ]);
 | 
			
		||||
                    $target->config[$key] = $value;
 | 
			
		||||
                }
 | 
			
		||||
                $config->addListener($listener);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        };
 | 
			
		||||
        $unwrap($yaml, $config);
 | 
			
		||||
 | 
			
		||||
        return $config;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setPublicUrl(string $publicUrl): self
 | 
			
		||||
    {
 | 
			
		||||
        $this->publicUrl = $publicUrl;
 | 
			
		||||
        $this->config['server.public_url'] = $publicUrl;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getPublicUrl(): ?string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->publicUrl;
 | 
			
		||||
        return $this->config['server.public_url']??null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setJwtSecret(string $secret): self
 | 
			
		||||
    {
 | 
			
		||||
        $this->jwtSecret = $secret;
 | 
			
		||||
        $this->config['security.jwt_secret'] = $secret;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getJwtSecret(): ?string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->jwtSecret;
 | 
			
		||||
        return $this->config['security.jwt_secret']??null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	function getAllowAnonymousSubscribe():bool
 | 
			
		||||
    {
 | 
			
		||||
		return $this->allowAnonymousSubscribe;
 | 
			
		||||
		return $this->config['subscribe.allow_anonymous']??false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	function setAllowAnonymousSubscribe(bool $allowAnonymousSubscribe): self
 | 
			
		||||
    {
 | 
			
		||||
		$this->allowAnonymousSubscribe = $allowAnonymousSubscribe;
 | 
			
		||||
		$this->config['subscribe.allow_anonymous'] = $allowAnonymousSubscribe;
 | 
			
		||||
		return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function addListener(array $config): self
 | 
			
		||||
    public function setListenAddress(string $address): self
 | 
			
		||||
    {
 | 
			
		||||
        $this->listeners[] = [
 | 
			
		||||
            'address' => $config['address']??throw new \Exception("Address can't be empty"),
 | 
			
		||||
            'cors' => isset($config['cors'])?[
 | 
			
		||||
                'allow_origin' => $config['cors']['allow_origin']??'*',
 | 
			
		||||
                'csp' => $config['cors']['csp']??'default-src * \'self\'',
 | 
			
		||||
            ]:[
 | 
			
		||||
                'allow_origin' => '*',
 | 
			
		||||
                'csp' => 'default-src * \'self\'',
 | 
			
		||||
            ],
 | 
			
		||||
        ];
 | 
			
		||||
        $this->config['server.address'] = $address;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getListeners(): array
 | 
			
		||||
    public function getListenAddress(): ?string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->listeners;
 | 
			
		||||
        return $this->config['server.address']??null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setAllowOriginHeader(string $value): self
 | 
			
		||||
    {
 | 
			
		||||
        $this->config['headers.allow_origin'] = $value;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setContentSecurityPolicyHeader(string $value): self
 | 
			
		||||
    {
 | 
			
		||||
        $this->config['headers.csp'] = $value;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -25,11 +25,13 @@ class Daemon
 | 
			
		||||
    {
 | 
			
		||||
        $this->server = new Server($this->config, $this->loop);
 | 
			
		||||
 | 
			
		||||
        $listeners = $this->config->getListeners();
 | 
			
		||||
        foreach ($listeners as $listener) {
 | 
			
		||||
            $socket = new SocketServer("tcp://".$listener['address']);
 | 
			
		||||
            $this->server->listen($socket);
 | 
			
		||||
        $listenAddress = $this->config->getListenAddress();
 | 
			
		||||
        if (!$listenAddress) {
 | 
			
		||||
            fwrite(STDERR, "Warning: Empty listening address. You won't make it far.\n");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        $socket = new SocketServer("tcp://".$listenAddress);
 | 
			
		||||
        $this->server->listen($socket);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function stop(): void
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user