rest-server run
- web server¶
This is the core feature of PyFake-API-Server. It does 2 things:
- Set up web application with the API from the detail settings of configuration.
- Run the web application by SGI server.
UML¶
- The sub-command line processor
SubCmdRun
would use functionsetup_wsgi
orsetup_asgi
to run the web application. - All the way to run web application by factory pattern in PyFake-API-Server.
- The functions as factory callee to set up web application is
create_flask_app
andcreate_fastapi_app
. - Functions
create_flask_app
orcreate_fastapi_app
would use adapterMockHTTPServer
to set up all APIs as Python code with Python web framework Flask or FastAPI.
Extension¶
If you have your own customize Python web framework, you also could extend this features by your own one.
Here would demonstrate how to extend it to implement your own web server.
First, the entire web server should be divided to 2 parts:
- Server implementation from Python web framework
- Server gateway interface (a.k.a SGI) server
They mean you should extend all below classes to implement:
-
For setting up web application by generating Python code
BaseAppServer
-
For running web application by SGI server
BaseSGIServer
BaseCommandOption
Don't forget it also needs to import the Python web framework into PyFake-API-Server to let it could generate Python code about APIs with configuration.
- Import web library
2 Things you need to implement: importing the web framework and check importing the web framework.
fake_api_server._utils.importing | |
---|---|
Importing way may be different with different web framework
The importing way should be based on how to use the customized Python web framework.
BaseAppServer
Extend the web application feature about how PyFake-API-Server should set up it? How to initial the web application by the customized Python web framework? How to add new API by the customized web framework?
BaseSGIServer
Implement how to run web application by your own customized Python web framework. In exactly, it just generates a command line with options.
fake_api_server.server.rest.sgi.cmd | |
---|---|
BaseCommandOption
Previous one implement the command line entry point, here implement each options how to set it.
Now, we have done the core implementation, then we just leave some utility functions which we need to add.
- Utility function in module
fake_api_server.server.sgi.__init__
Please take a look at the code line 10, it's the key line to let SGI server to catch which factory function it should use to generate the web application. Here usage should base on which way should use by your own customized Python web framework.
- Utility function in module
fake_api_server.server.__init__
The global variable foo_app
is the variable which web application instance will be saved at. Function create_foo_app
is
the factory function to generate web application. Function setup_foosgi
is the one which runs the web application which be
set up by your own customized Python web framework.
- Add option value in one specific function in module
fake_api_server.command.process
Finally, we need to add a new value to let option --app-type
could recognize and dispatch it to set up and run the web
application by your own customized Python web framework.
All things you need to do is done! Let's try to run the command line to test its feature:
If you could keep observing the log message which be generated by web application as you expect, congratulation you extend the feature successfully!