Skip to content

Commit 71dfe59

Browse files
author
kevin-hans
committed
init
0 parents  commit 71dfe59

16 files changed

+301
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/__pycache__
2+
/venv

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Flask",
9+
"type": "python",
10+
"request": "launch",
11+
"module": "flask",
12+
"env": {
13+
"FLASK_APP": "app.py",
14+
"FLASK_ENV": "development"
15+
},
16+
"args": [
17+
"run",
18+
"--no-debugger"
19+
],
20+
"jinja": true
21+
}
22+
]
23+
}

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.9.10-slim-buster
2+
3+
ADD ./ /usr/src/app
4+
5+
WORKDIR /usr/src/app
6+
ENV FLASK_APP=app
7+
8+
#COPY /app/requirements.txt ./
9+
RUN apt update \
10+
&& apt install -y gcc
11+
RUN pip install --upgrade pip
12+
RUN pip install -r requirements.txt
13+
14+
EXPOSE 5000
15+
16+
ENTRYPOINT [ "uwsgi", "--ini", "/usr/src/app/uwsgi.ini" ]

ReadMe.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# build image
2+
docker build -t flask-api .
3+
4+
# start container from image
5+
docker run -d -p 5000:5000 --name flask-api flask-api
6+
7+
# start container from image, when connecting exterl db outside this container
8+
docker run -d -p 5000:5000 -e DB_URI='host.docker.internal' --name flask-api flask-api
9+
10+
# start postgres
11+
docker run -d -e POSTGRES_USER='user' \
12+
-e POSTGRES_PASSWORD='pass' \
13+
-e POSTGRES_DB='dbname' \
14+
-p 5432:5432 --name postgres postgres:latest
15+
16+
17+
# modify python file on hot container
18+
docker cp ./app.py flask-api:/usr/src/app
19+
docker restart flask-api

app.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import imp
2+
from flask import Flask
3+
from flask import jsonify
4+
from flask_cors import CORS, cross_origin
5+
import logging
6+
from model.user import User
7+
from model.photo import Photo
8+
from globalapp import app
9+
from flask import Response
10+
import json
11+
from utils.AlchemyEncoder import AlchemyEncoder
12+
13+
logger = logging.getLogger()
14+
logger.setLevel(logging.INFO)
15+
logging.getLogger('flask_cors').level = logging.DEBUG
16+
17+
18+
@app.route("/")
19+
@cross_origin()
20+
def hello_world():
21+
return "<p>Hello, world!</p>"
22+
23+
@app.route("/json")
24+
@cross_origin()
25+
def hello_json():
26+
return jsonify({ "message": "日本語" })
27+
28+
@app.route("/user/list")
29+
@cross_origin()
30+
def getUserList1():
31+
return Response(json.dumps(User.query.all(), cls=AlchemyEncoder))
32+
33+
@app.route("/photo/list")
34+
@cross_origin()
35+
def getPhotoList():
36+
return Response(json.dumps(Photo.query.all(), cls=AlchemyEncoder))
37+

