|
| 1 | +import datetime |
| 2 | +import io |
| 3 | +import os |
| 4 | + |
| 5 | +import flask |
| 6 | + |
| 7 | +from app.api import api |
| 8 | + |
| 9 | + |
| 10 | +@api.route('/config/<name>', methods=['GET']) |
| 11 | +def get_config(name): |
| 12 | + nginx_path = flask.current_app.config['NGINX_PATH'] |
| 13 | + |
| 14 | + with io.open(os.path.join(nginx_path, name), 'r') as f: |
| 15 | + _file = f.read() |
| 16 | + |
| 17 | + return flask.render_template('config.html', name=name, file=_file) |
| 18 | + |
| 19 | + |
| 20 | +@api.route('/config/<name>', methods=['POST']) |
| 21 | +def post_config(name): |
| 22 | + content = flask.request.get_json() |
| 23 | + nginx_path = flask.current_app.config['NGINX_PATH'] |
| 24 | + |
| 25 | + with io.open(os.path.join(nginx_path, name), 'w') as f: |
| 26 | + f.write(content['file']) |
| 27 | + |
| 28 | + return flask.make_response({'success': True}), 200 |
| 29 | + |
| 30 | + |
| 31 | +@api.route('/domains', methods=['GET']) |
| 32 | +def get_domains(): |
| 33 | + config_path = flask.current_app.config['CONFIG_PATH'] |
| 34 | + sites_available = [] |
| 35 | + sites_enabled = [] |
| 36 | + |
| 37 | + for _ in os.listdir(config_path): |
| 38 | + |
| 39 | + if os.path.isfile(os.path.join(config_path, _)): |
| 40 | + domain, state = _.rsplit('.', 1) |
| 41 | + |
| 42 | + if state == 'conf': |
| 43 | + time = datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(config_path, _))) |
| 44 | + |
| 45 | + sites_available.append({ |
| 46 | + 'name': domain, |
| 47 | + 'time': time |
| 48 | + }) |
| 49 | + sites_enabled.append(domain) |
| 50 | + elif state == 'disabled': |
| 51 | + time = datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(config_path, _))) |
| 52 | + |
| 53 | + sites_available.append({ |
| 54 | + 'name': domain.rsplit('.', 1)[0], |
| 55 | + 'time': time |
| 56 | + }) |
| 57 | + |
| 58 | + return flask.render_template('domains.html', sites_available=sites_available, sites_enabled=sites_enabled) |
| 59 | + |
| 60 | + |
| 61 | +@api.route('/domain/<name>', methods=['GET']) |
| 62 | +def get_domain(name): |
| 63 | + config_path = flask.current_app.config['CONFIG_PATH'] |
| 64 | + _file = '' |
| 65 | + enabled = True |
| 66 | + |
| 67 | + for _ in os.listdir(config_path): |
| 68 | + |
| 69 | + if os.path.isfile(os.path.join(config_path, _)): |
| 70 | + if _.startswith(name): |
| 71 | + domain, state = _.rsplit('.', 1) |
| 72 | + |
| 73 | + if state == 'disabled': |
| 74 | + enabled = False |
| 75 | + |
| 76 | + with io.open(os.path.join(config_path, _), 'r') as f: |
| 77 | + _file = f.read() |
| 78 | + |
| 79 | + break |
| 80 | + |
| 81 | + return flask.render_template('domain.html', name=name, file=_file, enabled=enabled) |
| 82 | + |
| 83 | + |
| 84 | +@api.route('/domain/<name>', methods=['POST']) |
| 85 | +def post_domain(name): |
| 86 | + config_path = flask.current_app.config['CONFIG_PATH'] |
| 87 | + new_domain = flask.render_template('new_domain.j2', name=name) |
| 88 | + name = name + '.conf.disabled' |
| 89 | + |
| 90 | + with io.open(os.path.join(config_path, name), 'w') as f: |
| 91 | + f.write(new_domain) |
| 92 | + |
| 93 | + return flask.jsonify({'success': True}), 201 |
| 94 | + |
| 95 | + |
| 96 | +@api.route('/domain/<name>', methods=['DELETE']) |
| 97 | +def delete_domain(name): |
| 98 | + config_path = flask.current_app.config['CONFIG_PATH'] |
| 99 | + removed = False |
| 100 | + |
| 101 | + for _ in os.listdir(config_path): |
| 102 | + |
| 103 | + if os.path.isfile(os.path.join(config_path, _)): |
| 104 | + if _.startswith(name): |
| 105 | + os.remove(os.path.join(config_path, _)) |
| 106 | + removed = not os.path.exists(os.path.join(config_path, _)) |
| 107 | + break |
| 108 | + |
| 109 | + if removed: |
| 110 | + return flask.jsonify({'success': True}), 200 |
| 111 | + else: |
| 112 | + return flask.jsonify({'success': False}), 400 |
| 113 | + |
| 114 | + |
| 115 | +@api.route('/domain/<name>', methods=['PUT']) |
| 116 | +def put_domain(name): |
| 117 | + content = flask.request.get_json() |
| 118 | + config_path = flask.current_app.config['CONFIG_PATH'] |
| 119 | + |
| 120 | + for _ in os.listdir(config_path): |
| 121 | + |
| 122 | + if os.path.isfile(os.path.join(config_path, _)): |
| 123 | + if _.startswith(name): |
| 124 | + with io.open(os.path.join(config_path, _), 'w') as f: |
| 125 | + f.write(content['file']) |
| 126 | + |
| 127 | + return flask.make_response({'success': True}), 200 |
| 128 | + |
| 129 | + |
| 130 | +@api.route('/domain/<name>/enable', methods=['POST']) |
| 131 | +def enable_domain(name): |
| 132 | + content = flask.request.get_json() |
| 133 | + config_path = flask.current_app.config['CONFIG_PATH'] |
| 134 | + |
| 135 | + for _ in os.listdir(config_path): |
| 136 | + |
| 137 | + if os.path.isfile(os.path.join(config_path, _)): |
| 138 | + if _.startswith(name): |
| 139 | + if content['enable']: |
| 140 | + new_filename, disable = _.rsplit('.', 1) |
| 141 | + os.rename(os.path.join(config_path, _), os.path.join(config_path, new_filename)) |
| 142 | + else: |
| 143 | + os.rename(os.path.join(config_path, _), os.path.join(config_path, _ + '.disabled')) |
| 144 | + |
| 145 | + return flask.make_response({'success': True}), 200 |
0 commit comments