Skip to content

Commit

Permalink
Feat: use Freddie as a Symfony Bundle (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Jan 19, 2022
1 parent 444de73 commit bea4e31
Show file tree
Hide file tree
Showing 26 changed files with 807 additions and 395 deletions.
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
* text=auto

/.github export-ignore
/bin export-ignore
/config export-ignore
/tests export-ignore
/var export-ignore
/.env export-ignore
/.env.test export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/composer.lock export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/symfony.lock export-ignore
52 changes: 47 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,43 @@ See what features are covered and what aren't (yet) [here](#feature-coverage).

PHP 8.1 is required to run the hub.

### As a standalone Mercure hub

```bash
composer create-project freddie/mercure-x freddie && cd freddie
bin/freddie
```

This will start a Freddie instance on `127.0.0.1:8080`, with anonymous subscriptions enabled.

You can publish updates to the hub by generating a valid JWT signed with the `!ChangeMe!` key with `HMAC SHA256` algorithm.

To change these values, see [Security](#security).

### As a bundle of your existing Symfony application

```bash
composer req freddie/mercure-x
```

You can then start the hub by doing:

```bash
composer create-project freddie/mercure-x:"~0.2" freddie
cd freddie
bin/console freddie:serve
```

You can override relevant env vars in your `.env.local`
and services in your `config/services.yaml` as usual.

Then, you can inject `Freddie\Hub\HubInterface` in your services so that you can call `$hub->publish($update)`,
or listening to dispatched updates in a CLI context 👍

Keep in mind this only works when using the Redis transport.

⚠️ **Freddie** uses its own routing/authentication system (because of async / event loop).

The controllers it exposes cannot be imported in your `routes.yaml`, and get out of your `security.yaml` scope.

## Usage

```bash
Expand All @@ -35,17 +67,27 @@ To change this address, use the `X_LISTEN` environment variable:
X_LISTEN="0.0.0.0:8000" ./bin/freddie
```

### Security

The default JWT key is `!ChangeMe!` with a `HS256` signature.

You can set different values by changing the environment variables (in `.env.local` or at the OS level):
`X_LISTEN`, `JWT_SECRET_KEY`, `JWT_ALGORITHM`, `JWT_PUBLIC_KEY` and `JWT_PASSPHRASE` (when using RS512 or ECDSA)

Please refer to the [authorization](https://mercure.rocks/spec#authorization) section of the Mercure specification to authenticate as a publisher and/or a subscriber.

### Redis transport
### PHP Transport (default)

By default, the hub will run as a simple event-dispatcher, in a single PHP process.

It can fit common needs for a basic usage, but using this transport prevents scalability,
as opening another process won't share the same event emitter.

By default, the hub will run as a simple event-dispatcher, in a single PHP process.
It can fit common needs for a basic usage, but is not scalable (opening another process won't share the same event emitter).
It's still prefectly usable as soon as :
- You don't expect more than a few hundreds updates per second
- Your application is served from a single server.

### Redis transport

On the other hand, you can launch the hub on **multiple ports** and/or **multiple servers** with a Redis transport
(as soon as they share the same Redis instance), and optionally use a load-balancer to distribute the traffic.
Expand Down
2 changes: 1 addition & 1 deletion bin/freddie
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return static function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
$application = new Application($kernel);
$application->setDefaultCommand('serve');
$application->setDefaultCommand('freddie:serve');

return $application;
};
11 changes: 3 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
"ext-ctype": "*",
"ext-iconv": "*",
"bentools/querystring": "^1.1",
"clue/framework-x": "dev-main",
"clue/framework-x": "dev-main#74fafca31a1d3d4c6faa3bf9276a9d018cb587fa",
"clue/redis-react": "^2.5",
"doctrine/annotations": "^1.0",
"lcobucci/jwt": "^4.1",
"nyholm/dsn": "^2.0",
"phpdocumentor/reflection-docblock": "^5.3",
"react/async": "dev-main",
"react/async": "dev-main#ff11a7aa9eea7104af8f05bafbc85422dac4b8ab",
"rize/uri-template": "^0.3.4",
"symfony/console": "6.0.*",
"symfony/dotenv": "6.0.*",
"symfony/flex": "^1.17",
"symfony/flex": "^1.17|^2",
"symfony/framework-bundle": "6.0.*",
"symfony/options-resolver": "6.0.*",
"symfony/property-access": "6.0.*",
Expand Down Expand Up @@ -64,11 +64,6 @@
}
},
"bin": ["bin/freddie"],
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*"
},
"scripts": {
"post-install-cmd": [
"bin/freddie cache:clear"
Expand Down
Loading

0 comments on commit bea4e31

Please sign in to comment.