Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Creates and runs an Falcon application.
4"""
5import os
6import sys
7import logging
8import falcon
11def start_mlreststor(location='.', host='127.0.0.1', port=8081, name='ml', nostart=False,
12 wsgi='waitress', secret='', users='', algo='sha224',
13 fLOG=print):
14 """
15 Creates an :epkg:`falcon` application and
16 runs it through a :epkg:`wsgi` server.
17 The appplication stores machine learned models and runs predictions assuming
18 all the necessary packages were installed.
20 :param name: only one option is implemented 'ml'
21 :param location: location of the storage
22 :param host: host
23 :param port: port
24 :param nostart: do not start the wsgi server (for debug purpose)
25 :param wsgi: :epkg:`wsgi` framework which runs the falcon application
26 :param secret: secret used to encrypt the logging, default is empty which
27 disables the encryption
28 :param users: list of authorized users stored
29 in a text file with two columns:
30 login and encrypted password
31 :param algo: algorithm used to encrypt the passwords
32 :param fLOG: logging function
34 Only :epkg:`waitress` is implemented right now.
35 Other alternative such as :epkg:`mod_wsgi` with :epkg:`Apache`.
36 :epkg:`uwsgi` are not implemented. Parameter *users* can be empty
37 to disable authentification.
39 .. cmdref::
40 :title: Creates an falcon application to store machine learned models
41 :cmd: -m lightmlrestapi start_mlreststor --help
42 :lid: cmd_start_mlreststor_cmd
44 Creates an :epkg:`falcon` application and starts it through a :epkg:`wsgi` server.
45 The appplication stores machine learned models and runs predictions assuming
46 all the necessary packages were installed.
47 """
48 try:
49 from ..mlapp.mlstorage_rest import MLStoragePost
50 from ..mlapp.authfiction import AuthMiddleware
51 except (ImportError, ValueError):
52 folder = os.path.normpath(os.path.join(
53 os.path.abspath(os.path.dirname(__file__)), "..", ".."))
54 sys.path.append(folder)
55 from lightmlrestapi.mlapp.mlstorage_rest import MLStoragePost
56 from lightmlrestapi.mlapp.authfiction import AuthMiddleware
58 # Secrets.
59 if not secret:
60 secret = None
61 if not users:
62 users = None
63 if name not in {'ml'}:
64 raise ValueError("Name '{0}' is not recognized.".format(name))
66 # Authenfication.
67 if users:
68 if fLOG:
69 fLOG("[start_mlreststor] enable authentification")
70 middleware = [AuthMiddleware(users, algo=algo)]
71 app = falcon.API(middleware=middleware)
72 url_scheme = 'https'
73 else:
74 app = falcon.API()
75 url_scheme = 'http'
77 # REST API
78 location = os.path.abspath(location)
79 if not os.path.exists(location):
80 raise FileNotFoundError("Unable to find '{0}'".format(location))
81 rest = MLStoragePost(secret=secret, folder=location,
82 folder_storage=location, version=None)
84 app.add_route("/", rest)
86 # Server.
87 if wsgi is not None:
88 if wsgi == 'waitress':
89 from waitress import serve
90 if not nostart:
91 fLOG("[start_mlreststor] serve(app, host='{}', port='{}', url_scheme='{}')".format(
92 host, port, url_scheme))
93 logger = logging.getLogger('waitress')
94 logger.setLevel(logging.INFO)
95 serve(app, host=host, port=port, url_scheme=url_scheme)
96 else:
97 fLOG("[start_mlreststor] do not run serve(app, host='{}', port='{}')".format(
98 host, port))
99 else:
100 raise NotImplementedError(
101 "Server '{}' is not implemented.".format(wsgi))
103 return app