前提・実現したいこと
PythonにてWEB APIを作成しております。
(処理内容はまだからっぽですが。。。)
現在は下記のように一つのファイルにコードを記載しておりますが、
ファイルをjson定義、実処理等に分割したいと考えています。
いろいろなサイトみてもなにがいいのかがよくわからず、
下記の内容のソースであればどのようなディレクトリ構造、ファイル構造がいいのでしょうか。
該当のソースコード
Python
1from functools import wraps 2 3from flask import Flask, render_template, jsonify, request, url_for, abort, Response, json 4from flask_restplus import Api, Resource, fields, Namespace 5from flask_sqlalchemy import SQLAlchemy 6 7# API情報を指定して初期化 8app = Flask(__name__) 9 10#日本語の文字化け回避 11app.config['JSON_AS_ASCII'] = False 12 13#DB接続情報 14app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@localhost/develop' 15db = SQLAlchemy(app) 16 17#APIリファレンス情報 18api = Api(app, version='0.1', title='API Document', description='', doc='/doc/', 19 default='Example', default_label='default label') 20 21# Userテーブル操作 Namespaceの初期化と登録 22user_namespace = Namespace('user', description='Userのエンドポイント') 23# ホーム&ログイン&ログアウト Namespaceの初期化と登録 24login_namespace = Namespace('login', description='Loginのエンドポイント') 25 26#Userテーブル操作の登録 27api.add_namespace(user_namespace) 28#ホーム&ログイン&ログアウトの登録 29api.add_namespace(login_namespace) 30 31#jsonが送信されているかチェック 32def jsonCheck(content_type): 33 def _jsonCheck(function): 34 @wraps(function) 35 def __jsonCheck(*argv, **keywords): 36 if request.headers['Content-Type'] != content_type: 37 abort(400) 38 return function(*argv, **keywords) 39 return __jsonCheck 40 return _jsonCheck 41 42#userテーブル 43class user(db.Model): 44 __tablename__ = "user" 45 id = db.Column(db.Integer, primary_key=True) 46 username = db.Column(db.String(100), nullable=False) 47 password = db.Column(db.String(100), nullable=False, default=False) 48 49#JSONモデルの定義 user 50user = login_namespace.model('User', { 51 'username': fields.String( 52 required=True, 53 description='ログインユーザ名', 54 example='username' 55 ), 56 'password': fields.String( 57 required=True, 58 description='ログインパスワード', 59 example='password' 60 ) 61}) 62 63#JSONモデルの定義 status 64status = login_namespace.model('Status', { 65 'status': fields.String( 66 required=True, 67 description='ステータス', 68 example='status' 69 ) 70}) 71 72@user_namespace.route('/user') 73class user(Resource): 74 def get(self): 75 """ 76 ユーザを取得する 77 """ 78 return render_template('index.html') 79 80 @jsonCheck('application/json') 81 def post(self): 82 """ 83 ユーザを登録する 84 """ 85 return render_template('index.html') 86 87 @jsonCheck('application/json') 88 def put(self): 89 """ 90 ユーザを更新する 91 """ 92 return render_template('index.html') 93 94 @jsonCheck('application/json') 95 def delete(self): 96 """ 97 ユーザを削除する 98 """ 99 return render_template('index.html') 100 101@login_namespace.route('/user') 102class home(Resource): 103 def get(self): 104 """ 105 トップページを取得する 106 ログインセッションが張られていない場合、303 See Otherを返す 107 """ 108 return render_template('index.html') 109 110@login_namespace.route('/auth/login') 111class login(Resource): 112 def get(self): 113 """ 114 ログインページ取得を行う 115 ログインページを返す 116 """ 117 return render_template('login.html') 118 119 @login_namespace.expect(user, validate=True) 120 @login_namespace.marshal_with(status) 121 @jsonCheck('application/json') 122 # userモデルを利用して結果をパースして単体で返す 123 def post(self): 124 """ 125 ログインを行う 126 ログインに成功した場合、レスポンスのCookieに有効なセッション情報を設定して返す 127 (以降ログイン中は、リクエストヘッダーのCookieにこのセッション情報を指定する必要がある) 128 """ 129 #jsonを受け取る 130 username = request.json['username'] 131 password = request.json['password'] 132 print(username) 133 print(password) 134 135 #ユーザテーブルを検索する 136 137 #存在した場合、ログインOK 138 139 #存在しない場合、ログイン失敗 140 141 return ""
あなたの回答
tips
プレビュー