Skip to content

Commit

Permalink
feat: add api support, and scribe documentation (monicahq/chandler#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Nov 12, 2022
1 parent 88e5502 commit 2dc2a83
Show file tree
Hide file tree
Showing 17 changed files with 1,766 additions and 384 deletions.
24 changes: 14 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
/bootstrap/ssr
/data.ms
/lang/php_*.json
/node_modules
/public/build
/public/docs
/public/hot
/public/storage
/public/vendor/scribe
/resources/views/scribe
/storage/*.key
/vendor
/results
/.scribe
/tests/Features/screenshots/
/tests/Features/videos/
_ide_helper.php
_ide_helper_models.php
.env
.phpunit.result.cache
Homestead.json
.DS_Store
.idea
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.php_cs.cache
.DS_Store
/tests/Features/screenshots/
/tests/Features/videos/
/data.ms
_ide_helper.php
_ide_helper_models.php
.phpstorm.meta.php
lang/php_*.json
.phpunit.result.cache
yarn-error.log
28 changes: 11 additions & 17 deletions app/Console/Commands/SetupApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Support\Facades\Schema;
use MeiliSearch\Client;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -43,6 +42,7 @@ public function handle(): void
$this->migrate();
$this->cacheConfig();
$this->scout();
$this->documentation();
}
}

Expand Down Expand Up @@ -121,23 +121,17 @@ protected function migrate(): void
*/
protected function scout(): void
{
if (config('scout.driver') === 'meilisearch' && ($host = config('scout.meilisearch.host')) !== '') {
$this->info('-> Creating indexes on Meilisearch. Make sure Meilisearch is running.');

$config = [
'contacts' => ['id', 'vault_id'],
'notes' => ['id', 'vault_id', 'contact_id'],
'groups' => ['id', 'vault_id'],
];

$client = new Client($host, config('scout.meilisearch.key'));
foreach ($config as $name => $fields) {
$index = $client->index($name);
$index->updateFilterableAttributes($fields);
}
$this->artisan('✓ Setup scout', 'scout:setup', ['--force' => true]);
}

$this->info('✓ Indexes created');
}
/**
* Regenerate api documentation.
*
* @return void
*/
protected function documentation(): void
{
$this->artisan('✓ Generate api documentation', 'scribe:setup', ['--force' => true]);
}

private function artisan(string $message, string $command, array $options = [])
Expand Down
65 changes: 65 additions & 0 deletions app/Console/Commands/SetupDocumentation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @codeCoverageIgnore
*/
class SetupDocumentation extends Command
{
use ConfirmableTrait;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scribe:setup
{--force : Force the operation to run when in production.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install or update the application, and run migrations after a new release';

/**
* Execute the console command.
*/
public function handle(): void
{
if ($this->confirmToProceed()) {
$this->documentation();
}
}

/**
* Regenerate api documentation.
*
* @return void
*/
protected function documentation(): void
{
putenv('DB_CONNECTION=docs');
putenv('APP_ENV=testing');
putenv('CACHE_DRIVER=array');
putenv('QUEUE_CONNECTION=sync');
putenv('SESSION_DRIVER=array');
putenv('MAIL_MAILER=log');
putenv('SCOUT_DRIVER=null');

exec('php artisan scribe:generate --verbose --force', $output);

if ($this->getOutput()->getOutput()->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
foreach ($output as $line) {
$this->line($line);
}
$this->line('');
}
}
}
16 changes: 7 additions & 9 deletions app/Console/Commands/SetupDummyAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use App\Exceptions\EntryAlreadyExistException;
use App\Models\Contact;
use App\Models\ContactImportantDate;
use App\Models\Group;
use App\Models\Note;
use App\Models\PostTemplate;
use App\Models\User;
use App\Models\Vault;
Expand Down Expand Up @@ -44,6 +42,7 @@ class SetupDummyAccount extends Command
* @var string
*/
protected $signature = 'monica:dummy
{--migrate : Use migrate command instead of migrate:fresh.}
{--force : Force the operation to run.}';

/**
Expand Down Expand Up @@ -84,13 +83,12 @@ private function start(): void

private function wipeAndMigrateDB(): void
{
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Note::class]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Contact::class]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Group::class]);

$this->artisan('☐ Reset search engine', 'monica:setup');
$this->artisan('☐ Migration of the database', 'migrate:fresh');
$this->artisan('☐ Symlink the storage folder', 'storage:link');
if ($this->hasOption('migrate') && $this->option('migrate')) {
$this->artisan('☐ Migration of the database', 'migrate', ['--force' => true]);
} else {
$this->artisan('☐ Migration of the database', 'migrate:fresh', ['--force' => true]);
}
$this->artisan('☐ Reset search engine', 'scout:setup', ['--force' => true]);
}

private function stop(): void
Expand Down
84 changes: 84 additions & 0 deletions app/Console/Commands/SetupScout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Console\Commands;

use App\Models\Contact;
use App\Models\Group;
use App\Models\Note;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use MeiliSearch\Client;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @codeCoverageIgnore
*/
class SetupScout extends Command
{
use ConfirmableTrait;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:setup
{--force : Force the operation to run when in production.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup scout indexes.';

/**
* Execute the console command.
*/
public function handle(): void
{
if ($this->confirmToProceed()) {
$this->scout();
}
}

/**
* Configure scout.
*
* @return void
*/
protected function scout(): void
{
if (config('scout.driver') !== null) {
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Note::class]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Contact::class]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Group::class]);
}

if (config('scout.driver') === 'meilisearch' && ($host = config('scout.meilisearch.host')) !== '') {
$this->info('-> Creating indexes on Meilisearch. Make sure Meilisearch is running.');

$config = [
'contacts' => ['id', 'vault_id'],
'notes' => ['id', 'vault_id', 'contact_id'],
'groups' => ['id', 'vault_id'],
];

$client = new Client($host, config('scout.meilisearch.key'));
foreach ($config as $name => $fields) {
$index = $client->index($name);
$index->updateFilterableAttributes($fields);
}

$this->info('✓ Indexes created');
}
}

