diff --git a/engine/webserverflask.py b/engine/webserverflask.py index 14a6c08..a8c5e26 100644 --- a/engine/webserverflask.py +++ b/engine/webserverflask.py @@ -1,6 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -"""Webserver""" +"""Webserver + +# get ip of visitors https://stackoverflow.com/questions/3759981/get-ip-address-of-visitors +""" import os import sys @@ -14,13 +17,28 @@ import subprocess import re +import argparse + +from flask import request +from flask import jsonify + from engine.searcher import search_term, Db, Header +# Open Access mode +try: + from engine.open_access import OPEN_ACCESS # inside this file OPEN_ACCESS = ['work-fenzymes.html'] +except ImportError: + OPEN_ACCESS = [] + + app = Flask(__name__, static_url_path='') @app.route('/edit/') def edit(note_title): """Open a note with your edit""" + if request.remote_addr not in ['127.0.0.1', '0.0.0.0']: + return 'Hmm...' + os.system('open ' + PATH_TO_MD + ' ' + note_title) return 'edit note: %s' % note_title @@ -37,17 +55,20 @@ def edit_header(note_title, note_header): Open a note with your edit http://stackoverflow.com/questions/3139970/open-a-file-at-line-with-filenameline-syntax - + ..warning: if two headers found there will be a problem ;-) """ #grep -n 'May #2' *.md + if request.remote_addr not in ['127.0.0.1', '0.0.0.0']: + return 'Hmm...' + cmd = "cd " + PATH_TO_MD + " && /usr/bin/grep -n '" + note_header + "' " + note_title + ".md" print 'edit_header::cmd:', cmd out = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read() print 'edit_header::out:', out note_line = out.split(':')[0] os.system('emacsclient +' + note_line + ' ' + PATH_TO_MD + '/' + note_title + '.md &') - + return redirect('/view/' + note_title + '.md#' + note_header.lstrip('#').strip().replace(' ', '-')) @app.route('/js/') @@ -69,6 +90,11 @@ def send_img(path): @app.route('/view/') def view(note_title): """Open a note with your edit""" + print(note_title) + if request.remote_addr not in ['127.0.0.1', '0.0.0.0']: + if note_title not in OPEN_ACCESS: + return 'Hmm...' + head = open(PATH_TO_TEMPLATE_HTML).read() head = head.replace('{{ url_index }}', PATH_TO_HTML + '/' + 'index.html') head = head.replace('href="img/', 'href="' + '/img/') @@ -80,9 +106,12 @@ def view(note_title): html = open(PATH_TO_HTML + os.sep + note_title.replace('.md', '.html')).read() #return head + html return html - + @app.route('/search/') def search(text): + if request.remote_addr not in ['127.0.0.1', '0.0.0.0']: + return 'Hmm...' + results = search_term(text) head = open(PATH_TO_TEMPLATE_HTML).read() @@ -93,16 +122,35 @@ def search(text): head = head.replace('="js/', '="' + '/js/') os.system('open file://' + PATH_TO_HTML + '_search_geekbook_.html') - + # remove demo content head = re.sub(r'.*', r'', head, flags=re.M | re.DOTALL) return head + results #return send_from_directory('', 'file:///' + PATH_TO_HTML + '/geekbook-search.html') #return redirect(url_for('static', filename='file:///' + PATH_TO_HTML + '/geekbook-search.html')) + +def get_parser(): + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + + parser.add_argument('--public', default=False, + action="store_true", help="be public") + parser.add_argument('--debug', default=False, + action="store_true", help="debug mode") + parser.add_argument('--port', default=5000, type=int) + return parser + + #main if __name__ == "__main__": - app.run(debug=True) - # if you want your geekbook to be seen in the network uncomment this line, and comment the line above - # app.run(debug=True, host= '0.0.0.0') - # of course be vere careful with this. EVERYONE within network can read ALL your notes! (if they know your IP) + parser = get_parser() + args = parser.parse_args() + + if args.public: + # if you want your geekbook to be seen in the network uncomment this line, and comment the line above + print('WARNING PUBLIC MODE') + app.run(debug=args.debug, host= '0.0.0.0', port=args.port) + # of course be very careful with this. EVERYONE within network can read ALL your notes! (if they know your IP) + else: + app.run(debug=args.debug, port=args.port) diff --git a/geekbookapp.py b/geekbookapp.py index 97807e3..1596446 100755 --- a/geekbookapp.py +++ b/geekbookapp.py @@ -211,7 +211,10 @@ def start(self): def start_flask(args): if not args.noflask and not args.debug and not args.update: logger.info("Start off flask!") - os.system('python ' + PATH + os.sep + 'geekbook/engine/webserverflask.py &') + if args.public: + os.system('python ' + PATH + os.sep + 'geekbook/engine/webserverflask.py --public &') + else: + os.system('python ' + PATH + os.sep + 'geekbook/engine/webserverflask.py --public &') def start_gitweb(): @@ -243,6 +246,7 @@ def get_parser(): help='updates all jupiter notebooks!', action='store_true') parser.add_argument('--noflask', help='dont run flask', action='store_true') parser.add_argument('--noupdatedb', help='dont update the db', action='store_true') + parser.add_argument('--public', help='run as public server, edit engine.open_access to configure', action='store_true') return parser