diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ed174 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +.vscode \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ec9aa9 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# SI18.5 - Diffractive Coockbook + +Eheh diff --git a/app.js b/client/app.js similarity index 100% rename from app.js rename to client/app.js diff --git a/components/CookbookForm.js b/client/components/CookbookForm.js similarity index 100% rename from components/CookbookForm.js rename to client/components/CookbookForm.js diff --git a/index.html b/client/index.html similarity index 94% rename from index.html rename to client/index.html index 523d29a..078a304 100644 --- a/index.html +++ b/client/index.html @@ -6,7 +6,7 @@ - diffractive reading + Diffractive reading
diff --git a/style.css b/client/style.css similarity index 100% rename from style.css rename to client/style.css diff --git a/cookbook.py b/cookbook.py new file mode 100644 index 0000000..0b97cd6 --- /dev/null +++ b/cookbook.py @@ -0,0 +1,96 @@ +from flask import Flask, jsonify, request, redirect, url_for +from flask_cors import CORS +import datetime +import frontmatter +from glob import glob + +# prefix to add /SOUPBOAT/ATLAS-API to all the routes +# and to leave the @app.route() decorator more clean + + +class PrefixMiddleware(object): + def __init__(self, app, prefix=""): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + + if environ["PATH_INFO"].startswith(self.prefix): + environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix) :] + environ["SCRIPT_NAME"] = self.prefix + return self.app(environ, start_response) + else: + start_response("404", [("Content-Type", "text/plain")]) + return ["This url does not belong to the app.".encode()] + + +base_url = "/soupboat/cookbook" + +# create flask application +app = Flask(__name__) +CORS(app) + +# register the middleware to prefix all the requests with our base_url +app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=base_url) + + +# save the incoming data into a markdown file +# data is a dictionary with at least a 'title' key +def create_recipe(data): + # create filename from the title + # TODO: ensure safe filename + slug = data["title"].replace(" ", "-").lower() + today = datetime.datetime.now().strftime("%d-%m-%Y_%H-%M-%S") + + # create the list of properties from the incoming data, add the date + recipe = {**data, "date": datetime.datetime.now()} + post = frontmatter.Post("", **recipe) + + # save the data in a markdown file + # TODO: print directly yml file instead of md? + # since we are not really using the body of the md but just the yml frontmatter + with open(f"recipes/{slug}_{today}.md", "w") as f: + documentation = frontmatter.dumps(post) + f.write(documentation) + print(f"{slug}_{today}.md - saved in the archive") + + +# read the files from the recipes folder and return them in a list +def get_recipes(): + recipes = [] + paths = glob("recipes/*.md") + for path in paths: + with open(path, "r") as f: + meta, content = frontmatter.parse(f.read()) + recipes.append(meta) + return recipes + + +# sample object to test the md output +r = { + "title": "Test test test", + "description": "A super simple description", + "logs": ["first step", "second step", "third step"], + "who": "a friend of mine", +} + + +@app.route("/", methods=["GET", "POST"]) +def home(): + if request.method == "POST": + create_recipe(request.json) + redirect(url_for("home.home")) + + return "hello" + + +# return the list of recipes in JSON format +# TODO: get data from client +@app.route("/get") +def get(): + recipes = get_recipes() + return jsonify(recipes) + + +# TODO: set another port and setup nginx +app.run(port=5000) diff --git a/recipes/test-test-test_31-05-2022_01-38-40.md b/recipes/test-test-test_31-05-2022_01-38-40.md new file mode 100644 index 0000000..e0a8865 --- /dev/null +++ b/recipes/test-test-test_31-05-2022_01-38-40.md @@ -0,0 +1,10 @@ +--- +date: 2022-05-31 01:38:40.484897 +description: A super simple description +logs: +- first step +- second step +- third step +title: Test test test +who: a friend of mine +--- \ No newline at end of file