init flask app

master
km0 3 years ago
parent acc4ee822d
commit 508e807ff3

2
.gitignore vendored

@ -0,0 +1,2 @@
venv/
.vscode

@ -0,0 +1,3 @@
# SI18.5 - Diffractive Coockbook
Eheh

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/vue@3"></script> <script src="https://unpkg.com/vue@3"></script>
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
<title>diffractive reading</title> <title>Diffractive reading</title>
</head> </head>
<body> <body>
<div id="app"> <div id="app">

@ -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)

@ -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
---
Loading…
Cancel
Save