Short Flask Guidance
sudo apt update
sudo apt full-upgrade -y
sudo apt install python3 idle3
mkdir myproject
cd myproject
python3 -m venv venv
source venv/bin/activate
pip install flask
sudo nano myproject.py
Content:
from app import app
strg+s and strg+x
mkdir app
cd app
sudo nano __init__.py
Content:show
from flask import Flask
app = Flask(__name__)
from app import routes
strg+s and strg+x
sudo nano routes.py
Content:show
from flask import render_template, flash, redirect, url_for, send_file, request
from app import app
@app.route('/')
@app.route('/index')
@app.route('/index.html')
def index():
return render_template('index.html')
##start
@app.route('/start.html')
def start():
return render_template('start.html')
strg+s and strg+x
echo "from app import app" > sudo nano myproject.py
sudo reboot
cd myproject
source venv/bin/activate
pip install flask-wtf
pip install flask-login
pip install email-validator
cd app
mkdir templates
cd templates
sudo nano index.html
Content:show
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="keywords" content="TS3,ipfire,roll20,raspberry pi">
<meta name="author" content="me">
<link rel="shortcut icon" type="image/x-icon" href="/static/favicon.jpg">
<title>Shadow page</title>
</head>
<body>
<div style="height: 48px;"> </div>
<p align="center" style="margin-bottom: 1px;">
<a href="https://jordancosmetics.ydns.eu/jc/index.html"><img src="static/JC.jpg" /></a>
</p>
<p align="center" style="margin-top: 1px;">
<a href="start.html"><img src="static/shadow.jpg" /></a>
</p>
<div style="text-align:right"><br><a href="impressum.html">Impressum, Datenschutz</a></div>
Diese Webseite nutzt nur technisch notwendige Cookies!<br>
Technisch notwendige Cookies: Zur notwendigen Datenspeicherung gehören Cookies, die für die Funktionen einer Website$
</body>
</html>
strg+s and strg+x
sudo nano base.html
Content:show
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="keywords" content="TS3,ipfire,roll20,raspberry pi">
<meta name="author" content="me">
<link rel="shortcut icon" type="image/x-icon" href="/static/favicon.jpg">
<title>Shadow page</title>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto;
}
.grid-item {
border: 0px;
padding: 0px;
}
li {
margin: 10px 0;
}
pre {
background: #f0f0f0;
padding: 10px;
}
a.external {
background: url(/static/external3.png) center right no-repeat;
padding-right: 14px;
}
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 8px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">
<h2 style="margin: 0px;">shadowpage.ydns.eu
</div>
<div class="grid-item">
<div style="text-align:right">
<a href="/start.html">start
<a href="/login.html">Login
</div>
</div>
</div>
<div class="navbar">
<div class="dropdown">
<button class="dropbtn">IPFire
<i class="fa fa-caret-down">
</button>
<div class="dropdown-content">
<a href="/ipfire/ipfire.html">Install <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/ipfire_setup.html">Setup <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/IPS.html">IPS <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/DHCP.html">DHCP fixed leases <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/hardening.html">Hardening <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/Roadwarrior.html">OpenVPN Roadwarrior <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/lynis.html">Lynis <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/BackupRestore.html">BackupRestore <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/ClamAntiVirus.html">ClamAntiVirus <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
<a href="/ipfire/Tor.html">Tor <img src="/static/done.png" align="top" vspace="2" title="using" height="14">
</div>
</div>
</div>
{ % block content % }{ % endblock % }
<div style="text-align:right"><br><a href="/impressum.html">Impressum, Datenschutz</a></div>
</body>
</html>
strg+s and strg+x
sudo nano start.html
Content:show
{ % extends "base.html" % }
{ % block content % }
<style>
a.uc {
background: url(/static/underc.png) center right no-repeat;
padding-right: 84px;
}
td {
vertical-align: top;
}
</style>
<ul>
<li>
<h2><a href="https://gisanddata.maps.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6" target="_blank" class="external">covid</a></h2>
</li>
<li>
<h2>IPFire</h2>
<a href="ipfire/ipfire.html">IPFire Install</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/ipfire_setup.html">IPFire Setup</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/IPS.html">IPS</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/DHCP.html">DHCP fixed leases</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/hardening.html">Hardening</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/Roadwarrior.html">OpenVPN Roadwarrior</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/lynis.html">Auditing-tool lynis</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/BackupRestore.html">Backup and Restore</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="ipfire/ClamAntiVirus.html">ClamAntiVirus (ClamAV)</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15"><br>
<a href="https://wiki.ipfire.org/addons/guardian" target="_blank" class="external">The Guardian 2.0</a><br>
<a href="ipfire/Tor.html">Tor</a> <img src="static/done.png" align="top" vspace="2" title="using" height="15">
</li>
</ul>
{ % endblock % }
strg+s and strg+x
cd ..
mkdir static
copy favicon.jpg and JC.jpg and shadow.jpg
Test with
cd ~/myproject/
flask run
Browser:
http://127.0.0.1:5000/
Next:
cd ~/myproject/
pip install gunicorn
sudo nano .env
Content:show
SECRET_KEY=52cb883e323b48d78a0a36e8e951ba4a
MAIL_SERVER=localhost
MAIL_PORT=25
DATABASE_URL=mysql+pymysql://myproject:@localhost:3306/myproject
MS_TRANSLATOR_KEY=c2c9b294f57d49028fe03c2307f8cc4c
strg+s and strg+x
deactivate
sudo apt-get -y install supervisor
sudo nano /etc/supervisor/conf.d/myproject.conf
Content:show
[program:myproject]
command=/home/pi/myproject/venv/bin/gunicorn -b 0.0.0.0:8000 -w 4 myproject:app
directory=/home/pi/myproject
user=pi
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
strg+s and strg+x
sudo supervisorctl reload
Test with
Browser:
http://127.0.0.1:8000/
Next:
sudo apt-get -y install nginx
Test with
Browser:
http://192.168.6.10
sudo nano /etc/nginx/sites-enabled/default
Content:show
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm;
server_name _;
location / {
proxy_pass http://localhost:8000;
}
}
strg+s and strg+x
Test with
Browser:
192.168.6.10
cd myproject
source venv/bin/activate
pip install flask-sqlalchemy
pip install flask-migrate
sudo nano config.py
Content:show
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
strg+s and strg+x
cd app
sudo nano __init__.py
Content:show
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = '/login.html'
from app import routes, models
strg+s and strg+x
sudo nano models.py
Content:show
from datetime import datetime
from app import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from app import login
@login.user_loader
def load_user(id):
return User.query.get(int(id))
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
active = db.Column(db.Integer, default=0)
role = db.Column(db.Integer, default=0)
def __repr__(self):
return ''.format(self.username)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class Roles(db.Model):
id = db.Column(db.Integer, primary_key=True)
role = db.Column(db.String(140))
def __repr__(self):
return ''.format(self.role)
class Books(db.Model):
id = db.Column(db.Integer, primary_key=True)
bookname = db.Column(db.String(140), index=True, unique=True)
x_coordinate = db.Column(db.Integer, default=0)
y_coordinate = db.Column(db.Integer, default=0)
def __repr__(self):
return ''.format(self.bookname)
class Claims(db.Model):
id = db.Column(db.Integer, primary_key=True)
housenumber = db.Column(db.Integer, default=0)
estate = db.Column(db.Integer, default=0)
living_space = db.Column(db.Integer, default=0)
cellar = db.Column(db.Integer, default=0)
parcing_space = db.Column(db.Integer, default=0)
car_port = db.Column(db.Integer, default=0)
garage = db.Column(db.Integer, default=0)
price = db.Column(db.Numeric)
for_sale = db.Column(db.Integer, default=0)
text = db.Column(db.String(255))
contact = db.Column(db.String(255))
def __repr__(self):
return ''.format(self.housenumber)
strg+s and strg+x
cd ..
flask db init
flask db migrate -m "User table"
flask db migrate -m "Roles table"
flask db upgrade
Browser:
192.168.6.10/register.html
python3
from app import app, db
from app.models import User, Roles
import sqlalchemy as sa
app.app_context().push()
r = Roles(role='User')
db.session.add(r)
db.session.commit()
r = Roles(role='Admin')
db.session.add(r)
db.session.commit()
u = db.session.get(User, 1)
u.active=1
u.role=2
db.session.commit()