6
6
app = Flask (__name__ )
7
7
CORS (app )
8
8
9
- # Paths to JSON files
10
9
workspace_path = os .path .join (os .path .dirname (__file__ ), '../space/workspace.json' )
11
10
key_path = os .path .join (os .path .dirname (__file__ ), '../space/key.json' )
11
+ space_color_path = os .path .join (os .path .dirname (__file__ ), '../space/space-color.json' )
12
12
13
13
def read_json (file_path ):
14
14
try :
@@ -26,62 +26,69 @@ def write_json(file_path, data):
26
26
27
27
@app .route ('/<path>' , methods = ['GET' ])
28
28
def get_data (path ):
29
- file_path = workspace_path if path == 'workspace' else key_path
30
- data = read_json (file_path )
31
- if "error" in data :
32
- return jsonify (data ), 500
29
+ file_path = {
30
+ 'workspace' : workspace_path ,
31
+ 'key' : key_path ,
32
+ 'space-color' : space_color_path
33
+ }.get (path )
34
+ if file_path :
35
+ data = read_json (file_path )
36
+ return jsonify (data )
37
+ else :
38
+ return jsonify ({"error" : "Invalid path" }), 400
39
+
40
+ @app .route ('/workspace' , methods = ['POST' ])
41
+ def add_workspace ():
42
+ user_input = request .json .get ('input' )
43
+ data = read_json (workspace_path )
44
+ data ['workspace' ].append (user_input )
45
+ write_json (workspace_path , data )
33
46
return jsonify (data )
34
47
35
- @app .route ('/receive-message' , methods = ['GET' ])
36
- def receive_message ():
37
- message = "This is a local message."
38
- return jsonify ({"message" : message })
48
+ @app .route ('/workspace' , methods = ['DELETE' ])
49
+ def delete_workspace ():
50
+ index = int (request .json .get ('index' ))
51
+ data = read_json (workspace_path )
52
+ try :
53
+ data ['workspace' ].pop (index )
54
+ except IndexError :
55
+ return jsonify ({"error" : "Invalid index" }), 400
56
+ write_json (workspace_path , data )
57
+ return jsonify (data )
39
58
40
- @app .route ('/<path> ' , methods = ['POST' ])
41
- def send_input ( path ):
42
- file_path = workspace_path if path == 'workspace' else key_path
43
- data = request . json
44
- user_input = data . get ( 'input' )
45
- if not user_input :
46
- return jsonify ({ "error" : "No input provided" }), 400
59
+ @app .route ('/key ' , methods = ['POST' ])
60
+ def add_key ( ):
61
+ user_input = request . json . get ( 'input' )
62
+ data = read_json ( key_path )
63
+ data [ 'key' ]. append ( user_input )
64
+ write_json ( key_path , data )
65
+ return jsonify (data )
47
66
48
- json_data = read_json (file_path )
49
- if "error" in json_data :
50
- return jsonify (json_data ), 500
67
+ @app .route ('/key' , methods = ['DELETE' ])
68
+ def delete_key ():
69
+ index = int (request .json .get ('index' ))
70
+ data = read_json (key_path )
71
+ try :
72
+ data ['key' ].pop (index )
73
+ except IndexError :
74
+ return jsonify ({"error" : "Invalid index" }), 400
75
+ write_json (key_path , data )
76
+ return jsonify (data )
51
77
52
- item_list = json_data .get (path , [])
53
- if user_input not in item_list :
54
- item_list .append (user_input )
55
- json_data [path ] = item_list
56
- write_json (file_path , json_data )
57
-
58
- return jsonify ({path : item_list })
78
+ @app .route ('/space-color' , methods = ['POST' ])
79
+ def update_space_color ():
80
+ user_input = request .json .get ('input' )
81
+ nested_keys = request .json .get ('keys' )
82
+ data = read_json (space_color_path )
59
83
60
- @app .route ('/<path>' , methods = ['DELETE' ])
61
- def delete_input (path ):
62
- file_path = workspace_path if path == 'workspace' else key_path
63
- data = request .json
64
- index = data .get ('index' )
65
- if index is None :
66
- return jsonify ({"error" : "No index provided" }), 400
84
+ d = data ['workspace' ]['background' ]
85
+ for key in nested_keys [:- 1 ]:
86
+ d = d [key ]
87
+ d [nested_keys [- 1 ]] = user_input
67
88
68
- json_data = read_json (file_path )
69
- if "error" in json_data :
70
- return jsonify (json_data ), 500
89
+ write_json (space_color_path , data )
90
+ return jsonify (data )
71
91
72
- item_list = json_data .get (path , [])
73
- try :
74
- index = int (index )
75
- if 0 <= index < len (item_list ):
76
- item_list .pop (index )
77
- json_data [path ] = item_list
78
- write_json (file_path , json_data )
79
- else :
80
- return jsonify ({"error" : "Index out of range" }), 400
81
- except ValueError :
82
- return jsonify ({"error" : "Invalid index" }), 400
83
-
84
- return jsonify ({path : item_list })
85
92
86
93
if __name__ == '__main__' :
87
- app .run (host = '127.0.0.1' , port = 5000 )
94
+ app .run (host = '127.0.0.1' , port = 5000 )
0 commit comments