Skip to content

Commit

Permalink
add websocket, vite bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
jefyokta committed Nov 9, 2024
1 parent bfd3394 commit 0147af3
Show file tree
Hide file tree
Showing 54 changed files with 766 additions and 5,122 deletions.
Binary file removed .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
/node_modules
.env
./public/models/*.glb
./public/models/*.gltf
./public/models/*.gltf
.DS_Store
/resources/css/app.css
/public/build
6 changes: 0 additions & 6 deletions .rest

This file was deleted.

40 changes: 3 additions & 37 deletions app/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public function document(Request $request, Response $response)
}
private function monthly()
{
return Guest::raw("SELECT * FROM tamu WHERE MONTH(created_at) = MONTH(CURDATE()) AND YEAR(created_at) = YEAR(CURDATE());")->run(true) ?? 0;
return floor((Guest::raw("SELECT * FROM tamu WHERE MONTH(created_at) = MONTH(CURDATE()) AND YEAR(created_at) = YEAR(CURDATE());")->run(true) ?? 0) * 100) / 100;
}

private function mothlymethod()
{
return Guest::raw("SELECT * FROM tamu WHERE metode = 1 AND MONTH(created_at) = MONTH(CURDATE()) AND YEAR(created_at) = YEAR(CURDATE());")->run(true) ?? 0;
return floor((Guest::raw("SELECT * FROM tamu WHERE metode = 1 AND MONTH(created_at) = MONTH(CURDATE()) AND YEAR(created_at) = YEAR(CURDATE());")->run(true) ?? 0)*100)/100;
}

private function soldAndUnsold($type = null): array
Expand All @@ -116,40 +116,6 @@ private function soldAndUnsold($type = null): array
$sold = House::select("*")->where("status", '=', 400)->run(true) ?? 0;
$booked = House::select("*")->where("status", '=', 300)->run(true) ?? 0;
return ["sold" => $sold, "unsold" => $unsold, "booked" => $booked];


// Coroutine::run(function () {
// $chan = new Channel(3);

// go(function () use ($chan) {
// $unsold = House::select("*")->where("status", '!=', 400)->run(true) ?? 0;
// $chan->push(['unsold' => $unsold]);
// });

// go(function () use ($chan) {
// $sold = House::select("*")->where("status", '=', 400)->run(true) ?? 0;
// $chan->push(['sold' => $sold]);
// });

// go(function () use ($chan) {
// $booked = House::select("*")->where("status", '=', 300)->run(true) ?? 0;
// $chan->push(['booked' => $booked]);
// });

// $results = [];
// for ($i = 0; $i < 3; $i++) {
// $results[] = $chan->pop();
// }

// $chan->close();

// $finalResults = [];
// foreach ($results as $result) {
// $finalResults = array_merge($finalResults, $result);
// }

// return $finalResults; // Mengembalikan hasil akhir
// });
} else {

$unsold = House::select("*")->where("type", '=', $type)->andWhere("status", '!=', 400)->run(true) ?? 0;
Expand All @@ -172,7 +138,7 @@ private function increment($cur, $lastMonth)
return 0;
} elseif ($cur !== 0 && $lastMonth !== 0) {
$result = (($cur - $lastMonth) / $lastMonth) * 100;
return $result;
return floor($result * 100) / 100;
} else {
return 0;
}
Expand Down
48 changes: 35 additions & 13 deletions app/Controllers/GuestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Oktaax\Http\ResponseJson;
use RuntimeException;
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client;

class GuestController
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public function booking(Request $req, Response $res)
} else {
$active = 0;

$res->render("pages.booking", compact("house", "req","active"));
$res->render("pages.booking", compact("house", "req", "active"));
}
} else {
$res->status("404")->end("not found");
Expand All @@ -79,10 +80,12 @@ public function create(Request $req, Response $res)
$phonenumber = $req->post("nohp");
$email = $req->post("email");
$job = $req->post("pekerjaan");
$houseid = $req->post("rumahid");
$houseid = $req->body("rumahid");
$file = $req->files['document'] ?? null;

$method = $req->post("metode") ?? null;


$method = $req->post("metode") ?? 0;

$house = House::find($houseid);

Expand All @@ -104,15 +107,17 @@ public function create(Request $req, Response $res)
}
$allowedFiles = ["docx", "pdf"];
$destination = $uploadDir . uniqid() . "_" . basename($file['name']);
$fileName = end(explode("/", $destination));
$fileExt = end(explode('.', $fileName)) ?? "";
$fileParts = explode("/", $destination);
$fileName = end($fileParts);

$fileExtParts = explode('.', $fileName);
$fileExt = end($fileExtParts) ?? "";

if (!in_array($fileExt, $allowedFiles)) {
return $res->withError("File harus berformat docx atau pdf")->status(302)->redirect($req->request->header['referer']);
return $res->withError("File harus berformat docx atau pdf")->status(302)->back();
} else {
$src = Coroutine::readFile($file['tmp_name']);
if ($src) {


$result = Coroutine::writeFile($destination, $src);
if ($result === false) {
throw new RuntimeException("Gagal menyimpan file.");
Expand All @@ -123,25 +128,42 @@ public function create(Request $req, Response $res)

Console::info("File uploaded: {$file['name']}");

$reuslt = Guest::insert([
$result = Guest::insert([
"name" => $name,
"phone_number" => $phonenumber,
"email" => $email,
"pekerjaan" => $job,
"document" => $fileName,
"rumahid" => $houseid,
"metode" => $method
"metode" => $method === 1 ? 1 : 0
])->run(true);
if ($result > 0) {
$res->with("Berhasil Memesan")->status(302)->redirect("/siteplan");
go(function () use ($res) {
$res->with("Berhasil Memesan")->status(302)->back();
});
go(function () use ($houseid, $name) {
$client = new Client(config('app.host'), config('app.port'),true);

if ($client->upgrade("/notification")) {
$message = json_encode([
'event' => 'booking',
'message' => 'Ada tamu baru yang membooking rumah!',
"houseId" => $houseid,
"guestName" => $name,
]);
$client->push($message);
} else {
echo "Upgrade ke WebSocket gagal.";
}
});
}
} else {
$res->withError("Gagal Membuka file ")->status(302)->redirect($req->request->header['referer'] ?? "/siteplan");
$res->withError("Gagal Membuka file ")->status(302)->back();
}
}
});
} else {
$res->withError("File Tidak ada")->status(302)->redirect($req->request->header['referer'] ?? "/siteplan");
$res->withError("File Tidak ada")->status(302)->back();
}
}

Expand Down
20 changes: 19 additions & 1 deletion app/Controllers/HouseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Oktaax\Http\Request;
use Oktaax\Http\Response;
use Oktaax\Http\ResponseJson;
use Swoole\Coroutine\Http\Client;

class HouseController
{
Expand Down Expand Up @@ -47,7 +48,24 @@ public function updateStatus(Request $req, Response $res)

$r = House::update(["status" => $status], ["id" => $id])->run(true);
if ($r > 0) {
$res->status(200)->json(new ResponseJson(['msg' => "ok"]));

go(function () use ($res) {
$res->status(200)->json(new ResponseJson(['msg' => "ok"]));
});

go(function () use ($status, $id) {
$client = new Client(config('app.host'), config('app.port'),true);

if ($client->upgrade("/notification/house")) {
$message = json_encode([
"id" => $id,
"status" => $status
]);
$client->push($message);
} else {
echo "Upgrade ke WebSocket gagal.";
}
});
} else {
$res->status(500)->json(new ResponseJson(['msg' => "notok"]));
}
Expand Down
3 changes: 1 addition & 2 deletions app/Middlewares/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function logout(Request $req, Response $res, callable $next, $user
*/
public static function tokenVerify(Request $req, Response $res, callable $next)
{
$token = $req->request->cookie['X-KamelaSess'];
$token = $req->cookie('X-KamelaSess') ?? false;

if ($token) {
try {
Expand All @@ -92,7 +92,6 @@ public static function tokenVerify(Request $req, Response $res, callable $next)
$next($decoded);
} else {
$res->response->redirect('/login');
Console::error("ngentot");
}
}

Expand Down
41 changes: 35 additions & 6 deletions app/Test/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@
use Kamela\Models\User;
use Oktaax\Console;
use Oktaax\Oktaa;
use Swoole\Client;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\Http\Client as HttpClient;

require_once __DIR__ . "/../init.php";
require_once __DIR__ . "/../../vendor/autoload.php";

return new class
{
public function run()
{

Coroutine::run(function () {
function getMimeType($file)
{
// Memastikan file ada
if (!is_file($file)) {
return false;
}

var_dump($this->async());
});
// Menggunakan finfo untuk mendeteksi MIME type
$finfo = finfo_open(FILEINFO_MIME_TYPE); // Dapatkan tipe MIME
$mimeType = finfo_file($finfo, $file);
finfo_close($finfo);

return $mimeType;
}

echo getMimeType(__DIR__."/../../public/build/assets/app-CudV09GM.js");
}

public function seed()
Expand Down Expand Up @@ -80,7 +94,22 @@ private function async()
$finalResults = array_merge($finalResults, $result);
}

return $finalResults; // Mengembalikan hasil akhir
// });
return $finalResults;
}

