-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5366a1a
Showing
7 changed files
with
629 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
venv/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
from flask import Flask, render_template, request, jsonify, redirect | ||
from pymongo import MongoClient | ||
from dotenv import load_dotenv | ||
import os | ||
import random | ||
import string | ||
|
||
load_dotenv() | ||
|
||
app = Flask(__name__) | ||
client = MongoClient(os.getenv('MONGO_PATH'), int(os.getenv('MONGO_PORT'))) | ||
|
||
# MongoDB Database Access | ||
database = client.ShortUrlDatabase | ||
ShortUrlDatabase = database.URLData | ||
|
||
|
||
# Functions | ||
def generate_random_string(length=5): | ||
''' | ||
This function gives a combination of lowercase and uppercase letters of k length | ||
''' | ||
print('generating a random keyword') | ||
letters = string.ascii_letters | ||
random_string = ''.join(random.choices(letters, k=length)) | ||
print('keyword generated: '+str(random_string)) | ||
return random_string | ||
|
||
|
||
def is_keyword_present(keyword): | ||
''' | ||
Function to check if the current keyword is present in the DB or not | ||
''' | ||
print('Fetching if keyword is present in DB: '+str(keyword)) | ||
present = 0 | ||
for item in ShortUrlDatabase.find(): | ||
if (item['keyword'] == keyword): | ||
print('keyword present in DB: '+str(keyword)) | ||
present = 1 | ||
print('keyword not present in DB: '+str(keyword)) | ||
return present | ||
|
||
# Routes | ||
|
||
|
||
@app.route('/') | ||
def home(): | ||
''' | ||
Index page for URL Shortener (Project OSUS (Open Source URL Shortener)) | ||
''' | ||
print('Home Page API Called, rendering template') | ||
return render_template('index.html') | ||
|
||
|
||
@app.route('/getURL') | ||
def currentURl(): | ||
''' | ||
This function will return the current working URL, something like 'http://127.0.0.1:5000/' | ||
''' | ||
print('getURL API Called') | ||
current_url = request.host_url | ||
print('Current URL: '+str(current_url)) | ||
return f'The app is running on {current_url}' | ||
|
||
|
||
@app.route('/shorten', methods=['POST']) | ||
def shortenAPI(): | ||
''' | ||
This method will shorten the link and give the short URL | ||
''' | ||
# Logic | ||
# This end point should check if that URL is already present in the DB or not | ||
# This end point should take only POST calls and return the shortened version of the URL | ||
# do: Handle Get cases | ||
print('URL Shorten Called') | ||
print('Mongo Connection: '+str(os.getenv('MONGO_PATH')[:6])) | ||
|
||
if request.method == 'POST': | ||
longUrl = request.json.get("longUrl") | ||
keyword = request.json.get("keyword") | ||
|
||
# Logs | ||
print('Long URL Received: ' + str(longUrl)) | ||
print('Keyword Received: '+str(keyword)) | ||
|
||
if keyword == '': | ||
keyword = generate_random_string() | ||
print('New keyword generated:' + str(keyword)) | ||
|
||
|
||
if is_keyword_present(keyword) == 0: | ||
print('Keyword is not present, inserting into DB') | ||
ShortUrlDatabase.insert_one( | ||
{'keyword': keyword, 'url': longUrl, 'clicks': 0}) | ||
print('DB insert successful') | ||
else: | ||
print('Keyword is present, throwing error') | ||
return jsonify({'shortUrl': 'The Keyword Already Exists, Choose a Different One'}) | ||
|
||
if request.method == 'GET': | ||
print('Called get method on shorten end-point, throwing error') | ||
return "GET Method Not Allowed On This End Point" | ||
|
||
return jsonify({'shortUrl': str(request.host_url)+str(keyword)}) | ||
|
||
|
||
@app.route('/analytics', methods=['GET', 'POST']) | ||
def analyticsAPI(): | ||
# No Authentication to check the analytics of the URL as of now | ||
''' | ||
This end point will take URL and will provide the analytics for that URL, since we don\'t have authentication we\'ll be asking just for the keyword and we\'ll give the analytics for the URL as of now ''' | ||
print('Analytics API Called') | ||
return 'Under Development' | ||
|
||
|
||
@app.route('/heartbeat') | ||
def hearBeat(): | ||
''' | ||
This end point should do all the checks/ add dummy data to DB if required and respond saying the website is UP | ||
''' | ||
print('heartbeat API called') | ||
return 'The website is up' | ||
|
||
@app.route('/<keyword>') | ||
def reroute(keyword): | ||
print('Clicked on shortURL') | ||
link_status = 0 | ||
print('Finding the URL Keyword') | ||
print('Mongo Connection: '+str(os.getenv('MONGO_PATH')[:6])) | ||
for item in ShortUrlDatabase.find(): | ||
if (item['keyword'] == keyword): | ||
redirection = item['url'] | ||
# Write Logs Here | ||
print('Short URL <> Long URL mapping found in DB') | ||
link_status = 1 | ||
if (link_status == 0): | ||
print('Link Not Found in DB') | ||
return "Link Not Found" | ||
print('Redirecting to long url: '+str(redirection)) | ||
return redirect(redirection, code=302) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug=True, port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<!-- omit in toc --> | ||
# Contributing to Project OSUS | ||
|
||
Thank you for showing interest in contributing to **Project OSUS**! We appreciate all contributions, and your help is what makes this project grow. 🎉 | ||
|
||
This guide will help you understand how to get involved and contribute in different ways. The project is beginner-friendly, and we encourage everyone to participate, whether it's through code or non-technical contributions. | ||
|
||
<!-- omit in toc --> | ||
## Table of Contents | ||
|
||
- [Your First Contribution](#your-first-contribution) | ||
- [Suggesting Enhancements](#suggesting-enhancements) | ||
- [Reporting Bugs](#reporting-bugs) | ||
- [Improving Documentation and Non-Tech Contributions](#improving-documentation-and-non-tech-contributions) | ||
- [Styleguides](#styleguides) | ||
|
||
## Your First Contribution | ||
|
||
We’re excited to have you contribute! Whether you want to fix bugs, add features, optimize code, or suggest improvements, your contribution is valuable. Here’s how to get started: | ||
|
||
- Pick any existing issue from our [Issues page](https://github.com/harshithtunuguntla/project-osus/issues). Feel free to comment on the issue to let others know you’re working on it. | ||
- If you find something that can be improved (code quality, performance, etc.), you’re free to improve it. | ||
- Want to add a feature? Suggest it and implement it! Your ideas are welcome. | ||
- Don’t be shy if you’re a beginner — even the smallest improvements are helpful. | ||
|
||
You can also improve existing code by submitting optimizations or refactoring parts of the project. We appreciate any contribution, no matter how small! | ||
|
||
## Suggesting Enhancements | ||
|
||
Have an idea to make **Project OSUS** better? Enhancements and new features are a great way to contribute. | ||
|
||
- Browse through the existing [enhancements](https://github.com/harshithtunuguntla/project-osus/issues?q=label%3Aenhancement) to see if someone has already suggested a similar feature. | ||
- If not, open a new [issue](https://github.com/harshithtunuguntla/project-osus/issues/new) with a clear and descriptive title. | ||
- Explain your idea, the current behavior, and what improvement or feature you'd like to see. | ||
- If possible, include a step-by-step explanation, and feel free to include any examples or code snippets. | ||
|
||
Your ideas might inspire new features that benefit many users! | ||
|
||
## Reporting Bugs | ||
|
||
If you encounter any bugs or issues while working with **Project OSUS**, please report them to help us improve the project. Here’s how: | ||
|
||
- Make sure you’re using the latest version of the project. | ||
- Look through [existing issues](https://github.com/harshithtunuguntla/project-osus/issues?q=label%3Abug) to see if the bug has already been reported. | ||
- If the bug hasn’t been reported, open a new [issue](https://github.com/harshithtunuguntla/project-osus/issues/new). | ||
- Include as much detail as possible — platform information (e.g., OS, versions), how to reproduce the issue, and any logs or error messages that help. | ||
|
||
Please provide clear steps to reproduce the issue. This makes it easier for the maintainers to address and fix it! | ||
|
||
## Improving Documentation and Non-Tech Contributions | ||
|
||
Not a developer? No worries — you can still contribute in many other ways: | ||
|
||
- Improving the documentation: If you spot any missing or outdated info in our documentation, feel free to update it. | ||
- Enhancing the README: The README is a key part of helping others understand the project, so your contributions here are valuable. | ||
- Suggesting edits or writing for clarity: Help make our guides clearer for others by improving explanations and instructions. | ||
|
||
If you find any area in need of improvement, feel free to submit a pull request. | ||
|
||
## Styleguides | ||
|
||
- Follow the guidelines for commit messages. Make them clear and descriptive. | ||
- Ensure your code is clean and readable. Simplicity is key. | ||
- Use proper formatting for documentation updates. | ||
|
||
## Thank You | ||
|
||
Your contributions make **Project OSUS** better for everyone! We value and appreciate your efforts, whether you're contributing through code, ideas, or other forms of participation. |
Oops, something went wrong.