-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_Flask.sh
290 lines (232 loc) · 7.58 KB
/
template_Flask.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
################################################################################
# Create Project
################################################################################
# provide sudo for the following commands (like chmod,...) and exit if wrong password or ctrl + c
if ! sudo true;
then
exit 1
fi
#check project name
if [ $1 ];
then
PROJECT_NAME="$1" # Store project name in a variable
mkdir "$PROJECT_NAME" # Use variable and double quotes
else
echo 'No project name'
exit 1
fi
cd "$PROJECT_NAME" # Use variable and double quotes
# WHERE TO RUN PROJECT
cat > README.txt << EOF
- ALWAYS RUN PROJECT FROM HERE ( NOT IN src/ FOLDER) TO MAKE ALL LINK OF IMPORT FILE CORRECT
- Run file start.sh to run project
EOF
################################################################################
# git init
################################################################################
################################################################################
# Check virtual environment folder and requirements
################################################################################
######################################
# Check virtual environment folder
######################################
FLASK_ENV_FOLDER="flask_env"
python3 -m venv "$FLASK_ENV_FOLDER"
source "$FLASK_ENV_FOLDER/bin/activate"
############################in########
# install requirements
######################################
pip install flask
################################################################################
# Create templates for project
################################################################################
#######################################
# Create requirements.txt
#######################################
touch requirements.txt
#######################################
# create source folder
#######################################
mkdir src
cd src
#######################################
# Create models folđer
#######################################
mkdir models
#######################################
# Create template folder
#######################################
mkdir templates
#######################################
# Create static folder
#######################################
mkdir static
#######################################
# create constant folder
#######################################
mkdir constants
cat > constants/__init__.py << EOF
__all_ = ["http_status_code"]
EOF
cat > constants/http_status_code.py << EOF
HTTP_200_OK = 200
HTTP_201_CREATED = 201
HTTP_202_ACCEPTED = 202
HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203
HTTP_204_NO_CONTENT = 204
HTTP_205_RESET_CONTENT = 205
HTTP_206_PARTIAL_CONTENT = 206
HTTP_207_MULTI_STATUS = 207
HTTP_208_ALREADY_REPORTED = 208
HTTP_226_IM_USED = 226
HTTP_300_MULTIPLE_CHOICES = 300
HTTP_301_MOVED_PERMANENTLY = 301
HTTP_302_FOUND = 302
HTTP_303_SEE_OTHER = 303
HTTP_304_NOT_MODIFIED = 304
HTTP_305_USE_PROXY = 305
HTTP_306_RESERVED = 306
HTTP_307_TEMPORARY_REDIRECT = 307
HTTP_308_PERMANENT_REDIRECT = 308
HTTP_400_BAD_REQUEST = 400
HTTP_401_UNAUTHORIZED = 401
HTTP_402_PAYMENT_REQUIRED = 402
HTTP_403_FORBIDDEN = 403
HTTP_404_NOT_FOUND = 404
HTTP_405_METHOD_NOT_ALLOWED = 405
HTTP_406_NOT_ACCEPTABLE = 406
HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407
HTTP_408_REQUEST_TIMEOUT = 408
HTTP_409_CONFLICT = 409
HTTP_410_GONE = 410
HTTP_411_LENGTH_REQUIRED = 411
HTTP_412_PRECONDITION_FAILED = 412
HTTP_413_REQUEST_ENTITY_TOO_LARGE = 413
HTTP_414_REQUEST_URI_TOO_LONG = 414
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416
HTTP_417_EXPECTATION_FAILED = 417
HTTP_422_UNPROCESSABLE_ENTITY = 422
HTTP_423_LOCKED = 423
HTTP_424_FAILED_DEPENDENCY = 424
HTTP_426_UPGRADE_REQUIRED = 426
HTTP_428_PRECONDITION_REQUIRED = 428
HTTP_429_TOO_MANY_REQUESTS = 429
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451
HTTP_500_INTERNAL_SERVER_ERROR = 500
HTTP_501_NOT_IMPLEMENTED = 501
HTTP_502_BAD_GATEWAY = 502
HTTP_503_SERVICE_UNAVAILABLE = 503
HTTP_504_GATEWAY_TIMEOUT = 504
HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505
HTTP_506_VARIANT_ALSO_NEGOTIATES = 506
HTTP_507_INSUFFICIENT_STORAGE = 507
HTTP_508_LOOP_DETECTED = 508
HTTP_509_BANDWIDTH_LIMIT_EXCEEDED = 509
HTTP_510_NOT_EXTENDED = 510
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511
def is_informational(status):
# 1xx
pass
def is_success(status):
# 2xx
pass
def is_redirect(status):
# 3xx
pass
def is_client_error():
# 4xx
pass
def is_server_error():
# 5xx
pass
EOF
#######################################
# create App
#######################################
touch app_config.py
cat > app_config.py << EOF
EOF
# create __init__.py file
cat > "__init__.py" << END_TEXT
from flask import Flask
from flask.json import jsonify
from src.constants.http_status_code import *
def create_app(config_filename=None):
# create app object
app = Flask(__name__)
if config_filename is None:
# app.config.from_mapping (
# SECRET_KEY=os.environ.get("SECRET_KEY"),
# SQLALCHEMY_DATABASE_URI=os.environ.get("SQLALCHEMY_DB_URI"),
# SQLALCHEMY_TRACK_MODIFICATIONS=False,
# JWT_SECRET_KEY=os.environ.get('JWT_SECRET_KEY'),
# SWAGGER={
# 'title': "Bookmarks API",
# 'uiversion': 3
# }
# )
pass
else:
app.config.from_mapping(config_filename)
# connect database
# from yourapplication.model import db
# db.init_app(app)
# register blueprint
# from yourapplication.views.admin import admin
# from yourapplication.views.frontend import frontend
# app.register_blueprint(admin)
# app.register_blueprint(frontend)
@app.route('/')
def main_page():
return "Hello world!"
@app.errorhandler(HTTP_404_NOT_FOUND)
def handle_404(e):
return jsonify({'error': 'Not found'}), HTTP_404_NOT_FOUND
@app.errorhandler(HTTP_500_INTERNAL_SERVER_ERROR)
def handle_500(e):
return jsonify({'error': 'Something went wrong, we are working on it'}), HTTP_500_INTERNAL_SERVER_ERROR
return app
END_TEXT
#######################################
# create module folder
#######################################
# create module folder
mkdir modules
cat > modules/__init__.py << EOF
from . import *
EOF
cat > modules/readme.txt << EOF
If you want to create new flask module, create new folder
Each module have 4 default python files
- __init__.py => default file that helps python find each module (always need __all__)
- [module_name]_controller.py => define blueprint, route (@[blueprint].route) and call function from [module_nanme]_service.py files to solve request
- [module_nanme]_service.py => define function to solve request
- [module_name]_config.py => config of blueprint in module
EOF
################################################################################
# Create script to run project
################################################################################
SCRIPT_DIR='
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
cd "$SCRIPT_DIR"'
SCRIPT_COMMAND="source $FLASK_ENV_FOLDER/bin/activate
flask --app src run"
cat > ../start.sh << EOF
#!/bin/bash
$SCRIPT_DIR
$SCRIPT_COMMAND
EOF
sudo chmod u+x ../start.sh
################################################################################
# DONE
################################################################################
echo "DONE"
################################################################################
# Run the first time to check errors
################################################################################
cd ..
sudo ./start.sh