Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Latest commit

 

History

History
144 lines (103 loc) · 4.18 KB

README.md

File metadata and controls

144 lines (103 loc) · 4.18 KB

ShortURL

A simple Short URL service deploy on deta.sh with FastAPI

This is a simple short url service, you can use it to shorten your long url.

How it works

Create:

graph TB
    A[Input URL]
    B[Check URL integrity]
    C[Query database]
    D[Return]
    E[Inspector makes URL]
    F[Raise HTTP400]
    G[Use UTF-8 to encode]
    H[Use base64 to encode]
    I[Get MD5 hash]
    J[Prefix the url]
    K[Write to database]
    L[Use timestamp as material]
    A --> B
    B --> |coomplete| G --> I --> H --> C
    B --> |incomplete| F
    C --> |no same key| K --> J --> D
    C --> |have same key| E
    E --> |Same as the original URL| D
    E --> |The original URL is different| L --> G
Loading

Inquire:

graph TD
    A[Query database]
    B[Inpur Short URL]
    C[Return and jump to origin URL]
    D[Raise HTTP404]
    F[Count +1]
    B --> A
    A --> |Found| F --> C
    A --> |Not Found| D
Loading

Deploy

step 1: fork this repositorities

step 2: visit https://web.deta.sh and login

step 3: create a new project

step 4: visit https://web.deta.sh/settings and create a new Access Token

step 5: go to your repositorie's setting, add a new secret named DETA_PROJECT_NAME and set the value to the name you created in step 3

step 6: go to your repositorie's setting, add a new secret named DETA_TOKEN and set the value to the token you created in step 4

step 7: go to your repositorie's action page tick init mode then click Run workflow to init micro

step 8: From now on we can GitHub actiion renew .env file, so we have two ways to set the SERVER_PREFIX parameter, you can use your custom domain or use the deta provided domain, find the default domain on https://web.deta.sh micros>ShortURL-main that visiable on the righttop of the page, about hoe to setup custom domain, you can find it on https://docs.deta.sh/docs/micros/custom_domains

  1. go to your repositorie, path to micros/ShortURL-main/main.py and edit the SERVER_PREFIX parameter inline 20, then go to your repositorie's action page tick deploy option and click Run workflow to redeploy
#original
SERVER_PREFIX = os.getenv('SERVER_PREFIX')
#change to
SERVER_PREFIX = "<your server prefix>"
  1. go to your repositorie, path to micros/ShortURL-main/.env and edit the SERVER_PREFIX parameter, then go to your repositorie's action page tick update env option and click Run workflow to redeploy.

⚠️WARN! Never put any undisclosed key in .env on GitHub, E.gPROJECTING_TOKEN

  1. clone repositorities to your local, edit the SERVER_PREFIX parameter in micros/ShortURL-main/.env and use deta cli to update
SERVER_PREFIX=<your server prefix>

⚠️If you run on Deta Micro, you don't need to set PROJECTING_KEY

Local develop

if you want develop on your local, you have some steps to do

step 1: install python3.9 or higher

step 2: install deta cli, you can find it on https://docs.deta.sh/docs/cli/install

step 3: clone repositorie to your local

step 4: cd micros/ShortURL-main

step 5: pip install -r requirements.txt

step 6: deta login to login

step 7: add the SERVER_PREFIX and PROJECTING_KEY parameter to micros/ShortURL-main/.env

SERVER_PREFIX=<your server prefix>
DETA_TOKEN=<your deta token>

step 8: deta update -e .env to update env

step 9: deta deploy to deploy

Visor

Record the content of the request and its success on https://web.deta.sh > micros > visor step 1: go to your repositorie's action page tick visor enable and click Run workflow

step 2: Finish, visor is enable

Usage

POST /create to create a short url

{
    "origin_url": "<your long url>"
}

responseHTTP200

{
    "url": "<your short url>"
}

GET /<short url> to redirect to the long url

responseHTTP307

RedirectResponse(original_url)

Reference

Deploy FastAPI on Deta

Short URL API Server 架設心得與教學

BogDAAAMN/deta-deploy-action