In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to create a short URL service in Serverless, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The technology used
Products and services used:
Serverless Framework: a free and open source Serverless framework.
Tencent SCF: Tencent Cloud function service.
Lambda Store: the first Serverless Redis service in the world.
Language and framework:
Python 3.6
Flask: a miniature Web framework developed by Python.
Project initialization
Install the Serverless command line tool globally through npm:
Npm install-g serverless
Initialize the project using a template:
Serverless init flask-starter-name url-shortener
Service design and implementation
This simple short URL service mainly has the following interfaces, but there is no front-end page at present:
1. Convert a long URL to a short URL
2. When visiting a short URL, redirect it to the original long URL
3. Restore the short URL to the original long URL.
The data is currently stored in redis, where Lambda Store services are used.
When a short URL is generated, a 6-digit random identifier is generated (if the identifier already exists, it will be rebuilt, up to 20 attempts, and if it still fails, an error message will be returned), and then it will be stored in redis with key as the short URL identifier and value as the original long URL.
When visiting a short URL, first get the corresponding original long URL from redis with the identifier key, and if it is successful, perform a redirect operation, otherwise return 404.
When restoring a short URL, the corresponding original long URL is also obtained from redis with the identifier key. If it is successful, the corresponding original long URL is returned, otherwise an error message is returned.
The code has been uploaded to GitHub: https://github.com/donhui/url-shortener. The core code is as follows:
Import randomimport stringfrom flask import Flask, jsonify, request, abort, redirect, render_templateimport redisfrom settings import redis_settingsapp = Flask (_ _ name__) def generate_identifier (identifier 6): identifier = "" for i in range (n): identifier + = random.choice (string.ascii_letters) return identifierdef get_redis_instance (): return redis.Redis (host=redis_settings.get ('host'), port=redis_settings.get (' port')) Password=redis_settings.get ('password') redis_instance = get_redis_instance () @ app.route ("/") def index (): return render_template ("index.html") @ app.route ("/ generate//") def generate (address): identifier =' I = 0 while True: ifi = 20: break identifier = generate_identifier () if redis_instance Return jsonify (identifier): I = I + 1 continue else: break if identifier ='': return jsonify ({"status": "fail") "error_msg": "generate shortened_url fail, please try again."}) if not ("http://") or address.startswith (" https://")): address = "http://" + address redis_instance.set (identifier, address) shortened_url = request.host_url + identifier return jsonify ({" identifier ": identifier) "shortened_url": shortened_url}) @ app.route ("/ /") def fetch_original (identifier): try: origin_url = redis_instance.get (identifier) except: abort return redirect (origin_url) @ app.route ("/ restore//") def restore (identifier): try: original_url = redis_instance.get (identifier) except: pass if Original_url is None: return jsonify ({"status": "fail" "error_msg": "get original_url fail, please check the identifier."}) return jsonify ({"identifier": identifier, "original_url": str (original_url)}) if _ _ name__ = = "_ main__": app.run (debug=True)
After the development is completed, the screenshot of the code directory structure is as follows:
Deploy to Tencent Cloud SCF
Use the serverless deploy command to quickly deploy the service to Tencent Cloud SCF.
Visit services related to short web sites
Short URL, as the name implies, URL is relatively short, generally there will be a short domain name.
In theory, SCF supports custom domain names, preferably a short domain name.
The following Demo uses the API gateway address that comes with Tencent Cloud for demonstration purposes.
First go to the home page:
Generate a short URL:
After generating this short URL, visit it using a browser, and it will jump to the original URL.
Restore the short URL:
The above is how to create a short URL service in Serverless. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.