简体   繁体   中英

How to structure a Flask app generated with Swagger

I have a module that performs one function and would like to expose this function through an API specified with Swagger.

I used the Swagger codegen to generate a Flask application. I found the thing_controller.py file and know that I need to link the my backend in that file, where the codegen put the line return 'do some magic!

I was wondering how to best structure this application. I would like to have:

  1. flask-server-dir to keep all the code relevant to the API (so only the code generated by Swagger)
  2. src dir that contains all code of the backend.

I would like to be able to import src/thing in the thing_controller.py file. That way I can call return thing.func() to perform all operations needed.

I created a python module to create swagger + jsonapi services: https://github.com/thomaxxl/safrs

If you run the demo, it creates an example method (send_mail) that can be invoked from the swagger UI: https://github.com/thomaxxl/safrs/blob/master/examples/demo.py

If you make it a classmethod, you won't need object instances.

This solution may not fit your requirements thoug, but it's like 5min work to get it running

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM