Skip to content

Commit c87e317

Browse files
Refactor application to use SQLite for link storage, implement rate limiting, and enhance error handling; update README and add CI workflow
1 parent 4c53319 commit c87e317

13 files changed

Lines changed: 545 additions & 115 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up Python
14+
uses: actions/setup-python@v5
15+
with:
16+
python-version: "3.11"
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install -r requirements.txt
21+
pip install -r requirements-dev.txt
22+
- name: Run tests
23+
run: pytest -q

‎.gitignore‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.venv/
2+
__pycache__/
3+
*.pyc
4+
.pytest_cache/
5+
.env
6+
.env.*
7+
.vscode/
8+
*.sqlite
9+
*.db
10+
data/*.db
11+
data/*.sqlite
12+
data/*.json

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Sagar Biswas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ This project lets you turn a long link into a short link.
88

99
Example:
1010

11-
- Long : https://youtu.be/oUp4SWSrYYk?si=Z7r-93Y6WbqYXrns
12-
- Short (yours) : https://sharplink.onrender.com/MultiHAT
13-
- mirror (TinyURL) : https://tinyurl.com/2d89to88
11+
- Long : https://youtu.be/oUp4SWSrYYk?si=Z7r-93Y6WbqYXrns
12+
- Short (yours) : https://sharplink.onrender.com/MultiHAT
13+
- mirror (TinyURL) : https://tinyurl.com/2d89to88
1414

1515
When someone opens the short link, the website sends them to the long link.
1616

@@ -67,8 +67,6 @@ python -m venv .venv
6767

6868
### Step 2: Install the project packages
6969

70-
Remove gunicorn==22.0.0 from requirements.txt since it was used to host the web app on Render. Gunicorn==22.0.0 is not needed for local use.
71-
7270
Run:
7371

7472
```powershell
@@ -102,9 +100,19 @@ http://localhost:5000
102100

103101
## Where the links are saved
104102

105-
Your short links are saved in a small file on your computer:
103+
Your short links are saved in a small SQLite database file on your computer:
104+
105+
- `data/url_store.db`
106+
107+
If a legacy `data/url_store.json` exists, the app will migrate it on first run.
108+
109+
You can override the database location with:
110+
111+
```powershell
112+
$env:SHARPLINK_DB_PATH="C:\path\to\url_store.db"
113+
```
106114

107-
- `data/url_store.json`
115+
You can also use a `.env` file. See [.env.example](.env.example).
108116

109117
This file remembers:
110118

@@ -128,7 +136,6 @@ To make links work for everyone on the internet, you must deploy this app on a p
128136
You can also call the backend directly:
129137

130138
- `POST /api/shorten`
131-
132139
- JSON body example:
133140
```json
134141
{
@@ -147,10 +154,38 @@ You can also call the backend directly:
147154

148155
- Health check: http://localhost:5000/health
149156

150-
## Credits
157+
## Testing
151158

152-
Developed by Sagar Biswas: https://github.com/SagarBiswas-MultiHAT
159+
Install dev dependencies:
153160

161+
```powershell
162+
pip install -r requirements-dev.txt
163+
```
154164

165+
Run tests:
155166

167+
```powershell
168+
pytest
169+
```
170+
171+
## Security notes
172+
173+
- Only `http://` and `https://` URLs are accepted.
174+
- Aliases are length-limited and restricted to letters, numbers, `-`, and `_`.
175+
- API requests are rate-limited by IP.
176+
- If you deploy publicly, add abuse controls and monitoring.
177+
178+
## Rate limiting (production)
179+
180+
The default limiter uses in-memory storage, which is fine for local testing but not for production.
181+
Use Redis in production by setting:
182+
183+
```powershell
184+
$env:RATELIMIT_STORAGE_URI="redis://localhost:6379/0"
185+
```
186+
187+
Or add the same to your `.env` file. See [.env.example](.env.example).
156188

189+
## Credits
190+
191+
Developed by Sagar Biswas: https://github.com/SagarBiswas-MultiHAT

0 commit comments

Comments
 (0)