Skip to content

Commit

Permalink
API is completed. Release v1.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
laijingwu committed Jan 2, 2019
1 parent e73e42a commit cbb4e03
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### URL Shortener

#### 简述

轻量级自定义短链接生成

#### 依赖

[vlucas/phpdotenv](https://github.com/vlucas/phpdotenv)

[catfan/medoo](https://github.com/catfan/Medoo)

#### 安装

`composer require laijingwu/url-shortener`

#### Intro

A light url shortener for customizing links.

#### Dependence

[vlucas/phpdotenv](https://github.com/vlucas/phpdotenv)

[catfan/medoo](https://github.com/catfan/Medoo)

#### Installation

`composer require laijingwu/url-shortener`







2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "laijingwu",
"email": "[email protected]",
"homepage": "http://laijingwu.com"
"homepage": "https://laijingwu.com"
}
],
"require": {
Expand Down
12 changes: 12 additions & 0 deletions public/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
56 changes: 56 additions & 0 deletions public/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
require __DIR__."/../vendor/autoload.php";
header("Content-Type: application/json");

$raw_post_data = file_get_contents('php://input', 'r');
$data = json_decode($raw_post_data);
if (!$data || !isset($data->type) || !isset($data->uri)) {
echo json_encode([
'code' => 1000,
'errmsg' => "invalid request."
]);
exit;
}

$dotenv = new Dotenv\Dotenv(__DIR__."/../");
$dotenv->load();
$dotenv->required(['DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD', 'DB_TABLE']);
$config = require __DIR__."/../config.php";
$shortener = UrlShortener\UrlShortener::getInstance($config['db'], $config['options']);

$type = $data->type;
$uri = $data->uri;
if ($type == "short") {
$long = $shortener->toLong($uri);
if ($long == false) {
echo json_encode([
'code' => 1001,
'errmsg' => "invalid short uri."
]);
exit;
}
echo json_encode([
'code' => 0,
'data' => $long
]);
} elseif ($type == "long") {
$short = $shortener->toShort($uri);
if ($short == false) {
echo json_encode([
'code' => -1,
'errmsg' => "database error."
]);
exit;
}
echo json_encode([
'code' => 0,
'data' => $short
]);
} else {
echo json_encode([
'code' => 1000,
'errmsg' => "invalid request."
]);
exit;
}

6 changes: 3 additions & 3 deletions index.php → public/index.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
require __DIR__."/vendor/autoload.php";
require __DIR__."/../vendor/autoload.php";

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv = new Dotenv\Dotenv(__DIR__."/../");
$dotenv->load();
$dotenv->required(['DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD', 'DB_TABLE']);

$uri = getUri();

$config = require __DIR__."/config.php";
$config = require __DIR__."/../config.php";
$shortener = UrlShortener\UrlShortener::getInstance($config['db'], $config['options']);

$long = $shortener->toLong($uri);
Expand Down
4 changes: 2 additions & 2 deletions url_shortener.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Target Server Version : 50553
File Encoding : 65001
Date: 29/09/2018 21:16:01
Date: 02/01/2019 10:39:29
*/

SET NAMES utf8mb4;
Expand All @@ -29,6 +29,6 @@ CREATE TABLE `url_shortener` (
`created_at` datetime NOT NULL COMMENT 'create time',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `code`(`code`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
) ENGINE = MyISAM AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

0 comments on commit cbb4e03

Please sign in to comment.