-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
83 lines (65 loc) · 2.31 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##########################################################
## APS Database Server ##
## ##
## Authors: [email protected], ##
## Date: November 3, 2022 ##
## Version: 1 ##
##########################################################
#python -m flask --debug --app server run (--host=0.0.0.0)
import os
from pathlib import Path
from configobj import ConfigObj
from flask import Flask
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from flask import render_template, redirect, send_from_directory
from flaskext.markdown import Markdown
from data_scraping import DataScraping
#load configuration file
conf = ConfigObj('config.ini')
#get DataScraping class object
ds = DataScraping(conf)
#simple class to hold all configuration parameters for the flask server
class Config():
EXPLAIN_TEMPLATE_LOADING = True
SECRET_KEY = os.environ.get('SECRET_KEY') or 'vRbPDgZP6rHpjCSQWByy'
app = Flask(__name__)
Markdown(app)
app.config.from_object(Config)
#class for defining search form
class SearchForm(FlaskForm):
search_terms = StringField("search_terms")
submit = SubmitField("Submit")
#define the URLs routes for the Website
@app.route("/",methods =['POST','GET'])
def index():
form = SearchForm()
if form.validate_on_submit():
sstring = form.search_terms.data
if sstring == '':
dirs = ds.search_results(sterms=[])
dlink = f"rsync -a --progress <your_username>@{conf['Server']['url']}:/{conf['Data']['data_dir']} ."
else:
sterms = sstring.split(',')
sterms = [s.strip() for s in sterms]
#print(sterms)
dirs = ds.search_results(sterms=sterms)
#generate download link:
dlink = f"rsync -a --progress <your_username>@{conf['Server']['url']}"
for d in dirs:
dlink += f":{d} "
dlink += " ."
else:
dirs = ds.search_results(sterms=[])
dlink = f"rsync -a --progress <your_username>@{conf['Server']['url']}:/{conf['Data']['data_dir']} ."
return render_template("index.html", form=form, dirs=dirs, dlink=dlink)
#return render_template("index.html", form=form)
@app.route("/data/www/<path:path>")
def send_file(path):
return send_from_directory("/data/www", path)
@app.route("/readme")
def readme():
md_text = Path('Readme.md').read_text()
return render_template("readme.html", md_text=md_text)
if __name__ == "__main__":
app.run(debug=False)