[PYTHON] The highlight of REST + Normal Site construction with Flask, Eve

One of Flask's derivatives is Eve, which makes it easy to build REST services. As long as you design the schema, it will deploy the REST API to SiteRoot, but it feels like Django + REST Framework + angular, and when I tried to build it, it got stuck so I'll keep a record.

REST API Root changes

In the case of Eve, API Root = Root is usually set, so index.html cannot be displayed when root is accessed. Therefore,

settings.py


URL_PREFIX = 'api'

You can change the API route by defining a prefix.

Flask and Eve settings

There are separate settings for Eve and Flask. If Eve is not specified, it will read settings.py in the same directory. If you want to read it separately

app=EVE(settings='hoge.py')

If you write, I will go to read that. However, settings only accepts dict and file.

Other than the settings used by Eve, writing them in settings.py shouldn't work ... Flask settings need to be separated and given in appFactory such as create_app ().

app/__init__.py


def create_app(api_settings=None, flask_config='oss_app.config', package_name=__name__, flask_conf_override=None):

    # Define the WSGI application object
    if api_settings is not None:
        app = Eve(settings=api_settings, import_name=__name__)
    else:
        app = Eve()

    # Configurations
    app.config.from_object(flask_config)
    app.config.from_object(flask_conf_override)

static path In the case of Eve, if you hit / static with no settings, 404 will be returned. The reason is in eve's package flaskapp.py

flaskapp.py


class Eve(Flask, Events):

Omission

    def __init__(self, import_name=__package__, settings='settings.py',
                 validator=Validator, data=Mongo, auth=None, redis=None,
                 url_converters=None, json_encoder=None,
                 media=GridFSMediaStorage, **kwargs):

It has become. When Flask searches for static_root_path, it searches for import_name if there is import_name, so import_name = __ package__ = EVE, and the EVE directory in site-package is recognized as the satticfile directory, and 404 is returned. As a countermeasure, set import_name = \ name when creating an Eve instance.

app=Eve(import_name=__name__)

Use Eve-mongo engine.

In the case of Eve, pymongo is used in the initial state, and if you want to use ORM, you can use Eve-mongo engine. However, probably because the update has stopped for about a year, when I use it, it drops as etag error in the startup sequence. Etag has become mandatory from some version of Eve, but I think it hasn't caught up. About this Etag definition can be added around the 102nd and 190th lines of eve-mongoengine \ __ init.py.

eve-mongoengine\__init_.py



91:
    def _parse_config(self):
        # parse app config
        config = self.app.config
        try:
            self.last_updated = config['LAST_UPDATED']
        except KeyError:
            self.last_updated = '_updated'
        try:
            self.date_created = config['DATE_CREATED']
        except KeyError:
            self.date_created = '_created'
+        try:
+            self.etag = config['ETAG']
+        except KeyError:
+            self.etag = '_etag'

    def fix_model_class(self, model_cls):

Omission
192:
        date_field_cls = mongoengine.DateTimeField
+        string_field_cls = mongoengine.StringField

        # field names have to be non-prefixed
        last_updated_field_name = self.last_updated.lstrip('_')
+        date_created_field_name = self.date_created.lstrip('_')
        etag__field_name = self.etag.lstrip('_')
        new_fields = {
            # TODO: updating last_updated field every time when saved
            last_updated_field_name: date_field_cls(db_field=self.last_updated,
                                                    default=get_utc_time),
            date_created_field_name: date_field_cls(db_field=self.date_created,
                                                    default=get_utc_time),
             etag__field_name: string_field_cls(db_field=self.etag,
                                                    default=""),
        }

Recommended Posts

The highlight of REST + Normal Site construction with Flask, Eve
Flask site construction memorandum
Run the app with Flask + Heroku
Automatic posting of web design gary site with python + selenium (1) Environment construction
Understand the benefits of the Django Rest Framework
Align the size of the colorbar with matplotlib
I summarized the folder structure of Flask
Check the existence of the file with python
The third night of the loop with for
About the Normal Equation of Linear Regression
Let's summarize the construction of NFS server
The second night of the loop with for
Download the file with PHP [Under construction]
Count the number of characters with echo