cors + api rewrite

This commit is contained in:
Dmitry Belyaev 2021-05-31 16:08:10 +03:00
parent 8ab5215c26
commit 0ab6b70e6f
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
3 changed files with 69 additions and 21 deletions

View File

@ -2,6 +2,7 @@ infobases:
server_file: test/1CV8Clst.lst
apache_restart_flagfile: test/apache_restart
apache_config: webpub1c/test/apache.cfg
url_prefix: http://localhost
vrd_path: webpub1c/test/vrds
dir_path: webpub1c/test/pubs
url_base: /1c

View File

@ -1,19 +1,40 @@
import logging
import os
from typing import List, Optional
from typing import List, Optional, Dict
import traceback
import yaml
from flask import Flask, g
from flask_restful import Resource, Api, reqparse, abort
from flask_cors import CORS
from pathvalidate import is_valid_filepath
from brackets import get_infobases as br_get_infobases
from webpub1c.webpub1c import VRDConfig, ApacheConfig, urlpath_join
from webpub1c.webpub1c import VRDConfig, ApacheConfig, urlpath_join, WebPublication
apache_restart_flagfile = 'restart_apache'
def infobase_data_blank(name: str):
return {
'name': name,
'publicated': False,
'url': '',
'directory': '',
'vrd_filename': '',
}
def publication_data(publication: WebPublication):
return {
'name': publication.name,
'publicated': True,
'url': publication.url_path,
'directory': publication.directory,
'vrd_filename': publication.vrd_filename,
}
class WebPub1C:
def __init__(self, config, verbose: bool = False):
level = logging.INFO if verbose else logging.WARNING
@ -51,7 +72,6 @@ class WebPub1C:
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(),
@ -73,10 +93,15 @@ class WebPub1C:
self._log.info('module added')
def list(self) -> List[str]:
""" List publications """
""" List publication names """
return list(self._apache_cfg.publications)
def publications(self) -> List[Dict[str, str]]:
""" List of publications """
pubs = map(lambda p: publication_data(p), self._apache_cfg.iter())
return list(pubs)
def get(self, ibname: str):
""" Get publication info """
@ -84,12 +109,7 @@ class WebPub1C:
if publication is None:
return publication
return {
'name': publication.name,
'url': publication.url_path,
'directory': publication.directory,
'vrd_filename': publication.vrd_filename,
}
return publication_data(publication)
def add(self, ibname: str, url: Optional[str] = None) -> str:
""" Add new publication """
@ -161,23 +181,46 @@ def validate_url(url: Optional[str]):
abort(400, message='invalid url')
class InfobasesAvaible(Resource):
class InfobasesAvailable(Resource):
def get(self) -> List[str]:
cfg = get_config()
bases = load_infobases(cfg)
return bases
class Config(Resource):
def get(self):
return get_config()
class ConfigTest(Resource):
def get(self):
webpub = get_webpub1c()
return webpub.check()
class Publications(Resource):
def get(self) -> List[str]:
class InfobasesAll(Resource):
def get(self) -> List[Dict[str, str]]:
cfg = get_config()
webpub = get_webpub1c()
return webpub.list()
infobase_names = load_infobases(cfg)
pubs = webpub.publications()
result: List[Dict[str, str]] = []
for name in infobase_names:
publicated = False
for pub in filter(lambda x: x["name"] == name, pubs):
publicated = True
result.append(pub)
break
if not publicated:
result.append(infobase_data_blank(name))
return result
class Publications(Resource):
def get(self) -> List[Dict[str, str]]:
webpub = get_webpub1c()
return webpub.publications()
def put(self):
args = pub_parser.parse_args()
@ -292,20 +335,23 @@ class ApacheRestartFlag(Resource):
class APIIndex(Resource):
def get(self):
return ['infobases', 'publications',
'module', 'config', 'apache_restart']
return ['infobases-available', 'infobases-all', 'publications',
'module', 'config', 'config-test', 'apache-restart']
app = Flask(__name__)
api = Api(app, '/api/v1/')
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
api.add_resource(InfobasesAvaible, '/infobases')
api.add_resource(Publications, '/publications')
api.add_resource(InfobasesAvailable, '/infobases-available')
api.add_resource(InfobasesAll, '/infobases-all')
api.add_resource(Publications, '/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(Config, '/config')
api.add_resource(ConfigTest, '/config-test')
api.add_resource(ApacheRestartFlag, '/apache-restart')
api.add_resource(APIIndex, '/')
with app.app_context():

View File

@ -1,5 +1,6 @@
Flask==2.0.0
Flask-RESTful==0.3.9
flask-cors==3.0.10
jinja2==2.11.3
PyYAML==5.4.1
pathvalidate==2.4.1