使用flask以及sqlite製作的短網址服務
請事先安裝好python3以及確認系統上有sqlite3
$ git https://github.com/seal0112/flask-short-url
$ cd flask-short-url/
$ pip install virtualenv
$ virtualenv venv
在 Windows 系統中,使用:
venv\Scripts\activate.bat
在 Unix 或 MacOS 系統,使用:
$ source venv/bin/activate
$ pip install -r requirements.txt
$ flask db stamp head
$ flask db migrate -m "commit message"
$ flask db upgrade
$ gunicorn --bind=0.0.0.0:5000 wsgi:app # 指定host以及port
$ gunicorn --reload wsgi:app
Returns array of json data about all short url.
-
URL
/created-url
-
Method:
GET
-
Data Params
None
-
Success Response:
- Code: 200
Content:[ {"alias": "http://0.0.0.0:5000/1234567", "origin": "https://www.gmail.com/"}, {"alias": "http://0.0.0.0:5000/BFffktj", "origin": "https://www.gmail.com"} ]
- Code: 200
create new short url.
-
URL
/created-url
-
Method:
POST
-
Data Params
{ 'origin: 'http://google.com'} or { 'origin: 'http://google.com', 'alias': '1234567'}
-
Success Response:
- Code: 201
Content:{"alias": "http://0.0.0.0:5000/1234567", "origin": "https://www.gmail.com/"}
- Code: 201
-
Error Response:
- Code: 400 Bad Request
Content:{ error : "alias collision" }
- Code: 400 Bad Request
redirect browser to origin url.
-
URL
/
-
Method:
GET
-
URL Params
Required:
token=[string]
-
Data Params
None
-
Success Response:
- Code: 302
Content: None
- Code: 302
-
Error Response:
- Code: 404 Not Found
Content:{ error : "Couldn't find long url for <token>" }
- Code: 404 Not Found
redirect browser to origin url.
-
URL
/top-three-visited
-
Method:
GET
-
Data Params
None
-
Success Response:
- Code: 200
Content:[ { "alias": "http://0.0.0.0:5000/1234567", "count": 2, "origin": "https://www.gmail.com/" }, { "alias": "http://0.0.0.0:5000/9dRreG0", "count": 13, "origin": "https://tw.yahoo.com/" }, { "alias": "http://0.0.0.0:5000/o5HDQR", "count": 16, "origin": "https://www.google.com" } ]
- Code: 200
Return 's hit count.
-
URL
/hit-count/
-
Method:
GET
-
URL Params
Required:
token=[string]
-
Data Params
None
-
Success Response:
- Code: 200
Content:{ "alias": "http://0.0.0.0:5000/9dRreG0", "count": 13, "origin": "https://tw.yahoo.com/" }
- Code: 200
-
Error Response:
- Code: 404 Not Found
Content:{ error : "Couldn't find hit count record for <token>" }
- Code: 404 Not Found
Return short url hit count record, group by date, of all time.
-
URL
/hit-count-group-by-date
-
Method:
GET
-
URL Params
None
-
Data Params
None
-
Success Response:
- Code: 200
Content:[ { "Date": "2020-01-01", "Values": [ { "alias": "https://url-shortener.com/<token-1>", "origin": "https://www.google.com.tw?region=zh_tw#/firstpage", "count": 1 } ] }, { "Date": "2020-01-02", "Values": [ { "alias": "https://url-shortener.com/<token-1>", "origin": "https://www.google.com.tw?region=zh_tw#/firstpage", "count": 2 }, { "alias": "https://url-shortener.com/<token-2>", "origin": "https://www.facebook.com.tw", "count": 2 } ] } ]
- Code: 200
MIT