From 1737f040dc0bc7c801183589d25746f46f5adc0a Mon Sep 17 00:00:00 2001 From: "DESKTOP-GBA0BK8\\Admin" Date: Sun, 16 Apr 2023 15:09:47 -0400 Subject: [PATCH] Added apigate parts --- www-api/app/Controllers/WrenchApi.php | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/www-api/app/Controllers/WrenchApi.php b/www-api/app/Controllers/WrenchApi.php index 9cbaf022..51d725c2 100644 --- a/www-api/app/Controllers/WrenchApi.php +++ b/www-api/app/Controllers/WrenchApi.php @@ -18,6 +18,9 @@ class WrenchApi extends BaseController public function index() { + + + $data['envID'] = getenv('ENV_ID'); $data['home_background'] = getenv('HOME_PAGE_BACKGROUND'); return view('welcome_message',$data); @@ -70,6 +73,57 @@ return $endpoints; public function apigate(){ + //$request = service('request'); + header('Access-Control-Allow-Origin: *'); + //header("Access-Control-Allow-Origin: http://localhost:9057 "); + header('Access-Control-Expose-Headers: Access-Control-Allow-Origin'); + //header('Access-Control-Allow-Credentials: true '); + //header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"); + header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS'); + header('Content-type: application/json'); + + // what is the endpoint + $uri = urldecode(current_url(true)); + $findme = '?'; + $pos = strpos($uri, $findme); + if ($pos > 5) { + $uri = substr($uri, 0, $pos); + } + log_message('critical', "API-GATE URI -> ".$uri ); + + $pieces = explode('/', $uri); + $psc = count($pieces); + + $endpoint = $psc > 0 ? $pieces[$psc - 1] : ''; + log_message('critical', "Enpoint-> ".$endpoint ); + + $endpoints = $this->endPointList(); + + $res1 = []; + if (array_key_exists($endpoint, $endpoints)) { + } else { + http_response_code(404); + // tell the user product does not exist + echo json_encode([ + 'message' => 'Endpoint not found.', + 'URI' => $uri, + ]); + } + // echo "EXYTACT INPUT DATA HERE"; + $raw_json = file_get_contents('php://input'); + $raw_array = json_decode($raw_json, true); + + if ($_SERVER['REQUEST_METHOD'] == 'GET') { + log_message('critical', "Enpoint LOC2 HERE -> ".$endpoint ); + $get_param = $_GET['reqData'] ?? null; + $raw_array = ($get_param!=null) ? json_decode($get_param, true):[]; + } + + + //-- move to another module start + $call_backend = true; + $in["loc"] = $_SERVER["REMOTE_ADDR"]; + $raw_array =[]; $res1 = $this->dummyData($raw_array); return $this->response->setJson($res1); @@ -333,6 +387,7 @@ return $endpoints; private function dummyData($raw_array) { return [ + 'internal_return' => 0, 'msg' => 'Not implemented yet', 'raw_data' => $raw_array, ];