$ vi api.py

 ------------------------------------------------


from flask import Flask

from flask_restful import Resource, Api

from flask_restful import reqparse

from flaskext.mysql import MySQL


app = Flask(__name__)


api = Api(app)


'''INPUT

{"email": "gyunseul9@gmail.com","username": "gyunseul9","password": "gyunseul10"}

END'''


#mysql connection

mysql = MySQL()

app.config['MYSQL_DATABASE_USER'] = 'USER_ID'

app.config['MYSQL_DATABASE_PASSWORD'] = 'PASSWORD'

app.config['MYSQL_DATABASE_DB'] = 'DATABASE'

app.config['MYSQL_DATABASE_HOST'] = 'DATABASE_ENDPOINT'

mysql.init_app(app)


class BlankPage(Resource):

    def get(self):

        return {'status':'200'}


class CreateUser(Resource):

    def post(self):

        #return {'status':'success'}

        try:

            parser = reqparse.RequestParser()

            parser.add_argument('email', type=str)

            parser.add_argument('username', type=str)

            parser.add_argument('password', type=str)

            args = parser.parse_args()


            _userEmail = args['email']

            _userName = args['username']

            _userPassword = args['password']


            conn = mysql.connect()

            cursor = conn.cursor()

            cursor.callproc('sp_create_user', (_userEmail, _userName, _userPassword))

            data = cursor.fetchall()


            if len(data) is 0:

                conn.commit()

                return {'StatusCode': '200', 'Message': 'User creation success!!!'}

            else:

                return {'StatusCode': '1000', 'Message': str(data[0])}

            #return {'Email': args['email'], 'UserName': args['user_name'], 'Password': args['password']}

        except Exception as e:

            return {'error': str(e)}


api.add_resource(BlankPage,'/')


api.add_resource(CreateUser,'/user')


if __name__ == '__main__':

    app.run('0.0.0.0',debug=True)

---------------------------------


$ vi user.sql

---------------------------------

CREATE TABLE user (

user_id INT NOT NULL AUTO_INCREMENT,

email VARCHAR(80) NULL,

username VARCHAR(45) NULL,

password VARCHAR(45) NULL,

PRIMARY KEY (user_id));

---------------------------------


$ vi sp_create_user.sql

---------------------------------

REATE PROCEDURE `sp_create_user`(

    IN p_email varchar(80),

    IN p_username varchar(45),

    IN p_password varchar(45)

)

BEGIN

    IF ( select exists (select 1 from user where username = p_username) ) THEN

            select 'Username Exists !!';

    ELSE

        insert into user (

             email,

                username,

                password

        ) values (

         p_email,

            p_username,

            p_password

    );

END IF;

END

--------------------------------

'Python' 카테고리의 다른 글

ubuntu, goormide nginx+uwsgi  (0) 2018.03.11
heroku 설정 방법  (0) 2018.03.07
ubuntu, supervisor+uwsgi+nginx+python3  (0) 2018.02.27
ubuntu, flask restful  (0) 2018.02.26
AWS Cloud, Python, Use sshtunneling to store rds data in a local database  (0) 2018.02.25
Posted by 앤비
,

$ service supervisor stop

$ ps aux | grep uwsgi

$ kill -9 PID

$ rm /tmp/uwsgi.sock

$ vi /etc/supervisor/supervisor.conf
---
[inet_http_server]
port = 0.0.0.0:9001
username = future
password = future_01
---

$ vi /etc/supervior/conf.d/service.conf
---
[program:service]
command=/home/service/aws_flask/run.sh
environment=HOME='/home/service',USER='service'
directory=/home/service/aws_flask
autostart=true
autorestart=true
startretries=3
---

$ vi /home/service/aws_service/env/uwsgi.ini
---
[uwsgi]
chdir=/home/service/aws_flask/
chmod-socket=666
callable=app
module=app
socket=/tmp/uwsgi.sock
virtualenv=/home/service/aws_flask

master=true
processes=5
max-requests=1000
harakiri=10
lazy-apps=true
logto=/var/log/uwsgi/uwsgi.log
enable-threads = true
---

vi /home/service/aws_service/run.sh
---
source /home/service/aws_service/bin/activate

exec uwsgi --ini /home/service/aws_service/env/uwsgi.ini
---

$ service nginx restart

$ cd /tmp; ls -al

$ /home/service/aws_flask/run.sh

ctrl + c

$ cd /tmp; ls -al

$ service supervisor start

$ supervisorclt

Posted by 앤비
,

ubuntu, flask restful

Python 2018. 2. 26. 11:22

$ cd /home/service

$ git clone https://gyunseul9@bitbucket.org/gyunseul9/api_flask.git

$ virtualenv api_flask

$ cd api_flask

$ . bin/activate

$ pip3 install flask

$ pip3 install flask-restful

$ pip3 install uwsgi

$ pip3 freeze > requirements.txt

$ vi api.py
---
from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)

api = Api(app)

class CreateUser(Resource):
def post(self):
return {'status':'success'}
api.add_resource(CreateUser,'/user')

if __name__ == '__main__':
app.run('0.0.0.0',debug=True)
---

$ python3 api.py

$ git add .

$ git commit -am "google, initialize api_flask"

$ git push

$ mkdir env

$ vi ./env/uwsgi_api.ini
---
[uwsgi]
chdir=/home/service/api_flask
chmod-socket=666
callable=app
module=api
socket=/tmp/uwsgi_api.sock
virtualenv=/home/service/api_flask

master=true
processes=5
max-requests=1000
harakiri=10
lazy-apps=true
logto=/var/log/uwsgi/uwsgi_api.log
enable-threads = true
---

$ vi run.sh
---
#!/bin/bash

source /home/service/api_flask/bin/activate

uwsgi --ini /home/service/api_flask/env/uwsgi_api.ini
---

$ chmod +x run.sh

$ vi /etc/nginx/sites-available/default
---
server {
listen 8080;

server_name 0.0.0.0;

location / {
try_files $uri @api;
}
location @api {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_api.sock;
}
}
---

$ vi /etc/supervisor/conf.d/api.conf
---
[program:api]
command=/home/service/api_flask/run.sh
environment=HOME='/home/service',USER='service'
directory=/home/service/api_flask
autostart=true
autorestart=true
startretries=3
---

$ service nginx restart

$ cd /tmp

$ /home/service/api_flask/run.sh

ctrl + c

$ ls -al

$ service supervisor stop

$ service supervisor start

$ supervisorctl

Posted by 앤비
,