init commit

This commit is contained in:
Jean TOULZA 2018-07-18 12:31:42 +02:00
parent 6327fb08af
commit 296f429bac
4 changed files with 93 additions and 1 deletions

3
.htaccess Normal file
View File

@ -0,0 +1,3 @@
RewriteEngine on
RewriteRule ^/?get/(.+)$ /dweet.io/redis.php?action=get&key=$1
RewriteRule ^/?set/(.+)/(.+)$ /dweet.io/redis.php?action=set&key=$1&val=$2

View File

@ -1,3 +1,15 @@
# dweet # dweet
dweet.io-like opensource dweet.io-like opensource
Installation
------------
1. `git clone` this repo.
2. `composer install`
API
------------
- `https://your-dweet.url/get/<key>`
- `https://your-dweet.url/set/<key>/<value>`
Values are stored up to 24 hours.

13
composer.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "tasty/dweet",
"type": "project",
"require": {
"predis/predis": "^1.1"
},
"authors": [
{
"name": "HipsterCat",
"email": "tasty@hipstercat.fr"
}
]
}

64
redis.php Normal file
View File

@ -0,0 +1,64 @@
<?php
function g($key) {
if (isset($_GET[$key])) return $_GET[$key];
else return false;
}
function _die($type, $msg) {
header('Content-Type: application/json');
die(json_encode(array($type => $msg)));
}
function edie($msg) {
_die("error", $msg);
}
function sdie($msg) {
_die("success", $msg);
}
function ddie($msg) {
_die("data", $msg);
}
function get_redis($key) {
$client = new Predis\Client();
ddie($client->get($key));
}
function set_redis($key, $val) {
$client = new Predis\Client();
$client->set($key, $val);
$client->expire($key, 86400); // 1d
sdie("ok");
}
require 'vendor/autoload.php';
Predis\Autoloader::register();
if (g("action") === "get") {
$key = g("key");
if ($key) {
// get redis key
get_redis($key);
} else {
// no key
edie("no key given");
}
} elseif (g("action") === "set") {
$key = g("key");
if ($key) {
$val = g("val");
if ($val) {
// set redis val for key
set_redis($key, $val);
} else {
$data = json_decode(file_get_contents('php://input'), true);
if ($data === NULL) {
// got key but no val
edie("no val given");
} else {
set_redis($key, $data);
}
}
} else {
// no key
edie("no key given");
}
} else {
edie("no action given");
}
?>