Browse Source

Show project list in webpage

master
Burathar 4 years ago
parent
commit
2a7dea3649
  1. 43
      biscd/biscd/__init__.py
  2. 5
      biscd/biscd/config_default.yaml
  3. 13
      biscd/biscd/errors.py
  4. 0
      biscd/biscd/models.py
  5. 9
      biscd/biscd/routes.py
  6. 12
      biscd/biscd/templates/index.html
  7. 11
      biscd/biscd/utils.py
  8. 12
      biscd/config_example.yaml
  9. 13
      biscd/example.yaml
  10. 7
      biscd/projects_example.yaml
  11. 2
      biscd/run.sh

43
biscd/biscd/__init__.py

@ -1,23 +1,48 @@
from os import environ from os import path, mkdir
import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path from pathlib import Path
import confuse import confuse
from flask import Flask from flask import Flask
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
def read_config(): def read_config():
conf = confuse.Configuration('biscd', __name__) conf = confuse.Configuration('biscd', __name__)
try: try:
conf['config'].get() conf['logging'].get()
except confuse.NotFoundError: except confuse.NotFoundError as not_found:
raise ValueError("Failed to open any configfile containing a 'config' object. " \ raise ValueError("Failed to open any configfile containing a 'logging' object. " \
"Please make sure the module directory contains a config_default.yaml, " \ "Please make sure the module directory contains a config_default.yaml, " \
f"or /etc/{__name__}/config.yaml exists") f"or /etc/{__name__}/config.yaml exists") from not_found
return conf
return conf['config'] def setup_logging():
# pylint: disable=no-member
if config['logging']['log_to_sdout'].get(bool):
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
app.logger.addHandler(stream_handler)
else:
logfile = Path(config['logging']['logfile'].as_filename())
if not logfile.parent.exists():
logfile.parent.mkdir(mode=0o770, parents=True)
file_handler = RotatingFileHandler(str(logfile), maxBytes=10240, backupCount=10)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'))
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('Biscd startup')
app = Flask(__name__)
config = read_config() config = read_config()
bootstrap = Bootstrap(app)
setup_logging()
from biscd import routes, errors from biscd import routes, errors, models

5
biscd/biscd/config_default.yaml

@ -1,4 +1,3 @@
confiag:
flask_development_server: flask_development_server:
# Options: production, development # Options: production, development
flask_env: development flask_env: development
@ -7,7 +6,7 @@ confiag:
port: 5000 port: 5000
logging: logging:
infofile: info.log log_to_sdout: true
errorfile: error.log logfile: info.log
message: Hoi message: Hoi

13
biscd/biscd/errors.py

@ -1,5 +1,5 @@
from flask import render_template, request from flask import render_template
from biscd import app, config, utils from biscd import app, utils
@app.errorhandler(400) @app.errorhandler(400)
def bad_request(error): def bad_request(error):
@ -15,10 +15,7 @@ def internal_error(error):
return render_template('500.html', title='500'), 500 return render_template('500.html', title='500'), 500
def write_error(error): def write_error(error):
timestamp = utils.get_timestamp() # pylint: disable=no-member
remote = utils.get_remote_addr() remote = utils.get_remote_addr()
logline = f"{remote} {timestamp} {str(error)} {error.__dict__} \n" logline = f"{remote} {str(error)} {error.__dict__}"
app.logger.error(logline)
logfile = config.get('LOGGING', "errorfile")
with open(logfile, 'a') as logfile:
logfile.write(logline)

0
biscd/biscd/models.py

9
biscd/biscd/routes.py

@ -1,8 +1,9 @@
from flask import render_template, abort from flask import render_template
from biscd import app, config, utils from biscd import app
from biscd.models.project import Project
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
@app.route('/index', methods=['GET']) @app.route('/index', methods=['GET'])
def index(): def index():
return render_template('index.html', message=config['message'].get()) project_names = Project.list()
return render_template('index.html', projects=project_names)

12
biscd/biscd/templates/index.html

@ -2,5 +2,15 @@
{% block app_content %} {% block app_content %}
<h1>Biscd, what else would you like with your Gitea?</h1> <h1>Biscd, what else would you like with your Gitea?</h1>
<h2>{{ message }}</h2> <div class="table-responsive">
<table class="table">
<tbody>
{% for project in projects %}
<tr>
<td>{{ project }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %} {% endblock %}

11
biscd/biscd/utils.py

@ -0,0 +1,11 @@
from datetime import datetime
from flask import request
def get_timestamp():
return datetime.now().astimezone().isoformat(timespec='milliseconds')
def get_remote_addr():
#print(f"HTTP_X_REAL_IP: {request.environ.get('HTTP_X_REAL_IP')}")
#print(f"HTTP_X_FORWARDED_FOR: {request.environ.get('HTTP_X_FORWARDED_FOR')}")
return request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr)

12
biscd/config_example.yaml

@ -0,0 +1,12 @@
flask_development_server:
# Options: production, development
flask_env: production
# Options: 127.0.0.1 , 0.0.0.0
server_host: 127.0.0.1
port: 5000
logging:
log_to_sdout: true
logfile: info.log
message: Hoi

13
biscd/example.yaml

@ -1,13 +0,0 @@
config:
flask_development_server:
# Options: production, development
flask_env: production
# Options: 127.0.0.1 , 0.0.0.0
server_host: 127.0.0.1
port: 5000
logging:
infofile: info.log
errorfile: error.log
message: Hoi

7
biscd/projects.yaml → biscd/projects_example.yaml

@ -7,3 +7,10 @@ projects:
requirements-file: requirements.txt requirements-file: requirements.txt
tests: tests.py tests: tests.py
- Foo:
url: bar
git_repo: https://git.sciuro.org/Burathar/The-Hunt
branch: master
secret: thisissecret
requirements-file: requirements.txt
tests: tests.py

2
biscd/run.sh

@ -5,7 +5,7 @@ app_name='biscd'
source "$script_dir/venv/bin/activate" source "$script_dir/venv/bin/activate"
get_conf () { get_conf () {
echo `grep -s "$1" /etc/$app_name.yml "$script_dir/biscd/config_default.yaml" | cut -d ':' -f 3 | head -n 1` echo `grep -s "$1" /etc/$app_name/config.yaml "$script_dir/biscd/config_default.yaml" | cut -d ':' -f 3 | head -n 1`
} }
enviroment=`get_conf 'flask_env'` enviroment=`get_conf 'flask_env'`

Loading…
Cancel
Save