Skip to main content

Easy to Make Flask Server

Project description

easy-flask

Automatic generate a flask server project by command

Start

Install

pip3 install easy-flask-restful

Make a flask server project at current dir:

easy-flask -n demo_server
# or
easy-flask --new=demo_server

# start server in debug mode
cd demo_server
python3 app.py
# or
sh control.sh start|restart|stop

Introduce

By this tool you don`t need care how to make a flask server project. What you need to do is write api-file in easy format inside dir api. This project will automatically load uri by api-file, and pack json response with same format.

Project structure

.
├── api
│   ├── __init__.py
│   └── demo
│       ├── __init__.py
│       └── demo_api.py
├── conf
│   ├── __init__.py
│   ├── config.ini
├── internal
│   ├── __init__.py
│   ├── error.py
│   └── utils.py
├── app.py
├── start.py
├── control.sh
└── logs
    └── app.log

api - write api files here.

internal.error - define error info here.

app.py - start app by this file, you can rename it.

conf - config for application

control.sh - run app with command by gunicorn

Write Api File

For example:

from flask import g

from internal import Resource
from utils.parser import Type, Required, Default, Min, Max


class DemoApi(Resource):

    uri = ['/api/demo']

    def get(self):
        pattern = {
            'num': {Type: int, Min: 10, Max: 100},
            'print': {Type: str, Required: True},
            'default': {Type: str, Default: 'demo'}
        }

        data, err = self.parse_request_data(pattern=pattern)
        if err:
            g.logger.warning(err)
            return err
        g.logger.info('success')
        return {'result': data}

uri - required by all api file

g.logger is loaded before each request for making different log_id.

self.parse_request_data can help you to parse params, it will return ParamsError when param invalid.

Normal response:

{
    "errno": 0,
    "data": {
        "result": {
            "num": 100,
            "print": "aaaa",
            "default": "demo"
        }
    },
    "msg": "success",
    "log_id": "f40c889d1b5744c7a87a9045aea8595c"
}

Error response:

return errno and msg according to error.py.

{
    "errno": 2,
    "msg": "params error:num check max invalid",
    "log_id": "7c53531b93a4406e9d3050bae5b99968"
}

Suggest

A real project must have complex logic and interactions with DB or other service. By this tool, you just can get a simple framework. So I give some suggestions here for writing better code.

  • logic dir (or other name) for writing complex logic, keep simple in api file.
  • if you need db operation, use flask-sqlalchemy, and mkdir dto.
  • client dir for interact with other service, and make BaseClient class(inherited by other client), because you never want to write same code for many times.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy-flask-restful-1.1.5.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

easy_flask_restful-1.1.5-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file easy-flask-restful-1.1.5.tar.gz.

File metadata

  • Download URL: easy-flask-restful-1.1.5.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for easy-flask-restful-1.1.5.tar.gz
Algorithm Hash digest
SHA256 221e79110dead09accc8cc5a16a40999a68a6c649ce4f883f16bd7d0bdae9162
MD5 d1be22a5c1a36816eb3b7f726b99b07b
BLAKE2b-256 b81c8dab2eb2bdf28d295ee231253823c278dda4e9eab080778a488fa2a50d74

See more details on using hashes here.

File details

Details for the file easy_flask_restful-1.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_flask_restful-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 978562fe83b2ef1fc8bb3981442f8ed017afc1576faa8af5f9b71aa6dc01f8bf
MD5 a582f19544fe53d0dbbcf9cf284fcf31
BLAKE2b-256 19cee87cd01c6308e51e74e79c24c2a475bb76843e7916843322cd4ddc35cd71

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page