You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.8 KiB
Python
75 lines
1.8 KiB
Python
import os
|
|
from glob import glob
|
|
from flask import Flask, jsonify
|
|
import frontmatter
|
|
from markdown import markdown
|
|
|
|
|
|
base_url = '/soupboat/atlas-api'
|
|
|
|
|
|
# create flask application
|
|
app = Flask(__name__)
|
|
|
|
|
|
# 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()]
|
|
|
|
|
|
app.wsgi_app = PrefixMiddleware(
|
|
app.wsgi_app, prefix=base_url
|
|
)
|
|
|
|
|
|
# definition of the routes
|
|
|
|
@app.route('/<contribution>')
|
|
def contribution(contribution: None):
|
|
|
|
try:
|
|
with open(f'contributions/{contribution}.md', 'r') as f:
|
|
meta, content = frontmatter.parse(f.read())
|
|
c = {**meta, 'content_html': markdown(content)}
|
|
except FileNotFoundError:
|
|
c = {"error": f"'{contribution}' is not a valid contribution!"}
|
|
|
|
response = jsonify(c)
|
|
response.headers.add('Access-Control-Allow-Origin', '*')
|
|
return response
|
|
|
|
|
|
@app.route('/contributions')
|
|
def contributions():
|
|
|
|
files = glob("contributions/*.md")
|
|
|
|
contrib = []
|
|
|
|
for file in files:
|
|
with open(file, 'r') as f:
|
|
meta, content = frontmatter.parse(f.read())
|
|
c = {**meta, 'content_html': markdown(content)}
|
|
contrib.append(c)
|
|
|
|
response = jsonify(contrib)
|
|
response.headers.add('Access-Control-Allow-Origin', '*')
|
|
return response
|
|
|
|
|
|
app.run(port=3142)
|