Table of contents
This article outlines the steps and choice of programming language in the development of our cloud-native web app. In this capstone project, we would use Flask (Python) web framework to build our application backend.
Web frameworks are software libraries that provide a structured way to build and develop web applications. Their set of pre-defined functions, templates, and tools simplify the process of creating dynamic and interactive websites or web applications.
Phase 2 - Writing the application code using python-flask
Create a VENV: (optional but great for multiple projects)
Three simple steps to set up venv on your machine
//Install venv to host python using the cmd $ pip install virtualenv //Create a project folder in your terminal $ python<version> -m venv <virtual-environment-name> //Activate your venv for windows with the cmd $ <virtual-env-name>/Scripts/Activate.ps1 //Activete your venv for mac with the cmd $ source <virtual-env-name>/bin/activate
Install Flask using the command:
$ pip install Flask //Check to see all dependecies installed alongside flask $ pip list Package Version ------------ ------- blinker 1.6.2 click 8.1.7 colorama 0.4.6 Flask 2.3.3 itsdangerous 2.1.2 Jinja2 3.1.2 MarkupSafe 2.1.3 pip 22.3.1 psutil 5.9.5 setuptools 65.5.0 Werkzeug 2.3.7
```bash from flask import Flask, jsonify, render_template import socket, psutil
app = Flask(name)
@app.route("/") def hello_world(): return "
Hello, I am Nelson Osagie, a Cloud / DevOps Engineer.
"#Function that get system cpu and memory usage def app_health(): cpu_percent = psutil.cpu_percent() mem_percent = psutil.virtual_memory().percent message = None if cpu_percent > 85 or mem_percent > 70: message = "High CPU and Memory utilization, scale up" return str(cpu_percent), (mem_percent)
@app.route("/details") def details(): cpu_percent, mem_percent = app_health() hostname, ip_address = get_host_details() return render_template('index.html', HOSTNAME=hostname, IP=ip_address, CPU=cpu_percent, MEM=mem_percent)
if name == 'main': app.run(host="localhost", port=5000, debug=True)
index.html
```xml
<html>
<head>
<title>
A cloud-native-monitoring-web-app
</title>
</head>
<body>
<h2> Building and deploying a simple monitoring app, using python flask web framework</h2>
<h2> Details of Host machine: {{ HOSTNAME }} and IP address {{ IP }}</h2>
<h3> Details of CPU and Memory Utilization:</h3>
<h1> CPU Usage {{ CPU }} and Memory Usage: {{ MEM }}</h1>
</body>
</html>
You are free to build any sort of dynamics to the project, check the repo for updates, and feel free to make contributions: https://github.com/Nelgit007/cloud-native-monitoring-web-app
Use pip to freeze all dependecise in your venv:
$ pip freeze > requirements.txt