f6af342a by 胡边

add uwsgi deploy and sqlalchemy execu customized sql with json return

1 parent a3ee277d
......@@ -12,12 +12,15 @@ from flask_restful import Resource
class UserResource(Resource):
def get(self):
x = user_service.test_exec_sql()
return "server started"
class UserListResource(Resource):
def get(self):
parse = reqparse.RequestParser()
parse.add_argument('begin_time', type=str, location='args', required=False)
parse.add_argument('end_time', type=str, location='args', required=False)
parse.add_argument('name', type=str, location='args', required=False)
parse.add_argument('role', type=str, location='args', required=False)
args = parse.parse_args()
......
# -*- coding: utf-8 -*-
from server.log import log
import json
class SQLAlchemyAdapterMetaClass(type):
@staticmethod
......@@ -129,8 +129,15 @@ class SQLAlchemyAdapter(DBAdapter):
# ------------------------------ auto wrapped 'public' methods --- end ------------------------------
def exec_sql(self, sql_str):
"""
return a json/dict list object [{},{},{}]
:param sql_str: execute sql str
:return:
"""
try:
self.session().execte(sql_str)
result = self.session().execute(sql_str)
return [{key: value for (key, value) in row.items()} for row in result]
except Exception as ex:
log.error(ex)
return None
......
......@@ -37,18 +37,22 @@ class UserService(object):
def get_user_list(self, filter_json):
conditions = [1 == 1]
if 'begin_time' in filter_json:
if filter_json['begin_time']:
conditions.append(User.create_time > filter_json['begin_time'])
if 'end_time' in filter_json:
if filter_json['end_time']:
conditions.append(User.create_time > filter_json['end_time'])
if 'role' in filter_json:
if filter_json['role']:
conditions.append(User.role == filter_json['role'])
if 'name' in filter_json:
if filter_json['name']:
conditions.append(User.name.like("%" + filter_json['name'] + "%"))
user_list = db_adapter.get_all_objects(User, *conditions)
return map(lambda x: x.dic(), user_list)
def test_exec_sql(self):
sql_str = "select id from user"
return db_adapter.exec_sql(sql_str)
# --------------- helper private functions ---------------------#
def __generate_api_token(self, user):
......
import sys
sys.path.append("/home/hubian/WebService-python-framework")
from server.app import web_app
\ No newline at end of file
[uwsgi]
base = /home/hubian/WebService-python-framework
pythonpath = /home/hubian/WebService-python-framework
chdir = /home/hubian/WebService-python-framework
enable-threads = true
http = 0.0.0.0:7788
chmod-socket = 666
wsgi-file = /home/hubian/WebService-python-framework/uwsgi/app.uwsgi
callable = web_app
logto = /var/log/uwsgi/uwsgi.log
\ No newline at end of file
#!/bin/sh
### BEGIN INIT INFO
# Provides: uwsgi-emperor
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi emperor app server
# Description: starts uwsgi app server using start-stop-daemon
### END INIT INFO
#
# modified from https://gist.github.com/asmallteapot/1633492#file-init_uwsgi-sh
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/uwsgi
NAME=uwsgi-emperor
DESC=uwsgi-emperor
# modify as needed
VASALS=/opt/open-hackathon/nginx_openhackathon.uwsgi.ini
EMPEROR_LOGS=/var/log/uwsgi/*.log
test -x $DAEMON || exit 0
# Include uwsgi defaults if available
if [ -f /etc/default/uwsgi ] ; then
. /etc/default/uwsgi
fi
set -e
DAEMON_OPTS="--emperor $VASALS --die-on-term --master --daemonize $EMPEROR_LOGS"
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --signal 3 --quiet --retry 2 --stop \
--exec $DAEMON
echo "$NAME."
;;
status)
ps -ef | grep uwsgi
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|status}" >&2
exit 1
;;
esac
exit 0
\ No newline at end of file
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!