summaryrefslogtreecommitdiff
path: root/server.py
blob: 40123306db1355847c669d0e62f0e6384f786bf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from datetime import datetime
from flask import Flask
from flask import json, jsonify, render_template, request
from unipath import FSPath as Path
from uuid import uuid4

DATA_DIR = Path(__file__).absolute().ancestor(1).child('data')

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/data', methods=['POST', 'GET'])
def data():
    if request.method == 'GET':
        with open(DATA_DIR.child('data.txt'), 'r') as f:
            return jsonify({
                'data': f.read()
            })
    elif request.method == 'POST':
        with open(DATA_DIR.child('data.txt'), 'w') as f:
            f.write(request.json['data'])
        with open(DATA_DIR.child('data.{}.txt'.format(uuid4().hex[:10])), 'w') as f:
            f.write(request.json['data'])
        return 'ok'

if __name__ == '__main__':
    app.run(debug=True)