An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat: api tokens + deploy webhook
Andras Bacsaia664174c025b
diff --git a/app/Actions/Database/StartMongodb.php b/app/Actions/Database/StartMongodb.phpindex 39d1b99b3c..fbdf0bba4f 100644--- a/app/Actions/Database/StartMongodb.php+++ b/app/Actions/Database/StartMongodb.php@@ -2,7 +2,6 @@namespace App\Actions\Database;-use App\Models\Server;use App\Models\StandaloneMongodb;use Illuminate\Support\Str;use Symfony\Component\Yaml\Yaml;@@ -16,7 +15,7 @@ class StartMongodbpublic array $commands = [];public string $configuration_dir;- public function handle(Server $server, StandaloneMongodb $database)+ public function handle(StandaloneMongodb $database){$this->database = $database;@@ -102,7 +101,7 @@ class StartMongodb$this->commands[] = "echo '{$readme}' > $this->configuration_dir/README.md";$this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d";$this->commands[] = "echo '####### {$database->name} started.'";- return remote_process($this->commands, $server);+ return remote_process($this->commands, $database->destination->server);}private function generate_local_persistent_volumes()@@ -160,6 +159,5 @@ class StartMongodb$content = $this->database->mongo_conf;$content_base64 = base64_encode($content);$this->commands[] = "echo '{$content_base64}' | base64 -d > $this->configuration_dir/{$filename}";-}}diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.phpindex 615f3ed4a5..46e4109801 100644--- a/app/Actions/Database/StartPostgresql.php+++ b/app/Actions/Database/StartPostgresql.php@@ -2,7 +2,6 @@namespace App\Actions\Database;-use App\Models\Server;use App\Models\StandalonePostgresql;use Illuminate\Support\Str;use Symfony\Component\Yaml\Yaml;@@ -17,7 +16,7 @@ class StartPostgresqlpublic array $init_scripts = [];public string $configuration_dir;- public function handle(Server $server, StandalonePostgresql $database)+ public function handle(StandalonePostgresql $database){$this->database = $database;$container_name = $this->database->uuid;@@ -104,7 +103,7 @@ class StartPostgresql$this->commands[] = "echo '{$readme}' > $this->configuration_dir/README.md";$this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d";$this->commands[] = "echo '####### {$database->name} started.'";- return remote_process($this->commands, $server);+ return remote_process($this->commands, $database->destination->server);}private function generate_local_persistent_volumes()diff --git a/app/Actions/Database/StartRedis.php b/app/Actions/Database/StartRedis.phpindex 6ea950f5d7..af6b2ad4ff 100644--- a/app/Actions/Database/StartRedis.php+++ b/app/Actions/Database/StartRedis.php@@ -2,7 +2,6 @@namespace App\Actions\Database;-use App\Models\Server;use App\Models\StandaloneRedis;use Illuminate\Support\Str;use Symfony\Component\Yaml\Yaml;@@ -17,7 +16,7 @@ class StartRedispublic string $configuration_dir;- public function handle(Server $server, StandaloneRedis $database)+ public function handle(StandaloneRedis $database){$this->database = $database;@@ -104,7 +103,7 @@ class StartRedis$this->commands[] = "echo '{$readme}' > $this->configuration_dir/README.md";$this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d";$this->commands[] = "echo '####### {$database->name} started.'";- return remote_process($this->commands, $server);+ return remote_process($this->commands, $database->destination->server);}private function generate_local_persistent_volumes()diff --git a/app/Http/Livewire/Dashboard.php b/app/Http/Livewire/Dashboard.phpindex b46df481ab..723b00f7f3 100644--- a/app/Http/Livewire/Dashboard.php+++ b/app/Http/Livewire/Dashboard.php@@ -17,10 +17,6 @@ class Dashboard extends Component$this->servers = Server::ownedByCurrentTeam()->get();$this->projects = Project::ownedByCurrentTeam()->get();}- // public function createToken() {- // $token = auth()->user()->createToken('test');- // ray($token);- // }// public function getIptables()// {// $servers = Server::ownedByCurrentTeam()->get();diff --git a/app/Http/Livewire/Project/Database/Heading.php b/app/Http/Livewire/Project/Database/Heading.phpindex 1a199ecb73..6045e2b7f6 100644--- a/app/Http/Livewire/Project/Database/Heading.php+++ b/app/Http/Livewire/Project/Database/Heading.php@@ -47,15 +47,15 @@ class Heading extends Componentpublic function start(){if ($this->database->type() === 'standalone-postgresql') {- $activity = StartPostgresql::run($this->database->destination->server, $this->database);+ $activity = StartPostgresql::run($this->database);$this->emit('newMonitorActivity', $activity->id);}if ($this->database->type() === 'standalone-redis') {- $activity = StartRedis::run($this->database->destination->server, $this->database);+ $activity = StartRedis::run($this->database);$this->emit('newMonitorActivity', $activity->id);}if ($this->database->type() === 'standalone-mongodb') {- $activity = StartMongodb::run($this->database->destination->server, $this->database);+ $activity = StartMongodb::run($this->database);$this->emit('newMonitorActivity', $activity->id);}}diff --git a/app/Http/Livewire/Security/ApiTokens.php b/app/Http/Livewire/Security/ApiTokens.phpnew file mode 100644index 0000000000..f00b309309--- /dev/null+++ b/app/Http/Livewire/Security/ApiTokens.php@@ -0,0 +1,38 @@+<?php++namespace App\Http\Livewire\Security;++use Livewire\Component;++class ApiTokens extends Component+{+ public ?string $description = null;+ public $tokens = [];+ public function render()+ {+ return view('livewire.security.api-tokens');+ }+ public function mount()+ {+ $this->tokens = auth()->user()->tokens;+ }+ public function addNewToken()+ {+ try {+ $this->validate([+ 'description' => 'required|min:3|max:255',+ ]);+ $token = auth()->user()->createToken($this->description);+ $this->tokens = auth()->user()->tokens;+ session()->flash('token', $token->plainTextToken);+ } catch (\Exception $e) {+ return handleError($e, $this);+ }+ }+ public function revoke(int $id)+ {+ $token = auth()->user()->tokens()->where('id', $id)->first();+ $token->delete();+ $this->tokens = auth()->user()->tokens;+ }+}diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.phpindex f78ed363f1..f60994c616 100644--- a/app/Providers/RouteServiceProvider.php+++ b/app/Providers/RouteServiceProvider.php@@ -46,9 +46,9 @@ class RouteServiceProvider extends ServiceProvider{RateLimiter::for('api', function (Request $request) {if ($request->path() === 'api/health') {- return Limit::perMinute(5000)->by($request->user()?->id ?: $request->ip());+ return Limit::perMinute(1000)->by($request->user()?->id ?: $request->ip());}- return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());+ return Limit::perMinute(30)->by($request->user()?->id ?: $request->ip());});RateLimiter::for('5', function (Request $request) {return Limit::perMinute(5)->by($request->user()?->id ?: $request->ip());diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.phpindex dd761b4494..123670dc52 100644--- a/bootstrap/helpers/shared.php+++ b/bootstrap/helpers/shared.php@@ -1,7 +1,12 @@<?php+use App\Models\Application;use App\Models\InstanceSettings;use App\Models\Server;+use App\Models\Service;+use App\Models\StandaloneMongodb;+use App\Models\StandalonePostgresql;+use App\Models\StandaloneRedis;use App\Models\Team;use App\Models\User;use App\Notifications\Channels\DiscordChannel;@@ -453,3 +458,31 @@ function getServiceTemplates()}return $services;}++function getResourceByUuid(string $uuid, ?int $teamId = null)+{+ $resource = queryResourcesByUuid($uuid);+ if (!is_null($teamId)) {+ if ($resource->environment->project->team_id === $teamId) {+ return $resource;+ }+ return null;+ } else {+ return $resource;+ }+}+function queryResourcesByUuid(string $uuid)+{+ $resource = null;+ $application = Application::whereUuid($uuid)->first();+ if ($application) return $application;+ $service = Service::whereUuid($uuid)->first();+ if ($service) return $service;+ $postgresql = StandalonePostgresql::whereUuid($uuid)->first();+ if ($postgresql) return $postgresql;+ $redis = StandaloneRedis::whereUuid($uuid)->first();+ if ($redis) return $redis;+ $mongodb = StandaloneMongodb::whereUuid($uuid)->first();+ if ($mongodb) return $mongodb;+ return $resource;+}diff --git a/resources/views/components/security/navbar.blade.php b/resources/views/components/security/navbar.blade.phpindex 1d2296cc1e..084ed4d821 100644--- a/resources/views/components/security/navbar.blade.php+++ b/resources/views/components/security/navbar.blade.php@@ -10,8 +10,15 @@</ol></nav><nav class="navbar-main">- <a class="{{ request()->routeIs('security.private-key.index') ? 'text-white' : '' }}" href="{{ route('security.private-key.index') }}">+ <a href="{{ route('security.private-key.index') }}"><button>Private Keys</button></a>+ <a href="{{ route('security.api-tokens') }}">+ <button>API tokens</button>+ </a>+ <div class="flex-1"></div>+ <div class="-mt-9">+ <livewire:switch-team />+ </div></nav></div>diff --git a/resources/views/livewire/dashboard.blade.php b/resources/views/livewire/dashboard.blade.phpindex 2e79582e13..cd4f8a4580 100644--- a/resources/views/livewire/dashboard.blade.php+++ b/resources/views/livewire/dashboard.blade.php@@ -100,9 +100,6 @@</a>@endforeach</div>-{{-- <h3 class="py-4">Tokens</h3>- {{auth()->user()->tokens}}- <x-forms.button wire:click='createToken'>Create Token</x-forms.button> --}}<script>function gotoProject(uuid, environment = 'production') {window.location.href = '/project/' + uuid + '/' + environment;diff --git a/resources/views/livewire/security/api-tokens.blade.php b/resources/views/livewire/security/api-tokens.blade.phpnew file mode 100644index 0000000000..eb1027c327--- /dev/null+++ b/resources/views/livewire/security/api-tokens.blade.php@@ -0,0 +1,36 @@+<div>+ <x-security.navbar />+ <div class="flex gap-2">+ <h2 class="pb-4">API Tokens</h2>+ <x-helper+ helper="Tokens are created with the current team as scope. You will only have access to this team's resources." />+ </div>+ <h4>Create New Token</h4>+ <span>Currently active team: <span class="text-warning">{{ session('currentTeam.name') }}</span></span>+ <form class="flex items-end gap-2 pt-4" wire:submit.prevent='addNewToken'>+ <x-forms.input required id="description" label="Description" />+ <x-forms.button type="submit">Create New Token</x-forms.button>+ </form>+ @if (session()->has('token'))+ <div class="pb-4 font-bold text-warning">Please copy this token now. For your security, it won't be shown again.+ </div>+ <div class="pb-4 font-bold text-white"> {{ session('token') }}</div>+ @endif+ <h4 class="py-4">Issued Tokens</h4>+ <div class="grid gap-2 lg:grid-cols-1">+ @forelse ($tokens as $token)+ <div class="flex items-center gap-2">+ <div+ class="flex items-center gap-2 group-hover:text-white p-2 border border-coolgray-200 hover:text-white hover:no-underline min-w-[24rem] cursor-default">+ <div>{{ $token->name }}</div>+ </div>+ <x-forms.button wire:click="revoke('{{ $token->id }}')">Revoke</x-forms.button>+ </div>+ @empty+ <div>+ <div>No API tokens found.</div>+ </div>+ @endforelse+ </div>++</div>diff --git a/routes/api.php b/routes/api.phpindex 90507a496a..322582ace4 100644--- a/routes/api.php+++ b/routes/api.php@@ -1,7 +1,15 @@<?php+use App\Actions\Database\StartPostgresql;+use App\Models\Application;+use App\Models\Service;+use App\Models\StandaloneMongodb;+use App\Models\StandalonePostgresql;+use App\Models\StandaloneRedis;use App\Models\User;+use Illuminate\Http\Request;use Illuminate\Support\Facades\Route;+use Visus\Cuid2\Cuid2;/*|--------------------------------------------------------------------------@@ -17,9 +25,46 @@ use Illuminate\Support\Facades\Route;Route::get('/health', function () {return 'OK';});+Route::group([+ 'middleware' => ['auth:sanctum'],+ 'prefix' => 'v1'+], function () {+ Route::get('/deploy', function (Request $request) {+ $token = auth()->user()->currentAccessToken();+ $teamId = data_get($token, 'team_id');+ $uuid = $request->query->get('uuid');+ $force = $request->query->get('force') ?? false;++ if (is_null($teamId)) {+ return response()->json(['error' => 'Invalid token.'], 400);+ }+ if (!$uuid) {+ return response()->json(['error' => 'No UUID provided.'], 400);+ }+ $resource = getResourceByUuid($uuid, $teamId);+ if ($resource) {+ $type = $resource->getMorphClass();+ if ($type === 'App\Models\Application') {+ queue_application_deployment(+ application_id: $resource->id,+ deployment_uuid: new Cuid2(7),+ force_rebuild: $force,+ );+ return response()->json(['message' => 'Deployment queued.'], 200);+ } else if ($type === 'App\Models\StandalonePostgresql') {+ StartPostgresql::run($resource);+ $resource->update([+ 'started_at' => now(),+ ]);+ return response()->json(['message' => 'Database started.'], 200);+ }+ }+ return response()->json(['error' => 'No resource found.'], 404);+ });+});Route::middleware(['throttle:5'])->group(function () {- Route::get('/unsubscribe/{token}', function() {+ Route::get('/unsubscribe/{token}', function () {try {$token = request()->token;$email = decrypt($token);@@ -34,6 +79,5 @@ Route::middleware(['throttle:5'])->group(function () {} catch (\Throwable $e) {return 'Something went wrong. Please try again or contact support.';}-})->name('unsubscribe.marketing.emails');});diff --git a/routes/web.php b/routes/web.phpindex 928717a0ab..83e119aeea 100644--- a/routes/web.php+++ b/routes/web.php@@ -12,6 +12,7 @@ use App\Http\Livewire\Dev\Compose as Compose;use App\Http\Livewire\Dashboard;use App\Http\Livewire\Project\CloneProject;use App\Http\Livewire\Project\Shared\Logs;+use App\Http\Livewire\Security\ApiTokens;use App\Http\Livewire\Server\All;use App\Http\Livewire\Server\Create;use App\Http\Livewire\Server\Destination\Show as DestinationShow;@@ -164,6 +165,8 @@ Route::middleware(['auth'])->group(function () {Route::get('/security/private-key/{private_key_uuid}', fn () => view('security.private-key.show', ['private_key' => PrivateKey::ownedByCurrentTeam(['name', 'description', 'private_key', 'is_git_related'])->whereUuid(request()->private_key_uuid)->firstOrFail()]))->name('security.private-key.show');+ Route::get('/security/api-tokens', ApiTokens::class)->name('security.api-tokens');+});