Skip to content

Commit

Permalink
feat: #1 bootstrap the laravel app
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jun 19, 2024
1 parent 54bd16e commit 0ebe7db
Show file tree
Hide file tree
Showing 36 changed files with 1,193 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add 'triage' label to any issue that gets opened
triage:
- '/.*/'
18 changes: 18 additions & 0 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Issue Labeler"
on:
issues:
types: [opened]

permissions:
issues: write
contents: read

jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/[email protected]
with:
configuration-path: .github/labeler.yml
enable-versioned-regex: 0
repo-token: ${{ github.token }}
23 changes: 23 additions & 0 deletions app/HasOwningTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App;

use App\Models\Scopes\TeamScope;
use App\Models\Team;

trait HasOwningTeam
{
protected static function bootHasOwningTeam(): void
{
static::addGlobalScope(new TeamScope());

static::creating(function ($model) {
$model->team_id = auth()->user()->currentTeam->id;
});
}

public function team()
{
return $this->belongsTo(Team::class, 'team_id');
}
}
69 changes: 69 additions & 0 deletions app/Http/Controllers/NodeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreNodeRequest;
use App\Http\Requests\UpdateNodeRequest;
use App\Models\Node;
use Inertia\Inertia;

class NodeController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return Inertia::render('Nodes/Index');
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
return Inertia::render('Nodes/Create');
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreNodeRequest $request)
{
$node = Node::create($request->all());

return to_route('nodes.show', ['node' => $node->id]);
}

/**
* Display the specified resource.
*/
public function show(Node $node)
{
return Inertia::render('Nodes/Show', ['node' => $node]);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Node $node)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateNodeRequest $request, Node $node)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Node $node)
{
//
}
}
66 changes: 66 additions & 0 deletions app/Http/Controllers/SwarmController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreSwarmRequest;
use App\Http\Requests\UpdateSwarmRequest;
use App\Models\Swarm;

class SwarmController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreSwarmRequest $request)
{
//
}

/**
* Display the specified resource.
*/
public function show(Swarm $swarm)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Swarm $swarm)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateSwarmRequest $request, Swarm $swarm)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Swarm $swarm)
{
//
}
}
37 changes: 37 additions & 0 deletions app/Http/Middleware/NodeAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Middleware;

use App\Models\Node;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class NodeAuth
{
const AUTH_HEADER = 'x-node-token';

/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$token = $request->header(self::AUTH_HEADER);

if (!$token) {
return response()->json([
'message' => 'Unauthorized'
], 401);
}

$node = Node::whereAgentToken($token)->firstOrFail();

$node->last_seen_at = now();

$node->save();

return $next($request);
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/StoreNodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreNodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/StoreSwarmRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreSwarmRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/UpdateNodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateNodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/UpdateSwarmRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateSwarmRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}
35 changes: 35 additions & 0 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Models;

use App\HasOwningTeam;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class Node extends Model
{
use HasFactory;
use HasOwningTeam;

protected $fillable = [
'name',
];

protected $appends = [
'online',
];

protected static function booted()
{
self::creating(function (Node $node) {
$node->agent_token = Str::random(42);
});
}

public function getOnlineAttribute()
{
return $this->last_seen_at > now()->subSeconds(35);
}
}
18 changes: 18 additions & 0 deletions app/Models/Scopes/TeamScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class TeamScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
$builder->where('team_id', auth()->user()->currentTeam->id);
}
}
Loading

0 comments on commit 0ebe7db

Please sign in to comment.