Created
July 27, 2015 16:59
-
-
Save MizukiSonoko/d923b22afcdc91999c6b to your computer and use it in GitHub Desktop.
適当なREST API in Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ubuntu | |
MAINTAINER mizuki <mizuki.sonoko@gmail.com> | |
RUN apt-get update | |
RUN apt-get install -y build-essential | |
RUN apt-get install -y python-dev | |
RUN apt-get install -y python3-pip | |
RUN pip3 install PyMySQL3 | |
RUN pip3 install SQLAlchemy | |
RUN pip3 install flask | |
RUN pip3 install Flask-SQLAlchemy | |
RUN pip3 install Flask-API | |
RUN pip3 install simplejson | |
RUN mkdir workspace | |
ADD index.py /workspace/index.py | |
EXPOSE 80 8080 6543 | |
CMD ["/usr/bin/python3","/workspace/index.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simplejson | |
from flask import Flask, request | |
from flask.ext.sqlalchemy import SQLAlchemy | |
from flask.ext.api import status | |
dbscheme = # TODO | |
dbhost = # TODO | |
dbuser = # TODO | |
dbpass = # TODO | |
dbname = # TODO | |
dboption = 'charset=utf8' | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = dbscheme + '://' + dbuser + ':' + dbpass + '@' + dbhost + '/' +dbname + '?' + dboption | |
db = SQLAlchemy(app) | |
class Datas(db.Model): | |
id = db.Column(db.INT, primary_key=True) | |
name = db.Column(db.TEXT) | |
desc = db.Column(db.TEXT) | |
created_time = db.Column(db.DATETIME) | |
def __init__(self, name, desc): | |
self.desc = desc | |
self.name = name | |
def __repr__(self): | |
return "<Data %r>" % self.name | |
def NoDataResponse(): | |
return simplejson.dumps( dict( | |
error = dict( code = "404", message = "Not found.") | |
)), status.HTTP_404_NOT_FOUND | |
@app.errorhandler(404) | |
def page_not_found(error): | |
return NoDataResponse() | |
@app.route("/get", methods=["GET"]) | |
def get(): | |
name = request.args.get("name",None) | |
if name: | |
return getData(name) | |
return simplejson.dumps( dict( error = dict( code = "403", message = "Bad request."))) , status.HTTP_400_BAD_REQUEST | |
def getData(name): | |
data = Datas.query.filter_by( name = name).first() | |
if data: | |
return simplejson.dumps([ | |
dict( | |
code = "200", | |
id = data.id, | |
name = data.name, | |
desc = data.desc)]) | |
return getNoData() | |
@app.route('/') | |
def home(): | |
return simplejson.dumps( | |
dict( | |
code = "200", | |
title = "Information", | |
message = "Hello! This is api by mizuki." | |
) | |
), status.HTTP_200_OK | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment