Skip to content

Commit

Permalink
feat: add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrickn committed Sep 14, 2024
1 parent fefb6c9 commit bc51254
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 81 deletions.
13 changes: 2 additions & 11 deletions .env
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
REDIS_HOST=0.0.0.0
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_USERNAME=
REDIS_DATABASE=0

SERVER_HOST=0.0.0.0
SERVER_PORT=8080
SERVER_WORKERNUM=1

CONFIG_FILE=./config.json
TRANSPORT_FILE=/configs/transport.yml
ADAPTER_FILE=/configs/adapter.yml
64 changes: 53 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,72 @@ Octamp Wamp currently implemented using [Basic Profile](https://wamp-proto.org/w

```shell
composer create-project octamp/wamp ./wamp
cd ./wamp
```

This will create the project in wamp folder

You can update the file `/configs/adapter.yml` or copy to different file

And update the configuration
```yaml
adapter:
type: redis
host: 0.0.0.0
port: 6379
# -- Uncomment the auth if you need username and password
# auth:
# username:
# password:
#
# -- Uncomment options if you need to include other redis option such as database
options:
database: 0
```
You can update the file `/configs/transport.yml` or copy to different file

And update the configuration
```yaml
transport:
host: 0.0.0.0
port: 8080
workerNum: 1
realms:
- name: realm1
auths:
- method: anonymous
type: static
# -- You can add more method, such us the examples below
# - method: ticket
# type: dynamic
# authenticator: testing
# authenticatorRealm: realm1
# realms:
# - realm1
# - method: wampcra
# type: static
# users:
# - authid: auth
# secret: qa2/QVmmjSx1JJuyH5EI2gMDQf+ARnfwMcLOpUfln74=
# role: auth
# salt: salt1
# keylen: 32
# iterations: 1000
```

Copy the file `.env` to `.env.local`

Update the necessary data
```
REDIS_HOST=0.0.0.0
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_USERNAME=
REDIS_DATABASE=0
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
SERVER_WORKERNUM=1
TRANSPORT_FILE=/configs/transport.yml
ADAPTER_FILE=/configs/adapter.yml
```
Now run the bin/server
```shell
cd ./wamp
php ./bin/server
```

Expand Down Expand Up @@ -184,6 +227,5 @@ That will now run the server
## TODOs

- [ ] Implement CBOR Serializer https://wamp-proto.org/wamp_bp_latest_ietf.html#name-serializers
- [ ] Implement Advance Profile
- [ ] Remove Dependencies from Thruway Common
- [ ] Add OpenSwoole Table Adapter as Data Provider
80 changes: 32 additions & 48 deletions bin/server.php
Original file line number Diff line number Diff line change
@@ -1,62 +1,46 @@
<?php
error_reporting(E_ALL ^ E_DEPRECATED);

use Octamp\Wamp\Adapter\RedisAdapter;
use Octamp\Wamp\Config\AdapterConfiguration;
use Octamp\Wamp\Config\TransportConfiguration;
use Octamp\Wamp\Config\TransportProviderConfig;
use Octamp\Wamp\Wamp;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Yaml\Yaml;

$loader = require_once __DIR__ . '/../vendor/autoload.php';

$env = new Dotenv();
$env->loadEnv(dirname(__DIR__) . '/.env');
$rootPath = dirname(__DIR__);

$env = new Dotenv();
$env->loadEnv($rootPath . '/.env');

$redisOptions = [
'options' => [
'database' => $_ENV['REDIS_DATABASE'] ?? 0,
]
];
try {
$processor = new Processor();
$transportConfig = (object)$processor->processConfiguration(new TransportConfiguration(), Yaml::parseFile($rootPath . $_ENV['TRANSPORT_FILE']));
$transportProviderConfig = new TransportProviderConfig(
host: $transportConfig->host,
port: $transportConfig->port,
workerNum: $transportConfig->workerNum,
realms: $transportConfig->realms ?? [],
auth: $transportConfig->auths ?? []
);
$adapterConfig = (object)$processor->processConfiguration(new AdapterConfiguration(), Yaml::parseFile($rootPath . $_ENV['ADAPTER_FILE']));
$adapter = new RedisAdapter(
$adapterConfig->host,
$adapterConfig->port,
$adapterConfig->auth?->username ?? null,
$adapterConfig->auth?->password ?? null,
$adapterConfig->options ?? []
);
} catch (InvalidConfigurationException $exception) {
printf('[INVALID][%s]: %s', $exception->getPath(), $exception->getMessage());
exit(1);
}

$adapter = new RedisAdapter(
$_ENV['REDIS_HOST'],
$_ENV['REDIS_PORT'],
$_ENV['REDIS_USERNAME'] ?? null,
$_ENV['REDIS_PASSWORD'] ?? null,
$redisOptions
);
$transportConfig = new TransportProviderConfig(
host: $_ENV['SERVER_HOST'],
port: $_ENV['SERVER_PORT'],
workerNum: $_ENV['SERVER_WORKERNUM'],
realms: [
[
'name' => 'realm1'
],
],
auth: [
[
'method' => 'ticket',
'type' => 'dynamic',
'authenticator' => 'testing',
'authenticator-realm' => 'realm1',
'realms' => ['realm1']
],
[
'method' => 'wampcra',
'type' => 'static',
'users' => [
[
'authid' => 'auth',
'secret' => 'qa2/QVmmjSx1JJuyH5EI2gMDQf+ARnfwMcLOpUfln74=',
'role' => 'auth',
'salt' => 'salt1',
'keylen' => 32,
'iterations' => 1000
],
],
]
],
);
$wamp = new Wamp($transportConfig, $adapter);
$wamp = new Wamp($transportProviderConfig, $adapter);

$wamp->run();
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"symfony/dotenv": "^7.1",
"rybakit/msgpack": "^0.9.1",
"ext-openssl": "*",
"utopia-php/pools": "^0.5.0"
"utopia-php/pools": "^0.5.0",
"symfony/yaml": "^7.1",
"symfony/config": "^7.1"
},
"require-dev": {
"openswoole/ide-helper": "^22.1"
Expand Down
12 changes: 12 additions & 0 deletions configs/adapter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
adapter:
type: redis
host: 0.0.0.0
port: 6379
# -- Uncomment the auth if you need username and password
# auth:
# username:
# password:
#
# -- Uncomment options if you need to include other redis option such as database
options:
database: 0
25 changes: 25 additions & 0 deletions configs/transport.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
transport:
host: 0.0.0.0
port: 8080
workerNum: 1
realms:
- name: realm1
auths:
- method: anonymous
type: static
# -- You can add more method, such us the examples below
# - method: ticket
# type: dynamic
# authenticator: testing
# authenticatorRealm: realm1
# realms:
# - realm1
# - method: wampcra
# type: static
# users:
# - authid: auth
# secret: qa2/QVmmjSx1JJuyH5EI2gMDQf+ARnfwMcLOpUfln74=
# role: auth
# salt: salt1
# keylen: 32
# iterations: 1000
2 changes: 1 addition & 1 deletion src/Auth/AbstractDynamicAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function sendMessageToAuthenticator(HelloMessage $message, array $deta
}

$procedureName = $this->config['authenticator'];
$realm = $this->realmManager->getRealm($this->config['authenticator-realm']);
$realm = $this->realmManager->getRealm($this->config['authenticatorRealm']);
$realmSession = $realm->getMetaSession();
$requestId = IDHelper::incrementSessionWampID($realmSession);

Expand Down
48 changes: 48 additions & 0 deletions src/Config/AdapterConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Octamp\Wamp\Config;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class AdapterConfiguration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('adapter');
$this->configure($treeBuilder->getRootNode());

return $treeBuilder;
}

