Skip to content

✨ Implement an optional react/http server (Fixes #21)#26

Merged
Log1x merged 12 commits intomainfrom
feat/21
Feb 7, 2024
Merged

✨ Implement an optional react/http server (Fixes #21)#26
Log1x merged 12 commits intomainfrom
feat/21

Conversation

@Log1x
Copy link
Member

@Log1x Log1x commented Feb 3, 2024

This is an initial HTTP server implementation for react/http with full Laravel routing support. This will allow you to create webhooks and an API to interact with your bot.

Remaining Changes

  • Extract into an override-able Kernel.
  • Add sane default middleware (ConvertEmptyStringsToNull, TrimStrings, rate limiting, etc.)
  • Add API token support (bot:token generator command, default auth middleware, etc.)
  • Implement support for Controllers and make:controller for complex routes.
  • Decide on final directory structure when used in Laracord.

Testing

If you would like to help test this PR as-is, you can try it out with the following:

composer create-project laracord/laracord
cd laracord
composer require laracord/framework:dev-feat/21

app/Bot.php

<?php

namespace App;

use Illuminate\Support\Facades\Route;
use Laracord\Laracord;

class Bot extends Laracord
{
    /**
     * The HTTP routes.
     */
    public function routes(): void
    {
        Route::get('/', fn () => collect(Route::getRoutes()->getRoutes())->map(fn ($route) => $route->uri));

        Route::get('/commands', fn () => collect($this->registeredCommands)->map(fn ($command) => [
            'signature' => $command->getSignature(),
            'description' => $command->getDescription(),
        ]));

        Route::get('/servers', fn () => $this->discord()->guilds->map(fn ($server) => [
            'id' => $server->id,
            'name' => $server->name,
        ]));

        Route::get('/channels/{server}', fn ($server) => $this->discord()->guilds->get('id', $server)?->channels->map(fn ($channel) => [
            'id' => $channel->id,
            'name' => $channel->name,
        ]) ?? ['message' => 'Server not found.']);

        Route::get('/message', function () {
            $this
                ->message('This is a message from the HTTP server.')
                ->title('Hello from HTTP land!')
                ->send('your-channel-id');

            return ['message' => 'Message sent!'];
        });
    }
}

Once you boot your bot, it will be accessible at localhost:8080 by default.

Screenshot

Out of the box, it also includes the Symfony error handler when in local/development:

Exceptions Screenshot

Log1x added 12 commits February 3, 2024 04:31
➕ Add `react/http` to the project
➕ Add `symfony/psr-http-message-bridge` to the project
🔧 Add missing config values for routing
✨ Implement an overrideable Kernel
✨ Implement default middleware
🧑‍💻 Move route/middleware registration into the `Laracord` class
🔧 Add mergeable `app.php` config with necessary bootstrap configuration
🔒 Implement rate limiting
✨ Create a `bot:token` command to generate API tokens
🎨 Split the `ResolvesUser` logic into a trait
@Log1x Log1x merged commit 23928bf into main Feb 7, 2024
@Log1x Log1x deleted the feat/21 branch February 7, 2024 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant