created a simple sample

This commit is contained in:
Andreas Stephanides
2020-02-13 18:44:50 +01:00
commit e7deab6430
15 changed files with 255 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
*~
*.pyc
.env
env
*.cfg
*.log
*.sock
*.pid

6
config.cfg.sample Normal file
View File

@@ -0,0 +1,6 @@
pages_root: 'data_sample'
static_root: 'static'
default_template: 'page.html'
url_prefix: "/stuff"
FLATPAGES_EXTENSION: ".md"
FLATPAGES_AUTO_RELOAD: True

0
data/ghg/index.md Normal file
View File

0
data/ghg/test.jpg Normal file
View File

0
data/ghg/test.md Normal file
View File

1
data/index.md Normal file
View File

@@ -0,0 +1 @@
title: title

0
data/tg/index.md Normal file
View File

6
requirements.txt Normal file
View File

@@ -0,0 +1,6 @@
flask
Frozen-Flask
config
uwsgi
gevent

9
run.py Normal file
View File

@@ -0,0 +1,9 @@
from simple_sample import app,freezer
import sys
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "build":
freezer.freeze()
else:
app.run(host='0.0.0.0',port=4444, debug=True)

16
simple_sample.nginx.conf Normal file
View File

@@ -0,0 +1,16 @@
server {
listen 80 default_server;
listen [::]:80 ;
index index.html index.htm index.nginx-debian.html;
server_name _;
root /var/www/html;
location / {
include uwsgi_params;
uwsgi_pass unix:///srv/simple_sample_flat_index/uwsgi.sock;
}
}

13
simple_sample.service Normal file
View File

@@ -0,0 +1,13 @@
[Unit]
Description=uWSGI Simple Sample for Flat Page with Index Default
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/srv/simple_sample_flat_index
Environment="PATH=/srv/simple_sample_flat_index/.env/bin"
ExecStart=uwsgi --ini uwsgi.ini
[Install]
WantedBy=multi-user.target

94
simple_sample/__init__.py Normal file
View File

@@ -0,0 +1,94 @@
"""
This module is based on flask_flatpages and creates a website structure based on a file structure.
By default the .md extension creates a html website
"""
import logging
import sys
from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for
#from flask_flatpages import FlatPages, pygments_style_defs
from flatpages_index import FlatPagesIndex
from flask_frozen import Freezer
from config import Config
import os
import re
from flatpages_index.utils import path_depth, page_to_link, page_to_link, file_to_link
from functools import partial
#from .flatpages import FileLists
# This is the directory, required for absolute file paths
package_directory = os.path.dirname(os.path.abspath(__file__))
# Loading the config file
cfg = Config((os.path.join(package_directory, '../config.cfg')))
# Loading constants from config file
FLATPAGES_AUTO_RELOAD = cfg.get("FLATPAGES_AUTO_RELOAD",True) # Default can be overwritten by config cfg
FLATPAGES_EXTENSION = cfg.get("FLATPAGES_EXTENSION",".md") # Default can be overwritten by config cfg
FLATPAGES_ROOT = os.path.abspath(cfg.pages_root)
# Initialize application
app = Flask(__name__)
app.config.from_object(__name__)
app.logger.setLevel(logging.DEBUG)
# Initialize FlatPages Index
#FlatPagesIndex.FLATPAGES_ROOT=FLATPAGES_ROOT
flatpages = FlatPagesIndex(app)
flatpages.get('index')
app.logger.info('Initialize SimpleSample App')
app.logger.info('flatpages loaded %d pages' % len(flatpages._pages))
app.logger.info("Data directory is: %s" % flatpages.root)
app.logger.info("Url prefix;: %s" % cfg.url_prefix)
app.logger.info("Extensions: %s" % FLATPAGES_EXTENSION)
freezer = Freezer(app)
page_blueprint = Blueprint('intern', __name__)
@page_blueprint.route('/<path:name>/')
@page_blueprint.route('/')
def post(name='index'):
page = flatpages.get(name)
if not page is None:
page.meta["has_img"]=page.meta.get('has_img',True)
page=page.load_linklists(partial(url_for,'intern.post'))
return render_template(page.meta["template"], post=page,
pth=page.meta["dirpath"])
if os.path.exists(os.path.join(FLATPAGES_ROOT,name)):
return send_from_directory(FLATPAGES_ROOT,name)
else:
return send_from_directory('static',name)
@page_blueprint.route('/<path:name>.json',strict_slashes=False)
def postjson(name='index'):
page = flatpages.get(name)
if not page is None:
page.meta["has_img"]=page.meta.get('has_img',True)
page=page.load_linklists(partial(url_for,'intern.post'))
p=page.meta
p["html"]=page.html
return jsonify(page=p)
if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)):
return send_from_directory(FLATPAGES_ROOT,path)
else:
return send_from_directory('static',path)
app.register_blueprint(page_blueprint, url_prefix=cfg.url_prefix,static_folder='static')
app.add_url_rule('%s/<path:name>' % cfg.url_prefix,'page', post)

View File

@@ -0,0 +1,25 @@
{# -*-jinja2-*- #}
<html>
<head></head>
<LINK href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<body>
<div class="container">
<div class="row">
<div class="col-12">
<nav class="breadcrumb" style="background-color: #FFF">
{% for b in post.breadcrumbs %}
<a href="{#url_for('page',name=b.path)#}{{b.url}}" class="breadcrumb-item">{{b.title}} </a>
{% endfor %}
</nav>
{% block content %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,53 @@
{# -*-jinja2-*- #}
{% extends "layout.html" %}
{% block content %}
<h1>{{post.title}}</h1>
{{post.date}}
{% if post.sub_index_pages | length > 0 %}
<hr/>
<b id="up_head"> Unterseiten: </b>
<ul class="nav flex-column flex-sm-row " labeledby="up_head">
{% for d in post.sub_index_pages %}
<li class="nav-item">
<a href="{{d.url}}" class="nav-link">
<h6> {{d.title}} <small class="text-muted">{{d.desc}} </small> </h6>
</a>
</li>
{% endfor %}
</ul>
{% endif %}
{{ post.helloworld() }}
{{ post.html|safe }}
{% if post.sub_pages |length > 0 %}
<ul class="nav flex-column flex-sm-row" labeledby="inf_head">
{% for d in post.sub_pages %}
<li class="nav-item">
<a href="{{d.url}}" class="nav-link text-info">
<h6> {{d.title}} <small class="text-muted">{{d.desc}} </small>
</h6>
</a>
</li>
{% endfor %}
</ul>
{% endif %}
{% if post.list_files |length > 0 %}
<hr/>
<b id="inf_head">Files:</b>
<ul>
{% for d in post.list_files %}
<li>
<a href="{{d.url}}">{{d}} </a>
</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

24
uwsgi.ini Normal file
View File

@@ -0,0 +1,24 @@
[uwsgi]
# User and group www-data on debian
uid = 33
gid = 33
master = true
# No. of processes can be increased
processes = 7
chown-socket =www-data:www-data
virtualenv = /srv/simple_sample_flat_index/.env
pythonpath = /srv/simple_sample_flat_index/
#pidfile = /srv/simple_sample_flat_index/uwsgi.pid
#socket = /srv/simple_sample_flat_index/uwsgi.sock
pidfile=/var/run/simple_sample.pid
socket = /var/run/simple_sample.sock
chmod-socket = 666
module = simple_sample
callable= app
wsgi-file = run.py
logdate = true
#logger = file:/srv/simple_sample_flat_index/uwsgi.log
loglevel = debug
gevent = 100
vacuum=true
plugins=python3,logfile