URL shortener is a simple app built using Fission open-source serverless framework using MongoDB as the database. The application is built using Python.
This is a URL shotener application built using Python and Flask using Fission serverless framework. The app makes use of pyshorteners library for shortening of URLs.
- The user enters the url to be shortened from a web form.
- This URL is sent to a fission function
shortenurl
which first checks in Mongo DB whether the long URL has already been shortened or not. - If it is already shortened, the function returns the URL from Mongo DB.
- It the URL is not shortened, the function creates a new shortURL, adds it to MongoDB and returns the short URL.
This repo contains the following files and folders:
- Backend: backend code with
shortenurl.py
. Also containsbuild.sh
which is required to create a Fission package. - Frontend: frontend code with
app.py
. Also containsbuild.sh
which is required to create a Fission package. - Package.sh: bash script to create zip files required for creating Fission package.
- Configure a Kubernetes cluster on any environment - minikube, microk8s, AKS, GKE, EKS etc.
- Install Fission on the cluster and configure the Fission CLI.
- Create a Mongo DB account and configure a cluster. You can follow Mongo DB documentation to create a new cluster.
- Configure the connection to Mongo DB for your application to get a connection string. Refer to Mongo DB documentation to get connection string for your application.
Clone this repo and perform the following steps.
Note: Make sure to update the MongoDB connection string in
shortenurl.py
with your MongoDB crednetials.
Create a Python environment
fission environment create --name python --image fission/python-env --builder fission/python-builder:latest
Create zip archives for backend and frontend by executing package.sh script
./package.sh
Note: Make sure that the build.sh file is executable. You can do so by running
chmod +x build.sh
Create Fission Packages
fission package create --name frontend-pkg --sourcearchive frontend.zip --env python --buildcmd "./build.sh"
fission package create --name backend-pkg --sourcearchive backend.zip --env python --buildcmd "./build.sh"
Create Fission Functions
fission fn create --name frontend --pkg frontend-pkg --entrypoint "app.main"
fission fn create --name backend --pkg backend-pkg --entrypoint "shortenurl.main"
Create Routes for Fission functions
fission route create --name frontend --method POST --method GET --url /main --function frontend
fission route create --name backend --method POST --url /shorturl --function backend
With this our custom Python environment is ready along with our Fission function. You can run kubectl port-forward svc/router 8888:80 -nfission
to access your app from your browser. Visit http://localhost:8888/main
to access the application.