protected function configure(NodeDefinition|ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->enumNode('type')
->values(['redis'])
->defaultValue('redis')
->end()
->scalarNode('host')
->defaultValue('0.0.0.0')
->end()
->integerNode('port')
->defaultValue(6379)
->end()
->arrayNode('auth')
->children()
->scalarNode('username')->end()
->scalarNode('password')->end()
->end()
->end()
->arrayNode('options')
->ignoreExtraKeys(false)
->children()
->integerNode('database')->defaultValue(0)->end()
->end()
->end()
->end();
}
}
66 changes: 66 additions & 0 deletions src/Config/TransportConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Octamp\Wamp\Config;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class TransportConfiguration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('transport');
$this->configure($treeBuilder->getRootNode());

return $treeBuilder;
}

protected function configure(NodeDefinition|ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->scalarNode('host')->isRequired()->end()
->integerNode('port')->isRequired()->end()
->integerNode('workerNum')->defaultValue(1)->end()
->arrayNode('realms')->requiresAtLeastOneElement()
->arrayPrototype()
->children()
->scalarNode('name')->isRequired()->end()
->end()
->end()
->isRequired()
->validate()
->ifEmpty()->thenInvalid('Realms should not empty')
->end()
->end()
->arrayNode('auths')
->arrayPrototype()
->children()
->enumNode('method')->values(['anonymous', 'ticket', 'wampcra'])->isRequired()->end()
->enumNode('type')->values(['static', 'dynamic'])->isRequired()->end()
->scalarNode('authenticator')->end()
->scalarNode('authenticatorRealm')->end()
->arrayNode('realms')
->scalarPrototype()->end()
->validate()
->ifEmpty()->thenInvalid('Realms should not empty')
->end()
->end()
->arrayNode('users')
->arrayPrototype()->ignoreExtraKeys(false)->end()
->validate()
->ifEmpty()->thenInvalid('Users should not empty')
->end()
->end()
->end()
->end()
->isRequired()
->validate()
->ifEmpty()->thenInvalid('Auths should have one auth method')
->end()
->end()
->end();
}
}
4 changes: 3 additions & 1 deletion src/Roles/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected function getSubscriptionGroupByHash(string $hash, bool $global = false
if ($global) {
$raw = $this->adapter->get('subg:' . $hash);
if ($raw !== null) {
return new SubscriptionGroup($raw['match'], $raw['realm'], $raw['uri'], $raw['options'], $this->adapter, $this->sessionStorage, $this->serverId);
return new SubscriptionGroup($this->matcher->getMatch($raw['match']), $raw['realm'], $raw['uri'], $raw['options'], $this->adapter, $this->sessionStorage, $this->serverId);
}
}

Expand Down Expand Up @@ -240,7 +240,9 @@ protected function removeSubscriptionGroup(string $hash): void
unset($this->subscriptionGroups[$hash]);
if ($this->adapter->countFields('sub:' . $hash) === 0) {
$this->adapter->del('sub:' . $hash);
$this->adapter->del('subg:' . $hash);
}

}
}

Expand Down
Loading

0 comments on commit bc51254

Please sign in to comment.