private function artisan(string $message, string $command, array $options = [])
{
$this->info($message);
$this->getOutput()->getOutput()->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE
? $this->call($command, $options)
: $this->callSilent($command, $options);
}
}
21 changes: 21 additions & 0 deletions app/Domains/Settings/ManageUsers/Api/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Domains\Settings\ManageUsers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
/**
* GET api/user
*
* Get the authenticated User.
*
* @apiResourceModel \App\Models\User
*/
public function __invoke(Request $request)
{
return $request->user();
}
}
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Kernel extends HttpKernel
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
Expand Down
17 changes: 17 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

use App\Http\Controllers\Profile\WebauthnDestroyResponse;
use App\Http\Controllers\Profile\WebauthnUpdateResponse;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Knuckles\Scribe\Scribe;
use Laravel\Sanctum\Sanctum;
use LaravelWebauthn\Facades\Webauthn;

class AppServiceProvider extends ServiceProvider
Expand All @@ -34,5 +39,17 @@ public function boot()

Webauthn::updateViewResponseUsing(WebauthnUpdateResponse::class);
Webauthn::destroyViewResponseUsing(WebauthnDestroyResponse::class);

Scribe::beforeResponseCall(function () {
// @codeCoverageIgnoreStart
Carbon::setTestNow(Carbon::create(2020, 1, 1, 0, 0, 0, 'UTC'));
Artisan::call('monica:dummy', [
'--migrate' => true,
'--force' => true,
]);
$user = User::first();
Sanctum::actingAs($user, ['*']);
// @codeCoverageIgnoreEnd
});
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"guzzlehttp/guzzle": "^7.4.5",
"http-interop/http-factory-guzzle": "^1.2",
"inertiajs/inertia-laravel": "^0.6.3",
"knuckleswtf/scribe": "^4.2",
"laravel-notification-channels/telegram": "^2.1",
"laravel/fortify": "^1.13",
"laravel/framework": "^9.22.1",
Expand Down
Loading

0 comments on commit 2dc2a83

Please sign in to comment.