From 36ac6b8a802541a3ca6092b949e61e6da8f102dd Mon Sep 17 00:00:00 2001 From: subnixr Date: Tue, 24 Apr 2018 17:55:59 +0200 Subject: [PATCH] web: add list api --- .gitignore | 1 + setup.py | 1 + src/ciclostile_web | 64 --------------------------------------------- src/web/__init__.py | 22 ++++++++++++++++ src/web/api.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 64 deletions(-) create mode 100644 .gitignore delete mode 100755 src/ciclostile_web create mode 100644 src/web/__init__.py create mode 100755 src/web/api.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11041c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.egg-info diff --git a/setup.py b/setup.py index dfbbd32..00f43ed 100755 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ setup( entry_points=''' [console_scripts] ciclostile=cli:cli + ciclostile-web=web:cli ''', install_requires=['Click', 'markdown', 'jinja2', 'flask', 'flask-httpauth'] diff --git a/src/ciclostile_web b/src/ciclostile_web deleted file mode 100755 index 973b8d5..0000000 --- a/src/ciclostile_web +++ /dev/null @@ -1,64 +0,0 @@ -# #!/usr/bin/env python3 -# import os -# from flask import Flask, send_from_directory, render_template, request -# from flask_httpauth import HTTPDigestAuth -# import ciclostile - -# working_dir = os.getcwd() - -# target_path, template_path, markdown_path = [ -# os.path.join(working_dir, folder_name) -# for folder_name in ['target', 'templates', 'markdown'] -# ] - -# app = Flask(__name__) -# app.config['SECRET_KEY'] = 'secret key here' -# auth = HTTPDigestAuth() - -# users = { -# 'admin': 'password', -# } - - -# @auth.get_password -# def get_pw(username): -# if username in users: -# return users.get(username) -# return None - - -# @app.route('/assets/') -# def assets(path): -# assets_path = os.path.join(target_path, 'assets') -# return send_from_directory(assets_path, path) - - -# @app.route('/') -# @app.route('/.html') -# def page(page_name): -# return ciclostile.render(page_name, markdown_path, template_path) - - -# @app.route('//edit') -# @auth.login_required -# def edit(page_name): -# md_text = ciclostile.read_markdown(page_name, markdown_path) -# return render_template('edit.html', **locals()) - - -# @app.route('/edit', methods=['POST']) -# @auth.login_required -# def edit_actions(): -# page_name = request.form['page_name'] -# md_text = request.form['md_text'] - -# if request.form['action'] == 'preview': -# return ciclostile.render_from_text(md_text, template_path) - -# if request.form['action'] == 'save': -# ciclostile.save(md_text, page_name, markdown_path) -# return page(page_name) - - -# if __name__ == '__main__': -# app.run() diff --git a/src/web/__init__.py b/src/web/__init__.py new file mode 100644 index 0000000..3caf9ed --- /dev/null +++ b/src/web/__init__.py @@ -0,0 +1,22 @@ +import click +from flask import Flask, send_from_directory, render_template, request +from flask_httpauth import HTTPDigestAuth + +from .api import api + + +app = Flask(__name__) +app.register_blueprint(api, url_prefix="/api") + +app.config['SECRET_KEY'] = 'secret key here' + + +@click.command() +@click.option("-p", "--port", type=int, default=12345) +@click.argument("post_dir", default=".") +def cli(port, post_dir): + app.run(port=port) + + +if __name__ == '__main__': + cli() diff --git a/src/web/api.py b/src/web/api.py new file mode 100755 index 0000000..3763ebe --- /dev/null +++ b/src/web/api.py @@ -0,0 +1,60 @@ +import os +from flask import Blueprint, send_from_directory, render_template, request, jsonify +from flask_httpauth import HTTPDigestAuth +import ciclostile + +api = Blueprint('api', __name__) + +# TODO: get from cli param +POST_DIR = "." + +# auth = HTTPDigestAuth() +# users = { +# 'admin': 'password', +# } + + +# @auth.get_password +# def get_pw(username): +# if username in users: +# return users.get(username) +# return None + + +# @api.route('/assets/') +# def assets(path): +# assets_path = os.path.join(target_path, 'assets') +# return send_from_directory(assets_path, path) + + +# @api.route('/') +# @api.route('/.html') +# def page(page_name): +# return ciclostile.render(page_name, markdown_path, template_path) + + +# @api.route('//edit') +# @auth.login_required +# def edit(page_name): +# md_text = ciclostile.read_markdown(page_name, markdown_path) +# return render_template('edit.html', **locals()) + + +# @api.route('/edit', methods=['POST']) +# @auth.login_required +# def edit_actions(): +# page_name = request.form['page_name'] +# md_text = request.form['md_text'] + +# if request.form['action'] == 'preview': +# return ciclostile.render_from_text(md_text, template_path) + +# if request.form['action'] == 'save': +# ciclostile.save(md_text, page_name, markdown_path) +# return page(page_name) + + +@api.route("/posts") +def posts_list(): + dirs = os.listdir(os.path.join(POST_DIR)) + return jsonify(dirs)