-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
134 lines (119 loc) · 3.96 KB
/
app.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from datetime import timedelta
import json
import os
import threading
from uuid import uuid1
from flask import Flask, request, Response
from flask_cors import CORS
from flask_httpauth import HTTPBasicAuth
import shelve
import redis
import util
REDIS_HOST = os.environ.get('REDIS_HOST') or 'localhost'
REDIS_PORT = os.environ.get('REDIS_PORT') or 6379
REDIS_DB = os.environ.get('REDIS_DB') or 0
app = Flask(__name__)
CORS(app)
auth = HTTPBasicAuth()
db_write_lock = threading.Lock()
r = redis.StrictRedis(REDIS_HOST, REDIS_PORT, REDIS_DB)
@app.errorhandler(util.LackJSONParameter)
def lack_json_parameter(e):
db_write_lock.release()
return Response(str(e), 400)
@auth.verify_password
def verify_password(username, password) -> bool:
with shelve.open('data/user', "c") as data:
try:
return data[username] == password
except:
return False
@app.route('/user', methods=['GET'])
@auth.login_required
def get_user():
return auth.get_auth().get('username')
@app.route('/software/<name>', methods=['POST'])
@auth.login_required
def add(name):
db_write_lock.acquire()
with shelve.open('data/software', "c", writeback=True) as data:
if name in data:
db_write_lock.release()
return Response('Already exists', 400)
description = util.get_from_json(request.json, 'description')
content = util.get_from_json(request.json, 'content')
if util.check(content):
data[name] = {
'description': description,
'content': content
}
db_write_lock.release()
return 'Ok'
else:
db_write_lock.release()
return Response('Format error', 400)
@auth.login_required
@app.route('/software/<name>', methods=['DELETE'])
def remove(name):
db_write_lock.acquire()
with shelve.open('data/software', "c", writeback=True) as data:
try:
data.pop(name)
except KeyError:
db_write_lock.release()
return Response('Not found {}', 400)
db_write_lock.release()
return 'Ok'
@auth.login_required
@app.route('/software/<name>', methods=['PUT'])
def update(name):
db_write_lock.acquire()
with shelve.open('data/software', "c", writeback=True) as data:
if name not in data:
db_write_lock.release()
return Response('Not found', 400)
description = util.get_from_json(request.json, 'description')
content = util.get_from_json(request.json, 'content')
if util.check(content):
data[name] = {
'description': description,
'content': content
}
db_write_lock.release()
return 'Ok'
else:
db_write_lock.release()
return Response('Format error', 400)
@app.route('/software/<name>', methods=['GET'])
def select(name):
with shelve.open('data/software', "c") as data:
try:
info = data.get(name) or json.loads(r.get(name))
return info['content']
except KeyError:
return Response('Not found', 400)
@app.route('/software/<name>/info', methods=['GET'])
def info(name):
with shelve.open('data/software', "c") as data:
try:
res = data.get(name) or json.loads(r.get(name))
del res['content']
return json.dumps(res)
except KeyError:
return Response('Not found', 400)
@app.route('/list/software', methods=['GET'])
def listall():
with shelve.open('data/software', "c") as data:
return json.dumps(list(map(str, data.keys())))
@app.route('/software/temp', methods=['POST'])
def temp():
name = str(uuid1())
description = util.get_from_json(request.json, 'description')
content = util.get_from_json(request.json, 'content')
r.set(name, json.dumps({
'description': description,
'content': content
}), timedelta(minutes=5))
return json.dumps({
'name': name
})