Skip to content

Commit

Permalink
geekbookapp.py: --public (2) webserverflask.py --port
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagnus committed Mar 22, 2018
1 parent 8ae8eb7 commit 2edbda4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
66 changes: 57 additions & 9 deletions engine/webserverflask.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/<note_title>')
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

Expand All @@ -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/<path:path>')
Expand All @@ -69,6 +90,11 @@ def send_img(path):
@app.route('/view/<note_title>')
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/')
Expand All @@ -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/<text>')
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()
Expand All @@ -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'<!-- start of demo -->.*<!-- end of demo -->', 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)
6 changes: 5 additions & 1 deletion geekbookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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


Expand Down

1 comment on commit 2edbda4

@mmagnus
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #74

Please sign in to comment.