Debian Codegraph @

networking_bagpipe/bagpipe_bgp/api/api.py

raw
1 # Copyright 2017 Orange
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from oslo_config import cfg
16 import pecan
17 import socketserver
18 from wsgiref import simple_server
19
20
21 ROOT_CTRL = 'networking_bagpipe.bagpipe_bgp.api.controllers.RootController'
22
23
24 def setup_app(*args, **kwargs):
25 config = {
26 'server': {
27 'port': cfg.CONF.API.port,
28 'host': cfg.CONF.API.host,
29 },
30 'app': {
31 'root': ROOT_CTRL,
32 }
33 }
34 pecan_config = pecan.configuration.conf_from_dict(config)
35
36 app = pecan.make_app(
37 pecan_config.app.root,
38 debug=False,
39 force_canonical=False,
40 guess_content_type_from_ext=True
41 )
42
43 return app
44
45
46 class ThreadedSimpleServer(socketserver.ThreadingMixIn,
47 simple_server.WSGIServer):
48 pass
49
50
51 class PecanAPI:
52
53 def __init__(self):
54
55 app = setup_app()
56
57 self.wsgi = simple_server.make_server(
58 cfg.CONF.API.host,
59 cfg.CONF.API.port,
60 app,
61 server_class=ThreadedSimpleServer
62 )
63
64 def run(self):
65 self.wsgi.serve_forever()
66
67 def stop(self):
68 # call stop on RootController
69 self.wsgi.get_app().application.root.stop()
70
71
72 def main():
73 api = PecanAPI()
74 api.run()
75
76
77 if __name__ == "__main__":
78 main()