11import datetime
22import io
33import os
4-
54import flask
65
76from app .api import api
87
98
109@api .route ('/config/<name>' , methods = ['GET' ])
11- def get_config (name ):
10+ def get_config (name : str ):
11+ """
12+ Reads the file with the corresponding name that was passed.
13+
14+ :param name: Configuration file name
15+ :type name: str
16+
17+ :return: Rendered HTML document with content of the configuration file.
18+ :rtype: str
19+ """
1220 nginx_path = flask .current_app .config ['NGINX_PATH' ]
1321
1422 with io .open (os .path .join (nginx_path , name ), 'r' ) as f :
1523 _file = f .read ()
1624
17- return flask .render_template ('config.html' , name = name , file = _file )
25+ return flask .render_template ('config.html' , name = name , file = _file ), 200
1826
1927
2028@api .route ('/config/<name>' , methods = ['POST' ])
21- def post_config (name ):
29+ def post_config (name : str ):
30+ """
31+ Accepts the customized configuration and saves it in the configuration file with the supplied name.
32+
33+ :param name: Configuration file name
34+ :type name: str
35+
36+ :return:
37+ :rtype: werkzeug.wrappers.Response
38+ """
2239 content = flask .request .get_json ()
2340 nginx_path = flask .current_app .config ['NGINX_PATH' ]
2441
@@ -30,6 +47,12 @@ def post_config(name):
3047
3148@api .route ('/domains' , methods = ['GET' ])
3249def get_domains ():
50+ """
51+ Reads all files from the configuration file directory and checks the state of the site configuration.
52+
53+ :return: Rendered HTML document with the domains
54+ :rtype: str
55+ """
3356 config_path = flask .current_app .config ['CONFIG_PATH' ]
3457 sites_available = []
3558 sites_enabled = []
@@ -55,11 +78,23 @@ def get_domains():
5578 'time' : time
5679 })
5780
58- return flask .render_template ('domains.html' , sites_available = sites_available , sites_enabled = sites_enabled )
81+ # sort sites by name
82+ sites_available = sorted (sites_available , key = lambda _ : _ ['name' ])
83+ return flask .render_template ('domains.html' , sites_available = sites_available , sites_enabled = sites_enabled ), 200
5984
6085
6186@api .route ('/domain/<name>' , methods = ['GET' ])
62- def get_domain (name ):
87+ def get_domain (name : str ):
88+ """
89+ Takes the name of the domain configuration file and
90+ returns a rendered HTML with the current configuration of the domain.
91+
92+ :param name: The domain name that corresponds to the name of the file.
93+ :type name: str
94+
95+ :return: Rendered HTML document with the domain
96+ :rtype: str
97+ """
6398 config_path = flask .current_app .config ['CONFIG_PATH' ]
6499 _file = ''
65100 enabled = True
@@ -78,23 +113,44 @@ def get_domain(name):
78113
79114 break
80115
81- return flask .render_template ('domain.html' , name = name , file = _file , enabled = enabled )
116+ return flask .render_template ('domain.html' , name = name , file = _file , enabled = enabled ), 200
82117
83118
84119@api .route ('/domain/<name>' , methods = ['POST' ])
85- def post_domain (name ):
120+ def post_domain (name : str ):
121+ """
122+ Creates the configuration file of the domain.
123+
124+ :param name: The domain name that corresponds to the name of the file.
125+ :type name: str
126+
127+ :return: Returns a status about the success or failure of the action.
128+ """
86129 config_path = flask .current_app .config ['CONFIG_PATH' ]
87130 new_domain = flask .render_template ('new_domain.j2' , name = name )
88131 name = name + '.conf.disabled'
89132
90- with io .open (os .path .join (config_path , name ), 'w' ) as f :
91- f .write (new_domain )
133+ try :
134+ with io .open (os .path .join (config_path , name ), 'w' ) as f :
135+ f .write (new_domain )
136+
137+ response = flask .jsonify ({'success' : True }), 201
138+ except Exception as ex :
139+ response = flask .jsonify ({'success' : False , 'error_msg' : ex }), 500
92140
93- return flask . jsonify ({ 'success' : True }), 201
141+ return response
94142
95143
96144@api .route ('/domain/<name>' , methods = ['DELETE' ])
97- def delete_domain (name ):
145+ def delete_domain (name : str ):
146+ """
147+ Deletes the configuration file of the corresponding domain.
148+
149+ :param name: The domain name that corresponds to the name of the file.
150+ :type name: str
151+
152+ :return: Returns a status about the success or failure of the action.
153+ """
98154 config_path = flask .current_app .config ['CONFIG_PATH' ]
99155 removed = False
100156
@@ -113,7 +169,15 @@ def delete_domain(name):
113169
114170
115171@api .route ('/domain/<name>' , methods = ['PUT' ])
116- def put_domain (name ):
172+ def put_domain (name : str ):
173+ """
174+ Updates the configuration file with the corresponding domain name.
175+
176+ :param name: The domain name that corresponds to the name of the file.
177+ :type name: str
178+
179+ :return: Returns a status about the success or failure of the action.
180+ """
117181 content = flask .request .get_json ()
118182 config_path = flask .current_app .config ['CONFIG_PATH' ]
119183
@@ -128,7 +192,15 @@ def put_domain(name):
128192
129193
130194@api .route ('/domain/<name>/enable' , methods = ['POST' ])
131- def enable_domain (name ):
195+ def enable_domain (name : str ):
196+ """
197+ Activates the domain in Nginx so that the configuration is applied.
198+
199+ :param name: The domain name that corresponds to the name of the file.
200+ :type name: str
201+
202+ :return: Returns a status about the success or failure of the action.
203+ """
132204 content = flask .request .get_json ()
133205 config_path = flask .current_app .config ['CONFIG_PATH' ]
134206
0 commit comments