File size: 1,483 Bytes
9b76a89 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import os
import sys
import falcon
from falcon_cors import CORS
from .user import UserResource, UsersEndpoint
from .validator import Validator
from .utils import error_serializer
from .db import DB
from .cfg import Config
# pick the config directory
config_dir = os.environ.get('OWNJSON_CONFIG_DIR', None)
if config_dir is None:
prefix = os.environ.get('XDG_CONFIG_DIR')
if prefix is None:
prefix = os.environ.get('HOME')
config_dir = os.path.join(prefix, '.config/ownjson')
# load config
config = Config()
ok = config.load(config_dir)
if not ok:
sys.exit(1)
# set up the validator object
schema_dir = os.path.join(config_dir, 'server_schemas')
server_validator = Validator()
for name in config.config['server_schemas']:
server_validator.add_schema(name, os.path.join(schema_dir, '%s.schema' % (name) ))
uri = config.get('db_uri')
db_name = config.get('db_name')
db = DB(uri, db_name)
# data to send to kwargs for each resource
data = {
'config': config.config,
'server_validator': server_validator,
'db': db
}
# set up the resources
users = UserResource(**data)
user_endpoint = UsersEndpoint(**data)
cors = CORS(allow_all_origins=True, allow_all_methods=True,
allow_headers_list=['accept', 'content-type', 'authorization'])
# set up the wsgi app and add routes
app = falcon.API(middleware=[
cors.middleware
])
app.set_error_serializer(error_serializer)
app.add_route('/api/{username}', users)
app.add_route('/api/users', user_endpoint)
|