-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
116 lines (101 loc) · 2.91 KB
/
server.py
File metadata and controls
116 lines (101 loc) · 2.91 KB
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
from main import *
from flask import Flask, send_file, Response, request, jsonify, render_template
import os
app = Flask(__name__)
def file_name(list_item):
try:
data = []
for file in list_item:
file = file[:-4]
data.append(file)
return data
except:
return list_item[:-4]
def get_dir(dirname):
listdir = os.listdir(f'./chats/{dirname}/')
listdir = file_name(listdir)
return listdir
def get_dir_item(dirname,name):
with open(f'./chats/{dirname}/{name}.txt', 'r') as f:
content = f.read()
return content
def add_dir_item(dirname, name, content):
with open(f'./chats/{dirname}/{name}.txt', 'x') as f:
f.write(content)
def edit_dir_item(dirname, name, content):
with open(f'./chats/{dirname}/{name}.txt', 'w') as f:
f.write(content)
def delete_dir_item(dirname, name):
myfile = f'./chats/{dirname}/{name}.txt'
if os.path.isfile(myfile):
os.remove(myfile)
return True
raise Exception('not a file')
@app.route('/', methods=['GET', 'POST'])
def welcome():
return jsonify({'body':'Root of GPT-3 Python', 'status':200})
@app.route('/get/<dirname>', methods=['GET'])
def route_get_dir(dirname):
try:
listdir = get_dir(dirname)
return jsonify({
'body':{
'data':listdir,
'name':dirname,
},
'status':200,
})
except:
return jsonify({
'status':304,
})
@app.route('/get/<dirname>/<name>', methods=['GET'])
def route_get_dir_item(dirname,name):
try:
item = get_dir_item(dirname,name)
return jsonify({
'body':item,
'status':200,
})
except:
return jsonify({
'status':304,
})
@app.route('/add/<dirname>/<name>/<content>', methods=['GET', 'POST'])
def route_add_dir_item(dirname, name, content):
try:
add_dir_item(dirname, name, content)
return jsonify({
'status':200,
})
except:
return jsonify({
'status':304,
})
@app.route('/edit/<dirname>/<name>/<content>', methods=['GET', 'PUT'])
def route_edit_dir_item(dirname, name, content):
try:
edit_dir_item(dirname, name, content)
return jsonify({
'status':200,
})
except:
return jsonify({
'status':304,
})
@app.route('/delete/<dirname>/<name>', methods=['GET', 'DELETE'])
def route_delete_dir_item(dirname, name):
try:
delete_dir_item(dirname, name)
return jsonify({
'status':200,
})
except:
return jsonify({
'status':304,
})
@app.route('/chat', methods=['GET'])
def route_chat():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=2023, debug=True)