Exemplo n.º 1
0
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
const CUST_PATH = 'server/ext-php-server';
$scriptInvokedFromCli = isset($_SERVER['argv'][0]) && $_SERVER['argv'][0] === CUST_PATH . '/server.php';
if ($scriptInvokedFromCli) {
    $port = getenv('PORT');
    if (empty($port)) {
        $port = "3000";
    }
    echo 'starting server on port ' . $port . PHP_EOL;
    exec('php -S localhost:' . $port . ' -t ' . './client ' . CUST_PATH . '/server.php');
} else {
    return routeRequest();
}
function routeRequest()
{
    $comments = file_get_contents(CUST_PATH . '/comments.json');
    $uri = $_SERVER['REQUEST_URI'];
    if ($uri == '/') {
        echo file_get_contents('./client/' . 'index.html');
    } elseif (preg_match('/\\/api\\/comments(\\?.*)?/', $uri)) {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $commentsDecoded = json_decode($comments, true);
            $commentsDecoded[] = ['id' => round(microtime(true) * 1000), 'author' => $_POST['author'], 'text' => $_POST['text']];
            $comments = json_encode($commentsDecoded, JSON_PRETTY_PRINT);
            file_put_contents(CUST_PATH . '/comments.json', $comments);
        }
        header('Content-Type: application/json');
Exemplo n.º 2
0
<?php

/**
 * All requests should be redirected to this file, or a file with the same call
 * to routeRequest() in bootstrapper.php.
 */
include 'bootstrapper.php';
routeRequest();
Exemplo n.º 3
0
        } else {
            sendError(404, $vars[1] . " Not Found");
        }
    }
}
function routeRequest($gpio)
{
    $method = $_SERVER['REQUEST_METHOD'];
    $request_uri = $_SERVER['REQUEST_URI'];
    $root = $_SERVER['DOCUMENT_ROOT'];
    $script = $_SERVER['SCRIPT_FILENAME'];
    $path = pathinfo($script);
    $context = substr($path['dirname'], strlen($root));
    $uri = substr($request_uri, strlen($context));
    $vars = explode('/', $uri);
    global $SERVER_VERSION;
    header("Server: " + $SERVER_VERSION);
    if ($method == "GET") {
        doGET($gpio, $vars);
    } else {
        if ($method == "POST") {
            doPOST($gpio, $vars);
        } else {
            sendError(405, "Not Allowed");
        }
    }
}
$gpio = new GPIO();
$gpio->init();
routeRequest($gpio);
Exemplo n.º 4
0
<?php

/**
 * All requests should be redirected to this file, or a file with the same call
 * to routeRequest() in bootstrapper.php.
 */
include 'bootstrapper.php';
try {
    echo routeRequest();
    //Fallback error handler for when the PageEngine isn't around to help.
} catch (Exception $e) {
    echo "Sorry, there was an error: " . $e->getMessage() . ".";
}