dev_tool/initdb.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from globalapp import db
2+
from model.user import User
3+
from model.photo import Photo
4+
5+
db.create_all()
6+
7+
db.session.add(User(lastName = 'Snow', firstName= 'Jon', age = 35))
8+
db.session.add(User(lastName = 'Lannister', firstName = 'Cersei', age = 42))
9+
db.session.add(User(lastName = 'Lannister', firstName = 'Jaime', age = 45))
10+
db.session.add(User(lastName = 'Stark', firstName = 'Arya', age = 16))
11+
db.session.add(User(lastName = 'Targaryen', firstName = 'Daenerys', age = 6))
12+
db.session.add(User(lastName = 'Melisandre', firstName = 'test', age = 150))
13+
db.session.add(User(lastName = 'Clifford', firstName = 'Ferrara', age = 44))
14+
db.session.add(User(lastName = 'Frances', firstName = 'Rossini', age = 36))
15+
db.session.add(User(lastName = 'Roxie', firstName = 'Harvey', age = 65))
16+
17+
db.session.add(Photo(img = 'https://images.unsplash.com/photo-1551963831-b3b1ca40c98e', \
18+
title ='Breakfast', \
19+
rows = 2, \
20+
cols = 2))
21+
22+
23+
db.session.add(Photo( \
24+
img = 'https://images.unsplash.com/photo-1551782450-a2132b4ba21d', \
25+
title = 'Burger', \
26+
))
27+
db.session.add(Photo( \
28+
img = 'https://images.unsplash.com/photo-1522770179533-24471fcdba45', \
29+
title = 'Camera', \
30+
))
31+
db.session.add(Photo( \
32+
img = 'https://images.unsplash.com/photo-1444418776041-9c7e33cc5a9c', \
33+
title = 'Coffee', \
34+
cols = 2, \
35+
))
36+
db.session.add(Photo( \
37+
img = 'https://images.unsplash.com/photo-1533827432537-70133748f5c8', \
38+
title = 'Hats', \
39+
cols = 2, \
40+
))
41+
db.session.add(Photo( \
42+
img = 'https://images.unsplash.com/photo-1558642452-9d2a7deb7f62', \
43+
title = 'Honey', \
44+
author = '@arwinneil', \
45+
rows = 2, \
46+
cols = 2, \
47+
))
48+
db.session.add(Photo( \
49+
img = 'https://images.unsplash.com/photo-1516802273409-68526ee1bdd6', \
50+
title = 'Basketball', \
51+
))
52+
db.session.add(Photo( \
53+
img = 'https://images.unsplash.com/photo-1518756131217-31eb79b20e8f', \
54+
title = 'Fern', \
55+
))
56+
db.session.add(Photo( \
57+
img = 'https://images.unsplash.com/photo-1597645587822-e99fa5d45d25', \
58+
title = 'Mushrooms', \
59+
rows = 2, \
60+
cols = 2, \
61+
))
62+
db.session.add(Photo( \
63+
img = 'https://images.unsplash.com/photo-1567306301408-9b74779a11af', \
64+
title = 'Tomato basil', \
65+
))
66+
db.session.add(Photo( \
67+
img = 'https://images.unsplash.com/photo-1471357674240-e1a485acb3e1', \
68+
title = 'Sea star', \
69+
))
70+
db.session.add(Photo( \
71+
img = 'https://images.unsplash.com/photo-1589118949245-7d38baf380d6', \
72+
title = 'Bike', \
73+
cols = 2, \
74+
))
75+
76+
77+
db.session.commit()

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "3"
2+
services:
3+
app:
4+
build: .
5+
ports:
6+
- "5000:5000"
7+
container_name: app
8+
command: flask run --host=0.0.0.0

globalapp.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
5+
6+
app = Flask(__name__)
7+
8+
# Json
9+
app.config['JSON_AS_ASCII'] = False
10+
11+
# DB
12+
_db_user = os.environ.get("DB_USER", default="user")
13+
_db_pass = os.environ.get("DB_PASS", default="pass")
14+
_db_uri = os.environ.get("DB_URI", default="localhost")
15+
_db_name = os.environ.get("DB_NAME", default="dbname")
16+
17+
_full_db_uri = 'postgresql://{0}:{1}@{2}/{3}'.format(_db_user, _db_pass, _db_uri, _db_name)
18+
19+
# app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://user:pass@localhost/dbname'
20+
app.config['SQLALCHEMY_DATABASE_URI'] = _full_db_uri
21+
db = SQLAlchemy(app)

model/__init__.py

Whitespace-only changes.

model/photo.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from globalapp import db
2+
3+
class Photo(db.Model):
4+
id = db.Column("id", db.Integer, primary_key=True)
5+
img = db.Column("img", db.String(1000), unique=False, nullable=False)
6+
title = db.Column("title", db.String(120), unique=False, nullable=False)
7+
author = db.Column("author", db.String(120), unique=False, nullable=True)
8+
rows = db.Column("rows", db.Integer, unique=False, nullable=True)
9+
cols = db.Column("cols", db.Integer, unique=False, nullable=True)
10+
11+
def __repr__(self):
12+
return '<Photo %r %r >' % (self.title ,self.img)

0 commit comments

Comments
 (0)