add uwsgi deploy and sqlalchemy execu customized sql with json return
Showing
6 changed files
with
95 additions
and
6 deletions
| ... | @@ -12,12 +12,15 @@ from flask_restful import Resource | ... | @@ -12,12 +12,15 @@ from flask_restful import Resource |
| 12 | 12 | ||
| 13 | class UserResource(Resource): | 13 | class UserResource(Resource): |
| 14 | def get(self): | 14 | def get(self): |
| 15 | x = user_service.test_exec_sql() | ||
| 15 | return "server started" | 16 | return "server started" |
| 16 | 17 | ||
| 17 | 18 | ||
| 18 | class UserListResource(Resource): | 19 | class UserListResource(Resource): |
| 19 | def get(self): | 20 | def get(self): |
| 20 | parse = reqparse.RequestParser() | 21 | parse = reqparse.RequestParser() |
| 22 | parse.add_argument('begin_time', type=str, location='args', required=False) | ||
| 23 | parse.add_argument('end_time', type=str, location='args', required=False) | ||
| 21 | parse.add_argument('name', type=str, location='args', required=False) | 24 | parse.add_argument('name', type=str, location='args', required=False) |
| 22 | parse.add_argument('role', type=str, location='args', required=False) | 25 | parse.add_argument('role', type=str, location='args', required=False) |
| 23 | args = parse.parse_args() | 26 | args = parse.parse_args() | ... | ... |
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | from server.log import log | 3 | from server.log import log |
| 4 | 4 | import json | |
| 5 | 5 | ||
| 6 | class SQLAlchemyAdapterMetaClass(type): | 6 | class SQLAlchemyAdapterMetaClass(type): |
| 7 | @staticmethod | 7 | @staticmethod |
| ... | @@ -129,8 +129,15 @@ class SQLAlchemyAdapter(DBAdapter): | ... | @@ -129,8 +129,15 @@ class SQLAlchemyAdapter(DBAdapter): |
| 129 | # ------------------------------ auto wrapped 'public' methods --- end ------------------------------ | 129 | # ------------------------------ auto wrapped 'public' methods --- end ------------------------------ |
| 130 | 130 | ||
| 131 | def exec_sql(self, sql_str): | 131 | def exec_sql(self, sql_str): |
| 132 | """ | ||
| 133 | return a json/dict list object [{},{},{}] | ||
| 134 | |||
| 135 | :param sql_str: execute sql str | ||
| 136 | :return: | ||
| 137 | """ | ||
| 132 | try: | 138 | try: |
| 133 | self.session().execte(sql_str) | 139 | result = self.session().execute(sql_str) |
| 140 | return [{key: value for (key, value) in row.items()} for row in result] | ||
| 134 | except Exception as ex: | 141 | except Exception as ex: |
| 135 | log.error(ex) | 142 | log.error(ex) |
| 136 | return None | 143 | return None | ... | ... |
| ... | @@ -37,18 +37,22 @@ class UserService(object): | ... | @@ -37,18 +37,22 @@ class UserService(object): |
| 37 | def get_user_list(self, filter_json): | 37 | def get_user_list(self, filter_json): |
| 38 | conditions = [1 == 1] | 38 | conditions = [1 == 1] |
| 39 | 39 | ||
| 40 | if 'begin_time' in filter_json: | 40 | if filter_json['begin_time']: |
| 41 | conditions.append(User.create_time > filter_json['begin_time']) | 41 | conditions.append(User.create_time > filter_json['begin_time']) |
| 42 | if 'end_time' in filter_json: | 42 | if filter_json['end_time']: |
| 43 | conditions.append(User.create_time > filter_json['end_time']) | 43 | conditions.append(User.create_time > filter_json['end_time']) |
| 44 | if 'role' in filter_json: | 44 | if filter_json['role']: |
| 45 | conditions.append(User.role == filter_json['role']) | 45 | conditions.append(User.role == filter_json['role']) |
| 46 | if 'name' in filter_json: | 46 | if filter_json['name']: |
| 47 | conditions.append(User.name.like("%" + filter_json['name'] + "%")) | 47 | conditions.append(User.name.like("%" + filter_json['name'] + "%")) |
| 48 | 48 | ||
| 49 | user_list = db_adapter.get_all_objects(User, *conditions) | 49 | user_list = db_adapter.get_all_objects(User, *conditions) |
| 50 | return map(lambda x: x.dic(), user_list) | 50 | return map(lambda x: x.dic(), user_list) |
| 51 | 51 | ||
| 52 | def test_exec_sql(self): | ||
| 53 | sql_str = "select id from user" | ||
| 54 | return db_adapter.exec_sql(sql_str) | ||
| 55 | |||
| 52 | # --------------- helper private functions ---------------------# | 56 | # --------------- helper private functions ---------------------# |
| 53 | 57 | ||
| 54 | def __generate_api_token(self, user): | 58 | def __generate_api_token(self, user): | ... | ... |
uwsgi/app.uwsgi
0 → 100644
uwsgi/uwsgi_config.ini
0 → 100644
| 1 | [uwsgi] | ||
| 2 | base = /home/hubian/WebService-python-framework | ||
| 3 | pythonpath = /home/hubian/WebService-python-framework | ||
| 4 | chdir = /home/hubian/WebService-python-framework | ||
| 5 | |||
| 6 | enable-threads = true | ||
| 7 | http = 0.0.0.0:7788 | ||
| 8 | |||
| 9 | chmod-socket = 666 | ||
| 10 | |||
| 11 | wsgi-file = /home/hubian/WebService-python-framework/uwsgi/app.uwsgi | ||
| 12 | callable = web_app | ||
| 13 | |||
| 14 | logto = /var/log/uwsgi/uwsgi.log | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
uwsgi/uwsgi_for_flask
0 → 100644
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | ### BEGIN INIT INFO | ||
| 4 | # Provides: uwsgi-emperor | ||
| 5 | # Required-Start: $all | ||
| 6 | # Required-Stop: $all | ||
| 7 | # Default-Start: 2 3 4 5 | ||
| 8 | # Default-Stop: 0 1 6 | ||
| 9 | # Short-Description: starts the uwsgi emperor app server | ||
| 10 | # Description: starts uwsgi app server using start-stop-daemon | ||
| 11 | ### END INIT INFO | ||
| 12 | |||
| 13 | # | ||
| 14 | # modified from https://gist.github.com/asmallteapot/1633492#file-init_uwsgi-sh | ||
| 15 | # | ||
| 16 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | ||
| 17 | DAEMON=/usr/local/bin/uwsgi | ||
| 18 | |||
| 19 | NAME=uwsgi-emperor | ||
| 20 | DESC=uwsgi-emperor | ||
| 21 | |||
| 22 | # modify as needed | ||
| 23 | VASALS=/opt/open-hackathon/nginx_openhackathon.uwsgi.ini | ||
| 24 | EMPEROR_LOGS=/var/log/uwsgi/*.log | ||
| 25 | |||
| 26 | test -x $DAEMON || exit 0 | ||
| 27 | |||
| 28 | # Include uwsgi defaults if available | ||
| 29 | if [ -f /etc/default/uwsgi ] ; then | ||
| 30 | . /etc/default/uwsgi | ||
| 31 | fi | ||
| 32 | |||
| 33 | set -e | ||
| 34 | |||
| 35 | DAEMON_OPTS="--emperor $VASALS --die-on-term --master --daemonize $EMPEROR_LOGS" | ||
| 36 | |||
| 37 | case "$1" in | ||
| 38 | start) | ||
| 39 | echo -n "Starting $DESC: " | ||
| 40 | start-stop-daemon --start --exec $DAEMON -- $DAEMON_OPTS | ||
| 41 | echo "$NAME." | ||
| 42 | ;; | ||
| 43 | stop) | ||
| 44 | echo -n "Stopping $DESC: " | ||
| 45 | start-stop-daemon --signal 3 --quiet --retry 2 --stop \ | ||
| 46 | --exec $DAEMON | ||
| 47 | echo "$NAME." | ||
| 48 | ;; | ||
| 49 | status) | ||
| 50 | ps -ef | grep uwsgi | ||
| 51 | ;; | ||
| 52 | *) | ||
| 53 | N=/etc/init.d/$NAME | ||
| 54 | echo "Usage: $N {start|stop|status}" >&2 | ||
| 55 | exit 1 | ||
| 56 | ;; | ||
| 57 | esac | ||
| 58 | exit 0 | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment