Added database recording

This commit is contained in:
Deon George 2023-03-03 10:29:39 +11:00
parent a389264ec4
commit 32a75cb140
5 changed files with 101 additions and 41 deletions

View File

@ -7,6 +7,9 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Models\Site;
use App\Models\SiteVersion;
class VersionController extends Controller class VersionController extends Controller
{ {
const CACHE_TIME = 10; // Time to cache version const CACHE_TIME = 10; // Time to cache version
@ -21,6 +24,14 @@ class VersionController extends Controller
// v1.2.3-xxx-abcdef01 // v1.2.3-xxx-abcdef01
Log::info(sprintf('Connection from [%s] reporting version [%s]',$this->getUserIpAddr(),$version)); Log::info(sprintf('Connection from [%s] reporting version [%s]',$this->getUserIpAddr(),$version));
$so = Site::firstOrCreate([
'ip_address'=>$this->getUserIpAddr()
]);
$vo = new SiteVersion;
$vo->version = $version;
$so->versions()->save($vo);
$matches = []; $matches = [];
if (preg_match(self::VERSION_REGEX,$version,$matches)) { if (preg_match(self::VERSION_REGEX,$version,$matches)) {
// If xxx is "dev" we are a development version // If xxx is "dev" we are a development version
@ -45,10 +56,12 @@ class VersionController extends Controller
$repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id); $repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id);
// Find the tag associated with version $matches[1] and see if it is more recent than $matches[4] // Find the tag associated with version $matches[1] and see if it is more recent than $matches[4]
return ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)]; $response = ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)];
} else } else
return ['unknown'=>'vn.n.n-dev-hhhhhhhh']; $response = ['unknown'=>'vn.n.n-dev-hhhhhhhh'];
break;
case 'rel': case 'rel':
$current = Cache::remember('dev',self::CACHE_TIME,function() { $current = Cache::remember('dev',self::CACHE_TIME,function() {
@ -72,19 +85,26 @@ class VersionController extends Controller
// If $matches[1] is smaller, "upgrade available" // If $matches[1] is smaller, "upgrade available"
if ($matches[1] < $current->name) if ($matches[1] < $current->name)
return ['upgrade'=>$repository]; $response = ['upgrade'=>$repository];
// If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error // If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error
elseif ($matches[1] === $current->name) elseif ($matches[1] === $current->name)
return ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository]; $response = ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository];
// if $matches[1] is higher, abort // if $matches[1] is higher, abort
else else
return ['unknown'=>$repository]; $response = ['unknown'=>$repository];
} else } else
return ['unknown'=>'vn.n.n-rel-hhhhhhhh']; $response = ['unknown'=>'vn.n.n-rel-hhhhhhhh'];
break;
} }
$vo->response = $response;
$vo->save();
return $response;
} }
// Return the current version // Return the current version
@ -95,8 +115,10 @@ class VersionController extends Controller
{ {
if (isset($_SERVER['HTTP_CLIENT_IP'])) if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP']; $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; $ips = preg_split('/,\s*/',$_SERVER['HTTP_X_FORWARDED_FOR']);
$ipaddress = $ips[0];
}
else if(isset($_SERVER['HTTP_X_FORWARDED'])) else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED']; $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) else if(isset($_SERVER['HTTP_FORWARDED_FOR']))

17
app/Models/Site.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Site extends Model
{
protected $fillable = ['ip_address'];
/* RELATIONS */
public function versions()
{
return $this->hasMany(SiteVersion::class);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SiteVersion extends Model
{
/* RELATIONS */
public function sites()
{
return $this->belongsTo(Site::class);
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable, HasFactory;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name', 'email',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var string[]
*/
protected $hidden = [
'password',
];
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sites', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->ipAddress('ip_address');
});
Schema::create('site_versions', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('version');
$table->json('response')->nullable();
$table->bigInteger('site_id');
$table->foreign('site_id')->references('id')->on('sites');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('site_versions');
Schema::dropIfExists('sites');
}
};