Merge pull request #8 from lewis-wow/pr/api

Pr/api
This commit is contained in:
Ludvík Prokopec 2022-09-08 13:39:06 +02:00 committed by GitHub
commit 998d23b812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 167 additions and 109 deletions

1
.gitignore vendored
View File

@ -24,4 +24,5 @@ dist-ssr
*.sw?
# php
/public/vendor
/api/vendor

View File

@ -1,15 +0,0 @@
<Files ".env">
Order Allow,Deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

View File

@ -1,59 +0,0 @@
<?php
function remoteFileExists($url) {
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
return [
"post" => function($data, $headers, $db) {
$imagesHostName = 'https://geohry.skolazdola.cz/';
if(!isset($data['gameurl']) || empty($data['gameurl'])) return [
"error" => "Property 'gameurl' is not set."
];
$gameUrl = $data['gameurl'];
$gameDetails = $db->select('games4', '*', [
'url' => $gameUrl
])[0];
$gameDetails["questions"] = $db->select('questions4', '*', [
'url' => $gameUrl
]);
$thumbnailPath = "{$imagesHostName}/games/{$gameUrl}/intro.jpg";
$gameDetails["thumbnail"] = remoteFileExists($thumbnailPath) ? $thumbnailPath : null;
foreach($gameDetails["questions"] as &$question) {
$questionThumbnailPath = "{$imagesHostName}/games/{$gameUrl}/" . $question["uniqid"] . ".jpg";
$question["thumbnail"] = remoteFileExists($questionThumbnailPath) ? $questionThumbnailPath : null;
}
return $gameDetails;
}
];

View File

@ -1,6 +1,6 @@
{
"config": {
"vendor-dir": "api/vendor"
"vendor-dir": "public/vendor"
},
"require": {
"bramus/router": "1.5",

View File

@ -5,7 +5,15 @@
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
<title>Erant</title>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8RCL0H1Q7V"></script>
<script>
window.dataLayer = window.dataLayer || []
function gtag() { dataLayer.push(arguments) }
gtag('js', new Date())
gtag('config', 'G-8RCL0H1Q7V');
</script>
</head>
<body>

13
package-lock.json generated
View File

@ -8,7 +8,8 @@
"name": "geohry",
"version": "0.0.0",
"dependencies": {
"svelte-routing": "^1.6.0"
"svelte-routing": "^1.6.0",
"yallist": "^4.0.0"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.1",
@ -1449,6 +1450,11 @@
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"dev": true
},
"node_modules/yallist": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
"node_modules/yaml": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz",
@ -2327,6 +2333,11 @@
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"dev": true
},
"yallist": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
"yaml": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz",

View File

@ -7,7 +7,7 @@
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"php": "php -S localhost:8000 -t ./api"
"php": "php -S localhost:8000 -t ./public"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.1",
@ -20,6 +20,7 @@
"vite": "^3.0.0"
},
"dependencies": {
"svelte-routing": "^1.6.0"
"svelte-routing": "^1.6.0",
"yallist": "^4.0.0"
}
}

View File

@ -1,15 +1,10 @@
<Files ".env">
<Files "api/.env">
Order Allow,Deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

View File

