-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·118 lines (98 loc) · 3.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import unittest
import coverage
from flask_script import Manager, Command, Option
from flask_migrate import Migrate, MigrateCommand
from src.app import app, db
class GunicornServer(Command):
"""Run the app with gunicorn server"""
def __init__(self, host='0.0.0.0', port=80, workers=2, timeout=0):
self.host = host
self.port = port
self.workers = workers
self.timeout = timeout
def get_options(self):
return (
Option('-H', '--host',
dest='host',
default=self.host),
Option('-p', '--port',
dest='port',
type=int,
default=self.port),
Option('-w', '--workers',
dest='workers',
type=int,
default=self.workers),
Option('-t', '--timeout',
dest='timeout',
type=int,
default=self.timeout)
)
def __call__(self, app, host, port, workers, timeout):
from gunicorn import version_info
if version_info < (0, 9, 0):
from gunicorn.arbiter import Arbiter
from gunicorn.config import Config
arbiter = Arbiter(Config(
{'bind': '%s:%d' % (host, int(port)), 'workers': workers}
), app)
arbiter.run()
else:
from gunicorn.app.base import Application
class FlaskApplication(Application):
def init(self, parser, opts, args):
return {
'bind': '{0}:{1}'.format(host, port)
}
def load(self):
return app
FlaskApplication().run()
APP_COVERAGE = coverage.coverage(
branch=True,
include='src/*',
omit=[
'src/app/config.py',
'src/tests/*',
'src/app/*/__init__.py',
'src/app/api/*/__init__.py*'
]
)
APP_COVERAGE.start()
manager = Manager(app)
migrate = Migrate(app, db, compare_type=True)
# do migration
manager.add_command('migrasi', MigrateCommand)
# start gunicorn server
manager.add_command('gunicorn', GunicornServer())
@manager.command
def test():
"""Run unit test without coverage"""
tests = unittest.TestLoader().discover('src/tests', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
return 1
@manager.command
def test_cov():
"""Run unit test with coverage"""
tests = unittest.TestLoader().discover('src/tests', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=3).run(tests)
if result.wasSuccessful():
APP_COVERAGE.stop()
APP_COVERAGE.save()
print('Coverage Summary:')
APP_COVERAGE.report()
basedir = os.path.abspath(os.path.dirname(__file__))
cov_dir = os.path.join(basedir, 'tmp/coverage')
APP_COVERAGE.html_report(directory=cov_dir)
print('HTML Version: file://%s/index.html' % cov_dir)
APP_COVERAGE.erase()
return 0
return 1
@manager.command
def buat_db():
"""Buat database"""
db.create_all()
if __name__ == '__main__':
manager.run()