欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Serverless中怎么創(chuàng)建一個短網(wǎng)址服務

本篇文章為大家展示了Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設計、網(wǎng)站制作與策劃設計,長興網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:長興等地區(qū)。長興做網(wǎng)站價格咨詢:18980820575

使用的技術

使用的產(chǎn)品與服務:

  • Serverless Framework:一個免費開源的 Serverless 框架。

  • Tencent SCF:騰訊云云函數(shù)服務。

  • Lambda Store:全球第一個 Serverless redis 服務。

語言及框架:

  • Python 3.6

  • Flask:一個微型的 Python 開發(fā)的 Web 框架。

項目初始化

通過 npm 全局安裝 Serverless 命令行工具:

npm install -g serverless

使用模板初始化項目:

serverless init flask-starter --name url-shortener

業(yè)務設計與實現(xiàn)

這個簡單的短網(wǎng)址服務,主要有以下幾個接口,目前沒有前端頁面:

1、將長網(wǎng)址轉換為短網(wǎng)址

2、訪問短網(wǎng)址時將其重定向到原始的長網(wǎng)址

3、將短網(wǎng)址還原為原始的長網(wǎng)址

數(shù)據(jù)目前存儲到 redis 中,這里用到了 Lambda Store 服務。

在生成短網(wǎng)址時,會生成一個6位的隨機標識符(如果標識符已存在,會重新生成,最多嘗試20次,如果還是失敗,則返回錯誤信息),然后以 key 為短網(wǎng)址標識符,以 value 為原始的長網(wǎng)址,將其存儲到 redis 中。

訪問短網(wǎng)址時,首先以標識符為 key 從 redis 獲取相應的 原始的長網(wǎng)址,如果獲取成功,執(zhí)行重定向操作,否則返回404。

還原短網(wǎng)址時,也是以標識符為 key 從 redis 獲取相應的 原始的長網(wǎng)址,如果獲取成功,則返回相應的原始長網(wǎng)址,否則返回錯誤信息。

已將代碼上傳到 GitHub: https://github.com/donhui/url-shortener, 核心代碼如下:

import random
import string
from flask import Flask, jsonify, request, abort, redirect, render_template
import redis
from settings import redis_settings
app = Flask(__name__)


def generate_identifier(n=6):
    identifier = ""
    for i in range(n):
        identifier += random.choice(string.ascii_letters)
    return identifier


def 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/<path:address>/")
def generate(address):
    identifier = ''
    i = 0
    while True:
        if i == 20:
            break
        identifier = generate_identifier()
        if redis_instance.exists(identifier):
            i = i + 1
            continue
        else:
            break

    if identifier == '':
        return jsonify({"status": "fail", "error_msg": "generate shortened_url fail, please try again."})

    if not (address.startswith("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("/<string:identifier>/")
def fetch_original(identifier):
    try:
        origin_url = redis_instance.get(identifier)
    except:
        abort(404)
    return redirect(origin_url)


@app.route("/restore/<string:identifier>/")
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)

開發(fā)完成后,代碼目錄結構截圖如下:

Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務

部署到騰訊云 SCF

使用 serverless deploy 命令即可一鍵快速將服務部署到騰訊云 SCF。

Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務

訪問短網(wǎng)址相關服務

短網(wǎng)址顧名思義網(wǎng)址比較短,一般都會有一個短的域名。

理論上 SCF 支持自定義域名,當然最好是個短域名。

下面的  Demo 出于演示的目的,權且使用騰訊云自帶的 API 網(wǎng)關地址。

首先進入首頁:

Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務

生成一個短網(wǎng)址:

Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務

生成這個短網(wǎng)址后,使用瀏覽器訪問它,它會跳轉到原始的網(wǎng)址。

還原短網(wǎng)址:

Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務

上述內(nèi)容就是Serverless 中怎么創(chuàng)建一個短網(wǎng)址服務,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章標題:Serverless中怎么創(chuàng)建一個短網(wǎng)址服務
新聞來源:http://www.chinadenli.net/article38/iphspp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站App開發(fā)建站公司域名注冊關鍵詞優(yōu)化手機網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化