Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alihardan committed Jun 14, 2024
1 parent f9b1814 commit 4683383
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 152 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Deploy

1. apt-get install certbot -y && certbot certonly --standalone --agree-tos --register-unsafely-without-email --key-type rsa -d backend.oldpersiangames.org && certbot renew --dry-run
1. get docker-compose.yml and put in /opt/opgmanager-backend/docker-compose.yml (remove build at app and uncomment image at app)
1. copy .env file kenare docker-compose.yml, set mysql host to "db", and set user pass, add APP_KEY, also:
SANCTUM_STATEFUL_DOMAINS=dash.oldpersiangames.org
SESSION_DOMAIN=.oldpersiangames.org
FRONTEND_URL=https://dash.oldpersiangames.org
also TELEGRAM_TOKEN=bot_token and OWNER_TG_ID=yourid
also TELEGRAM_API and TELEGRAM_HASH for your python bot
1. docker compose pull && docker compose up --build -d && docker compose exec app bash -c "php artisan config:cache && php artisan route:cache && php artisan optimize"
1. git clone https://${BACKUP_TOKEN}@github.com/oldpersiangames/opg-backups /opgactions/opg-backups && cd /opgactions/opg-backups && git config user.email [email protected] && git config user.name alihardan
1. docker compose exec app bash -c "mysql -h db -u opg -p123456 opg < /opgactions/opg-backups/opgmanager.sql"
1. docker compose exec app bash -c "chown -R application:application /opgactions"
- apt-get install certbot -y && certbot certonly --standalone --agree-tos --register-unsafely-without-email --key-type rsa -d backend.oldpersiangames.org && certbot renew --dry-run
- get docker-compose.yml and put in /opt/opgbackend/docker-compose.yml (remove build at app and uncomment image at app)
- copy .env file kenare docker-compose.yml, set mysql host to "db", and set user pass, add APP_KEY, also:
SANCTUM_STATEFUL_DOMAINS=dash.oldpersiangames.org
SESSION_DOMAIN=.oldpersiangames.org
FRONTEND_URL=https://dash.oldpersiangames.org
also TELEGRAM_TOKEN=bot_token and OWNER_TG_ID=yourid
also TELEGRAM_API and TELEGRAM_HASH for your python bot
also OPG_KEY_HASH
- docker compose pull && docker compose up --build -d
- git clone https://${BACKUP_TOKEN}@github.com/oldpersiangames/opg-backups /opgactions/opg-backups && cd /opgactions/opg-backups && git config user.email [email protected] && git config user.name alihardan
- docker compose exec app bash -c "mysql -h db -u opg -p123456 opg < /opgactions/opg-backups/opgbackend.sql"
- docker compose exec app bash -c "chown -R application:application /opgactions"
- docker compose exec app bash -c "php artisan config:cache && php artisan route:cache && php artisan optimize"
4 changes: 3 additions & 1 deletion app/Console/Commands/InsertMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Models\Item;
use Illuminate\Console\Command;

/* Not used anymore */

class InsertMedia extends Command
{
/**
Expand Down Expand Up @@ -39,7 +41,7 @@ public function handle()
foreach ($games as $game) {
if ($game->photos)
foreach ($game->photos as $photo) {
$game->addMediaFromDisk('gamePhotos/' . $game->id . '/' . $photo['id'] . '.jpg', 'public')->usingFileName(str_replace('/tmp/', '', tempnam(sys_get_temp_dir(), 'media-library')))->preservingOriginal()->toMediaCollection();
$game->addMediaFromDisk('game-photos/' . $game->id . '/' . $photo['id'] . '.jpg', 'public')->usingFileName(str_replace('/tmp/', '', tempnam(sys_get_temp_dir(), 'media-library')))->preservingOriginal()->toMediaCollection();
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions app/Console/Commands/TransferContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;

/* It was a temporary script used to manage transfering data from the legacy website to the new one.
This command not used anymore */

class TransferContents extends Command
{
Expand Down Expand Up @@ -48,14 +49,11 @@ public function handle()
$result = Process::run("node -e \"const myToken = require('/oldpersiangames.github.io/src/_data/games.js');
myToken().then(token => {
process.stdout.write(JSON.stringify(token));
}).catch(err => {
console.log(err);
});\"");
// echo $result->errorOutput();
$games = json_decode($result->output(), true);


// $tgfiles = File::json('/archive/tmp/opg/sourceout-20230918.json');
$tgfiles = File::json('/sourceout-20230918.json');
$tgfiles = array_reverse($tgfiles);

Expand Down Expand Up @@ -189,7 +187,7 @@ public function handle()
'name' => $contribute['username'],
'email' => $contribute['username'] . "@oldpersiangames.org",

], ["password" => Hash::make('12345678')]);
]);

