pub1c-web/pub1c-rest.py

318 lines
9.6 KiB
Python
Raw Normal View History

2021-05-21 12:19:11 +00:00
import logging
import os
from typing import List, Optional
import traceback
import yaml
from flask import Flask, g
from flask_restful import Resource, Api, reqparse, abort
from pathvalidate import is_valid_filepath
from brackets import get_infobases as br_get_infobases
from webpub1c.webpub1c import VRDConfig, ApacheConfig, urlpath_join
apache_restart_flagfile = 'restart_apache'
class WebPub1C:
def __init__(self, config, verbose: bool = False):
level = logging.INFO if verbose else logging.WARNING
logging.basicConfig(level=level)
self._log = logging.getLogger("webpub1c")
self._log.setLevel(level)
self._config = config
vrd_params: Optional[VRDConfig] = self._config.get('vrd_params', None)
apache_config: str = self._config.get('apache_config', '')
self._vrd_path: str = self._config.get('vrd_path', '')
self._dir_path: str = self._config.get('dir_path', '')
self._url_base: str = self._config.get('url_base', '')
self._apache_cfg = ApacheConfig(apache_config, self._vrd_path,
self._dir_path, self._url_base,
vrd_params)
def _is_vrd_path_valid(self) -> bool:
return os.path.isdir(self._vrd_path)
def _is_dir_path_valid(self) -> bool:
return os.path.isdir(self._dir_path)
def _is_url_base_valid(self) -> bool:
return is_valid_filepath(self._url_base, platform='posix') and self._url_base.startswith('/')
def _is_module_valid(self) -> bool:
if 'platform_path' not in self._config:
return False
if 'ws_module' not in self._config:
return False
return os.path.isfile(os.path.join(self._config['platform_path'], self._config['ws_module']))
def check(self):
return {
'config': self._config,
'is_apache_cfg_valid': self._apache_cfg.is_valid(),
'is_vrd_path_valid': self._is_vrd_path_valid(),
'is_dir_path_valid': self._is_dir_path_valid(),
'is_url_base_valid': self._is_url_base_valid(),
'is_module_valid': self._is_module_valid()
}
def has_module(self):
return self._apache_cfg.has_1cws_module()
def add_module(self):
""" Add 1cws module to apache config """
if self._apache_cfg.has_1cws_module():
self._log.info('config unchanged')
else:
module: str = os.path.join(self._config['platform_path'], self._config['ws_module'])
self._apache_cfg.add_1cws_module(module)
self._log.info('module added')
def list(self) -> List[str]:
""" List publications """
return list(self._apache_cfg.publications)
def get(self, ibname: str):
""" Get publication info """
publication = self._apache_cfg.get_publication(ibname)
if publication is None:
return publication
return {
'name': publication.name,
'url': publication.url_path,
'directory': publication.directory,
'vrd_filename': publication.vrd_filename,
}
def add(self, ibname: str, url: Optional[str] = None) -> str:
""" Add new publication """
publication = self._apache_cfg.create_publication(ibname, url)
self._apache_cfg.add_publication(publication)
self._log.info(f'publication added: {ibname}')
return publication.url_path
def set_url(self, ibname: str, url: str) -> None:
""" Set publication url """
publication = self._apache_cfg.get_publication(ibname)
if publication is None:
raise KeyError(f'infobase "{ibname}" not publicated')
publication.url_path = urlpath_join(self._url_base, url)
self._apache_cfg.remove_publication(publication.name, destroy=False)
self._apache_cfg.add_publication(publication)
self._log.info(f'publication changed: {ibname}')
def remove(self, ibname: str):
""" Remove publication """
self._apache_cfg.remove_publication(ibname)
self._log.info(f'publication removed: {ibname}')
pub_parser = reqparse.RequestParser()
pub_parser.add_argument('name', required=True, type=str, help='name required')
pub_parser.add_argument('url', type=str)
url_parser = reqparse.RequestParser()
url_parser.add_argument('url', type=str)
url_req_parser = reqparse.RequestParser()
url_req_parser.add_argument('url', type=str, required=True)
def load_config(filename: str = 'config.yml'):
with open(filename, 'r', encoding='utf-8') as cfg_file:
return yaml.safe_load(cfg_file)
def get_config():
if 'config' not in g:
g.config = load_config()
return g.config
def get_webpub1c():
if 'webpub1c' not in g:
g.webpub1c = WebPub1C(get_config())
return g.webpub1c
def load_infobases(config) -> List[str]:
result = []
if 'infobases' in config and 'server_file' in config['infobases']:
result = br_get_infobases(config['infobases']['server_file'])
return result
def validate_url(url: Optional[str]):
if url is not None:
if not is_valid_filepath(url, platform='posix'):
abort(400, message='invalid url')
class InfobasesAvaible(Resource):
def get(self) -> List[str]:
cfg = get_config()
bases = load_infobases(cfg)
return bases
class ConfigTest(Resource):
def get(self):
webpub = get_webpub1c()
return webpub.check()
class Publications(Resource):
def get(self) -> List[str]:
webpub = get_webpub1c()
return webpub.list()
def put(self):
args = pub_parser.parse_args()
validate_url(args.url)
webpub = get_webpub1c()
url = args.url
if args.name in webpub.list():
abort(409, message=f'publication exists: {args.name}')
try:
url = webpub.add(args.name, url)
except Exception as e:
abort(422, message=f'publication failed: {args.name}', traceback=traceback.format_exc())
return {'message': 'created', 'name': args.name, 'url': url}, 201
class Publication(Resource):
def get(self, name: str):
webpub = get_webpub1c()
if name not in webpub.list():
abort(404, message=f'publication not found: {name}')
return {
name: webpub.get(name)
}
def put(self, name: str):
args = url_parser.parse_args()
validate_url(args.url)
webpub = get_webpub1c()
url = args.url
if name in webpub.list():
abort(409, message=f'publication exists: {name}')
try:
url = webpub.add(name, url)
except Exception as e:
abort(422, message=f'publication failed: {name}', traceback=traceback.format_exc())
return {'message': 'created', 'name': name, 'url': url}, 201
def delete(self, name: str):
webpub = get_webpub1c()
if name not in webpub.list():
abort(404, message=f'publication not found: {name}')
try:
webpub.remove(name)
except Exception as e:
abort(422, message='delete failed', traceback=traceback.format_exc())
return {'message': 'deleted', 'name': name}
class PublicationURL(Resource):
def get(self, name: str):
webpub = get_webpub1c()
if name not in webpub.list():
abort(404, message=f'publication not found: {name}')
return {
'url': webpub.get(name)['url']
}
def post(self, name: str):
args = url_req_parser.parse_args()
validate_url(args.url)
webpub = get_webpub1c()
url = args.url
if name not in webpub.list():
abort(404, message=f'publication not found: {name}')
try:
webpub.set_url(name, url)
except Exception as e:
abort(422, message=f'set url failed', traceback=traceback.format_exc())
return {'message': 'success', 'name': name, 'url': url}
class EnterpriseModule(Resource):
def get(self):
webpub = get_webpub1c()
if not webpub.has_module():
abort(404, message='not found')
return {
'message': 'found'
}
def put(self):
webpub = get_webpub1c()
if webpub.has_module():
abort(304, message='already added')
webpub.add_module()
return {
'message': 'success'
}
class ApacheRestartFlag(Resource):
def get(self):
cfg = get_config()
flagfile = cfg.get('apache_restart_flagfile', apache_restart_flagfile)
if not os.path.isfile(flagfile):
abort(404, message='not found')
return {
'message': 'found'
}
def put(self):
cfg = get_config()
flagfile = cfg.get('apache_restart_flagfile', apache_restart_flagfile)
if os.path.isfile(flagfile):
abort(304, message='found')
with open(flagfile, 'a'):
pass
return {
'message': 'success'
}
class APIIndex(Resource):
def get(self):
return ['infobases', 'publications',
'module', 'config', 'apache_restart']
app = Flask(__name__)
api = Api(app, '/api/v1/')
api.add_resource(InfobasesAvaible, '/infobases')
api.add_resource(Publications, '/publications')
api.add_resource(Publication, '/publications/<string:name>')
api.add_resource(PublicationURL, '/publications/<string:name>/url')
api.add_resource(EnterpriseModule, '/module')
api.add_resource(ConfigTest, '/config')
api.add_resource(ApacheRestartFlag, '/apache_restart')
api.add_resource(APIIndex, '/')
with app.app_context():
get_config()
get_webpub1c()
if __name__ == '__main__':
app.run(debug=True)