@ -1,11 +1,6 @@
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/utils/cors.php';
use Medoo\Medoo;
cors();
header('Content-Type: application/json; charset=utf-8');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
@ -16,8 +11,6 @@ function env($key) {
return array_key_exists($key, $_ENV) ? $_ENV[$key] : null;
}
$router = new \Bramus\Router\Router();
$database = new Medoo([
'type' => 'mysql',
'host' => env("DB_HOST"),
@ -49,7 +42,10 @@ $getFolderContentUtil = function($dir) use($routesDirectory, &$getFolderContentU
$router->{$key}(str_replace(".php", '', $pathWithoutRelativeRoot), function() use($router, $fn, $data, $error, $database) {
$responseResult = $fn($data, getallheaders(), $database, $error, $router);
if($responseResult) {
echo json_encode($responseResult);
$responseResult["status"] = array_key_exists("status", $responseResult) ? $responseResult["status"] : 200;
http_response_code($responseResult["status"]);
echo json_encode($responseResult["data"]);
exit();
}
});
}

View File

@ -0,0 +1,74 @@
<?php
return [
"post" => function($data, $headers, $db) {
function remoteFileExists($url) {
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
$imagesHostName = 'https://geohry.skolazdola.cz/';
if(!isset($data['gameurl']) || empty($data['gameurl'])) return [
"data" => [
"success" => false,
"message" => "Property 'gameurl' is not set."
]
];
$gameUrl = $data['gameurl'];
$gameDetails = $db->select('games4', '*', [
'url' => $gameUrl
]);
if(!$gameDetails) return [
"data" => [
"success" => false,
"message" => "Invalid 'gameurl'."
]
];
$gameDetails = $gameDetails[0];
$gameDetails["questions"] = $db->select('questions4', '*', [
'url' => $gameUrl
]);
$thumbnailPath = "{$imagesHostName}/games/{$gameUrl}/intro.jpg";
$gameDetails["thumbnail"] = remoteFileExists($thumbnailPath) ? $thumbnailPath : null;
foreach($gameDetails["questions"] as &$question) {
$questionThumbnailPath = "{$imagesHostName}/games/{$gameUrl}/" . $question["uniqid"] . ".jpg";
$question["thumbnail"] = remoteFileExists($questionThumbnailPath) ? $questionThumbnailPath : null;
}
return [
"success" => true,
"data" => $gameDetails
];
}
];

View File

@ -1,5 +1,4 @@
<?php
/**
* An example CORS-compliant method. It will allow any GET, POST, or OPTIONS requests from any
* origin.
@ -18,20 +17,21 @@ function cors() {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Header: *");
header("Access-Control-Max-Age: 86400"); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
header("Access-Control-Allow-Origin: *");
exit(0);
}
}

View File

@ -1,3 +1,30 @@
<?php
require_once(__DIR__ . '/index.html');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/cors.php';
cors();
$router = new \Bramus\Router\Router();
$router->setBasePath('/');
$router->mount("/api", function() use($router) {
require_once __DIR__ . '/api/index.php';
});
$router->set404('/api(/.*)?', function() {
header('HTTP/1.1 404 Not Found');
header('Content-Type: application/json');
echo json_encode([
"message" => "Route not defined."
]);
});
$router->get("/.*", function() {
header('Content-Type: text/html; charset=UTF-8');
echo "frontend!";
});
$router->run();

View File

@ -1,13 +1,16 @@
async function send({ method, path, body, token, headers }) {
const opts = { method, headers: {} }
opts.headers['Content-Type'] = 'application/json'
const opts = { method, headers: new Headers(), mode: 'cors' }
opts.headers.append('Content-Type', 'application/json')
opts.headers.append('Access-Control-Allow-Origin', '*')
if (body) {
opts.body = JSON.stringify(body)
}
if (headers) {
opts.headers = Object.fromEntries(headers.map(({ key, value }) => [key, value]))
for (const [k, v] of Object.entries(headers)) {
opts.headers.append(k, v)
}
}
if (token) {

View File

@ -0,0 +1,16 @@
export default (question, questionType) => {
switch (questionType.toLowerCase()) {
case 'text':
return question
case 'number':
return Number.parseFloat(question)
case 'choice':
return question.split(/;\s*/).map(item => ({
label: item.startsWith('*') ? item.substring(1) : item,
value: item.startsWith('*') ? true : false
}))
default:
return null
}
}

View File

@ -6,16 +6,16 @@
import Button from '../lib/Button.svelte'
import * as api from '$lib/utils/api'
import parseQuestion from '$lib/utils/parseQuestion'
let assets = Array.from(Array(4).keys()).map((_, i) => `/assets/temp/kar${i + 1}.png`)
;(async () => {
console.log(api.hostName)
const gameData = await api.post(`${api.hostName}/game/details`, {
gameurl: 'pisek-mesto-kralovske',
})
console.log(gameData)
console.log(parseQuestion(gameData.questions[0].answer, gameData.questions[0].type))
})()
</script>