foreach ($contribute['contribute'] as $cc) {
$g->contributes()->create([
Expand All @@ -201,15 +199,15 @@ public function handle()
}

if ($game['photos']) {
// if (!file_exists('/archive/tmp/opgmanager-backend/storage/app/gamePhotos/' . $g->id . '/'))
// mkdir('/archive/tmp/opgmanager-backend/storage/app/gamePhotos/' . $g->id . '/', 0777, true);
if (!file_exists('/usr/share/nginx/backend.oldpersiangames.org/opgmanager-backend/storage/app/gamePhotos/' . $g->id . '/'))
mkdir('/usr/share/nginx/backend.oldpersiangames.org/opgmanager-backend/storage/app/gamePhotos/' . $g->id . '/', 0777, true);
// if (!file_exists('/archive/tmp/opgbackend/storage/app/game-photos/' . $g->id . '/'))
// mkdir('/archive/tmp/opgbackend/storage/app/game-photos/' . $g->id . '/', 0777, true);
if (!file_exists('/usr/share/nginx/backend.oldpersiangames.org/opgbackend/storage/app/game-photos/' . $g->id . '/'))
mkdir('/usr/share/nginx/backend.oldpersiangames.org/opgbackend/storage/app/game-photos/' . $g->id . '/', 0777, true);
$photos = [];
foreach ($game['photos'] as $index => $photo) {

// copy('/archive/tmp/oldpersiangames.github.io/src/games/' . $game['id'] . '/photos/' . $photo['path'], '/archive/tmp/opgmanager-backend/storage/app/gamePhotos/' . $g->id . '/' . ($index + 1) . '.jpg');
copy('/oldpersiangames.github.io/src/games/' . $game['id'] . '/photos/' . $photo['path'], '/usr/share/nginx/backend.oldpersiangames.org/opgmanager-backend/storage/app/gamePhotos/' . $g->id . '/' . ($index + 1) . '.jpg');
// copy('/archive/tmp/oldpersiangames.github.io/src/games/' . $game['id'] . '/photos/' . $photo['path'], '/archive/tmp/opgbackend/storage/app/game-photos/' . $g->id . '/' . ($index + 1) . '.jpg');
copy('/oldpersiangames.github.io/src/games/' . $game['id'] . '/photos/' . $photo['path'], '/usr/share/nginx/backend.oldpersiangames.org/opgbackend/storage/app/game-photos/' . $g->id . '/' . ($index + 1) . '.jpg');
$photos[] = [
"id" => $index + 1,
"type" => $photo['type'],
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/UpdateDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function handle()
$result = Process::run("cd /opgactions/opg-backups/ && git pull");
// echo $result->output();
// echo $result->errorOutput();
$result = Process::timeout(600)->run("mysql -u root -p1 opg < /opgactions/opg-backups/opgmanager.sql");
$result = Process::timeout(600)->run("mysql -u root -p1 opg < /opgactions/opg-backups/opgbackend.sql");
}
}
77 changes: 77 additions & 0 deletions app/Http/Controllers/CICDController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Process;
use App\Models\Game;
use App\Models\Item;
use App\Models\TGFile;
use SergiX44\Nutgram\Nutgram;

class CICDController extends Controller
{
public function makeBackup(Request $request)
{
if (hash('sha256', $request->key) != env('OPG_KEY_HASH'))
abort(403);

Process::path('/opgactions/opg-backups')->run('mysqldump --skip-extended-insert --skip-dump-date -h' . env('DB_HOST') . ' -u' . env('DB_USERNAME') . ' -p' . env('DB_PASSWORD') . ' ' . env('DB_DATABASE') . ' > opgbackend.sql');
Process::path('/opgactions/opg-backups')->run('git add .');
Process::path('/opgactions/opg-backups')->run('git commit -m "' . Carbon::now()->setTimezone('UTC')->toDateTimeString() . '"');
Process::path('/opgactions/opg-backups')->run('git push');
}

public function beforeIa(Request $request)
{
if (hash('sha256', $request->key) != env('OPG_KEY_HASH'))
abort(403);

$games = Game::whereNull('ia_id')->where('selling', false)
->whereNot('slug', 'grand-theft-auto-san-andreas-oldpersiangames')
->get(['slug', 'tgfiles', 'games', 'title_en'])->map(function ($game) {
$tgfiles = TGFile::whereIn('file_unique_id', $game->tgfiles)->orderByRaw("FIELD(file_unique_id, '" . implode("','", $game->tgfiles) . "')")->get(['file_name', 'file_size', 'file_id', 'file_unique_id', 'date']);
$game->tgfiles = $tgfiles->toArray();

$game->title = $game->title_en ?? $game->games[0]['title_en'][0];
$game->type = 'game';
unset($game->title_en);
unset($game->games);
return $game;
});


$items = Item::whereNull('ia_id')->where('selling', false)
->get(['slug', 'tgfiles', 'title_en'])->map(function ($item) {
$tgfiles = TGFile::whereIn('file_unique_id', $item->tgfiles)->orderByRaw("FIELD(file_unique_id, '" . implode("','", $item->tgfiles) . "')")->get(['file_name', 'file_size', 'file_id', 'file_unique_id', 'date']);
$item->tgfiles = $tgfiles->toArray();

$item->title = $item->title_en;
$item->type = 'item';
return $item;
});

return [...$games, ...$items];
}

public function setIa(Request $request, Nutgram $bot)
{
if (hash('sha256', $request->key) != env('OPG_KEY_HASH'))
abort(403);

$value = ['ia_id' => $request->slug];
if ($request->has('filesJSON'))
$value['files'] = $request->filesJSON;

$item = Game::where('slug', $request->slug)->whereNull('ia_id')->first();
if (!$item) $item = Item::where('slug', $request->slug)->whereNull('ia_id')->first();

$item->update($value);

$bot->sendMessage(
text: $request->slug,
chat_id: env("OWNER_TG_ID")
);
}
}
20 changes: 0 additions & 20 deletions app/Http/Controllers/OptionController.php

This file was deleted.

32 changes: 7 additions & 25 deletions app/Http/Controllers/PublicApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace App\Http\Controllers;

use App\Http\Resources\GameCollection;
use App\Http\Resources\GameResource;
use App\Models\Game;
use App\Models\Item;
use App\Models\TGFile;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Storage;

class PublicApiController extends Controller
{
public function games(Request $request)
public function games()
{
return GameResource::collection(Game::with(['producers:slug,title_fa,title_en', 'publishers:slug,title_fa,title_en'])->latest()->get());
}
Expand All @@ -24,18 +22,14 @@ public function game($slug)
$game = Game::with(['producers:id,title_en,title_fa', 'publishers:id,title_en,title_fa', 'contributes:id,user_id,time,contributable_type,contributable_id,contribute,sort', 'contributes.user:id,name'])->where('slug', $slug)->firstOrFail();
$tgfiles = TGFile::whereIn('file_unique_id', $game->tgfiles)->orderByRaw("FIELD(file_unique_id, '" . implode("','", $game->tgfiles) . "')")->get(['file_id', 'file_name', 'file_size', 'date']);
$game->tgfiles = $tgfiles->toArray();
$game->producers = $game->producers->map(function ($company) {
unset($company['pivot']);
});
$game->publishers = $game->publishers->map(function ($company) {
unset($company['pivot']);
});

$titles = [];
if ($game->title_en)
$titles[] = $game->title_en;
foreach ($game->games as $g) {
array_push($titles, ...$g['title_en']);
}

$relatedGames = Game::with(['producers:id,title_en,title_fa', 'publishers:id,title_en,title_fa'])
->whereNot('id', $game->id)
->where(function (Builder $query) use ($titles) {
Expand All @@ -45,12 +39,6 @@ public function game($slug)
}
})->get(['id', 'slug', 'title_fa', 'title_en', 'games']);

$relatedGames->map(function ($g) {
$g->producers = $g->producers;
$g->publishers = $g->publishers;
return $g;
});

$game->related = $relatedGames;

$photos = $game->getMedia()->map(function ($media) {
Expand All @@ -60,7 +48,7 @@ public function game($slug)
return [...$game->toArray(), 'photos' => $photos];
}

public function items(Request $request)
public function items()
{
return Item::latest()->with(['producers:title_fa,title_en', 'publishers:title_fa,title_en'])->get(['id', 'slug', 'title_fa', 'title_en', 'release_dates']);
}
Expand All @@ -70,22 +58,16 @@ public function item($slug)
$item = Item::with(['producers:slug,title_en,title_fa', 'publishers:slug,title_en,title_fa', 'contributes:user_id,contributable_type,contributable_id,contribute', 'contributes.user:id,name'])->where('slug', $slug)->firstOrFail();
$tgfiles = TGFile::whereIn('file_unique_id', $item->tgfiles)->orderByRaw("FIELD(file_unique_id, '" . implode("','", $item->tgfiles) . "')")->get(['file_id', 'file_name', 'file_size', 'date']);
$item->tgfiles = $tgfiles->toArray();
$item->producers = $item->producers->map(function ($company) {
unset($company['pivot']);
});
$item->publishers = $item->publishers->map(function ($company) {
unset($company['pivot']);
});

$photos = $item->getMedia()->map(function ($media) {
return $media->getFullUrl();
});
return [...$item->toArray(), 'photos' => $photos];
}

public function users(Request $request)
public function users()
{
$users = User::orderBy('name', 'asc')->with(['contributes:id,user_id,contributable_type,contributable_id,contribute'])->get(['id', 'name', 'telegram']);
return $users;
return User::orderBy('name', 'asc')->with(['contributes:id,user_id,contributable_type,contributable_id,contribute'])->get(['id', 'name', 'telegram']);
}

public function lostGames()
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
# build:
# context: ./
# dockerfile: Dockerfile
image: ghcr.io/oldpersiangames/opgmanager-backend:main
image: ghcr.io/oldpersiangames/opgbackend:main
env_file:
- path: ./.env
required: true
Expand Down
Loading

0 comments on commit 4683383

Please sign in to comment.