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.
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
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
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
- go to your repositorie, path to
micros/ShortURL-main/main.py
and edit theSERVER_PREFIX
parameter inline 20, then go to your repositorie's action page tickdeploy
option and clickRun workflow
to redeploy
#original
SERVER_PREFIX = os.getenv('SERVER_PREFIX')
#change to
SERVER_PREFIX = "<your server prefix>"
- go to your repositorie, path to
micros/ShortURL-main/.env
and edit theSERVER_PREFIX
parameter, then go to your repositorie's action page tickupdate env
option and clickRun workflow
to redeploy.
.env
on GitHub, E.gPROJECTING_TOKEN
- clone repositorities to your local, edit the
SERVER_PREFIX
parameter inmicros/ShortURL-main/.env
and use deta cli to update
SERVER_PREFIX=<your server prefix>
PROJECTING_KEY
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
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
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)