private function connect()
{

$client = new HttpClient('127.0.0.1', 8000);
$client->upgrade('/notification');

$message = [
'event' => 'booking',
'message' => 'Ada tamu baru yang membooking rumah!'
];

$client->push(json_encode($message));

// $client->close();
}
};
45 changes: 45 additions & 0 deletions app/Websocket/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Kamela\Websocket;

use Swoole\Coroutine;
use Swoole\Table;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;

class Notification
{


public static function handle(Server $server, Frame $frame, Table $table)
{

$data = $frame->data;

$message = json_decode($data);

foreach ($table as $fd => $row) {
$details = json_decode($row['details']);
if ($details->isAdmin === 1) {
if ($server->exists($fd)) {
$server->push($fd, json_encode(["message" => $message->message, "guestName" => $message->guestName, "houseId" => $message->houseId]));
}
}
}
$server->push($frame->fd, json_encode($data));
}


public static function house(Server $server, Frame $frame, Table $table)
{
$data = json_decode($frame->data);

foreach ($server->connections as $fd) {

$server->push($fd, json_encode(["event" => "houseUpdate", "house" => [
"id" => $data->id,
"status" => $data->status
]]));
}
}
}
13 changes: 8 additions & 5 deletions app/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
"app" => [
"name" => "KamelaPermai",
"locale" => "ID",
"key" => OktaEnv("APPKEY")
"key" => OktaEnv("APP_KEY"),
"host" => OktaEnv("APP_HOST") ?? "kamela-permai.oktaa",
"port" => OktaEnv("APP_PORT") ?? 8000,

],
"db" => [
"host" => OktaEnv("DBHOST"),
"user" => OktaEnv("DBUSER"),
"password" => OktaEnv("DBPASSWORD"),
"name" => OktaEnv("DBNAME"),
"host" => OktaEnv("DB_HOST"),
"user" => OktaEnv("DB_USER"),
"password" => OktaEnv("DB_PASSWORD"),
"name" => OktaEnv("DB_NAME"),
"port" => 3306,
"connection" => "mysql"

Expand Down
16 changes: 11 additions & 5 deletions app/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ function url($url)

return $url;
}
// function request(): Request
// {
// global $currentRequest;
// return $currentRequest;
// }


function vite($path) {
$manifestPath = __DIR__ . '/../../public/build/.vite/manifest.json';
if (!file_exists($manifestPath)) {
throw new Exception('Vite manifest not found.');
}

$manifest = json_decode(file_get_contents($manifestPath), true);
return "/build/".$manifest[$path]['file'] ?? null;
}
Binary file added bun.lockb
Binary file not shown.
7 changes: 7 additions & 0 deletions cert/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Usage
this certificate is sign for host kamela-permai.oktaa, so:

- add kamela-permai.oktaa to your hosts files
- trust the ca file: jefyokta-cert.pem for your device

congratulations, now you can use https://kamela-permai.oktaa with valid certfificate
Loading

0 comments on commit 0147af3

Please sign in to comment.