def app(): """Create and configure a new app instance for each test.""" app = create_app({'TESTING': True, 'DATABASE': 'sqlite:///:memory:'}) # create the database and load test data with app.app_context(): init_db() yield app
def create_app(): app = Flask(__name__) app.config.from_object('api.config.Config') hello() init_db(app) api = Api(app) api.add_resource(ResultsListAPI, '/results') api.add_resource(ResultsAPI, '/results/<id>')
def create_app(): app = Flask(__name__) CORS(app) app.config.from_object("api.config.Config") init_db(app) api = Api(app) api.add_resource(UserAPI, "/api/user/<id>") api.add_resource(UsersAPI, "/api/users") return app
def register_extensions(app, worker=False): jwt = JWTManager(app) # noqa @jwt.token_in_blacklist_loader def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token['jti'] query = users.Tokens.query.filter_by(api_key=jti).first() if query is None: return False elif query.revoked is False: return False elif query.revoked is True: return True init_db(app) # load celery config celery.config_from_object(app.config) if not worker: # register celery irrelevant extensions pass
def setUp(self): self.tmp_db = tempfile.NamedTemporaryFile() self.tmp_purge_list = tempfile.NamedTemporaryFile() self.tmp_puzzle_resources = tempfile.mkdtemp() self.tmp_puzzle_archive = tempfile.mkdtemp() cookie_secret = "oatmeal" self.app = make_app( SQLITE_DATABASE_URI=self.tmp_db.name, HOSTREDIS="127.0.0.1", PORTREDIS=6379, REDIS_DB=1, REDIS_URL="redis://127.0.0.1:6379/1/", HOSTAPI="127.0.0.1", PORTAPI=6310, DEBUG=True, TESTING=True, # Ignore wal journal_mode requirement PUZZLE_RESOURCES=self.tmp_puzzle_resources, PUZZLE_ARCHIVE=self.tmp_puzzle_archive, PURGEURLLIST=self.tmp_purge_list.name, MINIMUM_PIECE_COUNT=20, MAX_POINT_COST_FOR_REBUILDING=1000, MAX_POINT_COST_FOR_DELETING=1000, BID_COST_PER_PUZZLE=100, POINT_COST_FOR_CHANGING_BIT=100, POINT_COST_FOR_CHANGING_NAME=100, NEW_USER_STARTING_POINTS=1300, POINTS_CAP=15000, SECURE_COOKIE_SECRET=cookie_secret, cookie_secret=cookie_secret, database_writable=True, ) self.db = db # TODO: set logger level to DEBUG when actively developing the tests self.app.logger.setLevel(logging.WARN) with self.app.app_context(): with self.app.test_client() as c: init_db()
# app.py import dotenv from api import create_app from api.database import init_db if __name__ == '__main__': # 환경변수 설정 dotenv.load_dotenv(dotenv_path=".env") # MongoDB 접속 및 기초 데이터 입력 init_db() # Flask App 실행 app = create_app() app.run(host="localhost", port=3000)
def setUp(self): PUZZLE_PIECE_GROUPS = list( map(int, "100 200 400 800 1600 2200 4000 60000".split())) self.tmp_db = tempfile.NamedTemporaryFile() self.tmp_purge_list = tempfile.NamedTemporaryFile() self.tmp_puzzle_resources = tempfile.mkdtemp() self.tmp_puzzle_archive = tempfile.mkdtemp() cookie_secret = "oatmeal" self.app = make_app( HOST="127.0.0.1", HOSTNAME="legacy_puzzle_massive", PORT=6300, HOSTCACHE="127.0.0.1", HOSTORIGIN="127.0.0.1", HOSTPUBLISH="127.0.0.1", PORTPUBLISH=6311, HOSTDIVULGER="127.0.0.1", PORTDIVULGER=6320, HOSTSTREAM="127.0.0.1", PORTSTREAM=6321, CHILL_DATABASE_URI="sqlite:////var/lib/puzzle-massive/sqlite3/db", PUBLIC_URL_PREFIX="/site", ROOT_FOLDER="root", DOCUMENT_FOLDER="documents", MEDIA_FOLDER="media", MEDIA_PATH="/media/", THEME_STATIC_FOLDER="dist", PACKAGEJSON={ "version": "0", "author": "Beaker" }, VERSION="0", THEME_STATIC_PATH="/theme/0/", THEME_TEMPLATE_FOLDER="templates", THEME_SQL_FOLDER="queries", CACHE_NO_NULL_WARNING=True, CACHE_TYPE="null", FREEZER_DESTINATION="frozen", FREEZER_BASE_URL="http://legacy_puzzle_massive/", UNSPLASH_APPLICATION_ID="", UNSPLASH_APPLICATION_NAME="", UNSPLASH_SECRET="", SUGGEST_IMAGE_LINK="", ENVIRONMENT="development", NEW_PUZZLE_CONTRIB="rizzo", SMTP_HOST="", SMTP_PORT="", SMTP_USER="", SMTP_PASSWORD="", EMAIL_SENDER="", EMAIL_MODERATOR="", PUBLISH_WORKER_COUNT=2, STREAM_WORKER_COUNT=2, PUZZLE_PIECES_CACHE_TTL=20, MAX_RECENT_POINTS=25, RECENT_POINTS_EXPIRE=1209600, INITIAL_KARMA=10, MAX_KARMA=25, KARMA_POINTS_EXPIRE=3600, BLOCKEDPLAYER_EXPIRE_TIMEOUTS=list( map(int, "10 30 300 600 1200 2400 3600".split())), MAXIMUM_PIECE_COUNT=50000, PUZZLE_PIECE_GROUPS=PUZZLE_PIECE_GROUPS, ACTIVE_PUZZLES_IN_PIECE_GROUPS=list( map(int, "40 20 10 10 5 5 5 5".split())), MINIMUM_IN_QUEUE_PUZZLES_IN_PIECE_GROUPS=list( map(int, "6 6 2 2 1 1 1 1".split())), SKILL_LEVEL_RANGES=list( zip([0] + PUZZLE_PIECE_GROUPS, PUZZLE_PIECE_GROUPS)), MINIMUM_TO_CLAIM_ACCOUNT=1400, BIT_ICON_EXPIRATION=dict( map( lambda x: [int(x[:x.index(":")]), x[1 + x.index(":"):].strip()], """ 0: 2 days, 1: 4 days, 50: 14 days, 400: 1 months, 800: 4 months, 1600: 8 months """.split(","), )), PIECE_MOVE_TIMEOUT=4, MAX_PAUSE_PIECES_TIMEOUT=15, TOKEN_LOCK_TIMEOUT=5, TOKEN_EXPIRE_TIMEOUT=60 * 5, PLAYER_BIT_RECENT_ACTIVITY_TIMEOUT=10, PIECE_JOIN_TOLERANCE=100, AUTO_APPROVE_PUZZLES=True, LOCAL_PUZZLE_RESOURCES=True, CDN_BASE_URL="http://localhost:38685", PUZZLE_RESOURCES_BUCKET_REGION="local", PUZZLE_RESOURCES_BUCKET_ENDPOINT_URL= "http://s3fake.puzzle.massive.test:4568", PUZZLE_RESOURCES_BUCKET="chum", PUZZLE_RESOURCES_BUCKET_OBJECT_CACHE_CONTROL= "public, max-age:31536000, immutable", PUZZLE_RULES={"all"}, PUZZLE_FEATURES={"all"}, SHOW_OTHER_PLAYER_BITS=True, DOMAIN_NAME="puzzle.massive.test", SITE_TITLE="Test Puzzle Massive", HOME_PAGE_ROUTE="/chill/site/front/", SOURCE_CODE_LINK="https://github.com/jkenlooper/puzzle-massive/", M3="", SQLITE_DATABASE_URI=self.tmp_db.name, HOSTREDIS="127.0.0.1", PORTREDIS=6379, REDIS_DB=1, REDIS_URL="redis://127.0.0.1:6379/1/", HOSTAPI="127.0.0.1", PORTAPI=6310, DEBUG=True, TESTING=True, # Ignore wal journal_mode requirement PUZZLE_RESOURCES=self.tmp_puzzle_resources, PUZZLE_ARCHIVE=self.tmp_puzzle_archive, PURGEURLLIST=self.tmp_purge_list.name, MINIMUM_PIECE_COUNT=20, MAX_POINT_COST_FOR_REBUILDING=1000, MAX_POINT_COST_FOR_DELETING=1000, BID_COST_PER_PUZZLE=100, POINT_COST_FOR_CHANGING_BIT=100, POINT_COST_FOR_CHANGING_NAME=100, NEW_USER_STARTING_POINTS=1300, POINTS_CAP=15000, REWARD_INSTANCE_SLOT_SCORE_THRESHOLD=0, SECURE_COOKIE_SECRET=cookie_secret, cookie_secret=cookie_secret, database_writable=True, ) self.db = db # TODO: set logger level to DEBUG when actively developing the tests # self.app.logger.setLevel(logging.WARN) self.app.logger.setLevel( logging.DEBUG if self.debug else logging.CRITICAL) with self.app.app_context(): with self.app.test_client() as c: init_db()
#!/usr/bin/env python2 # coding=utf-8 from api.database import init_db if __name__ == '__main__': init_db()
import os from dotenv import load_dotenv from flask import Flask from flask_restful import Api from flask_cors import CORS from api.controllers import Clima, Cities from api.database import init_db app = Flask(__name__) load_dotenv() environment_configuration = os.getenv('CONFIGURATION_SETUP', "None") app.config.from_object(environment_configuration) init_db(app) CORS(app) api = Api(app) api.add_resource(Clima, '/clima', '/clima') api.add_resource(Cities, '/cidades', '/cidades') if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)
app = Flask(__name__) app.config.from_object('settings') @app.route('/') def main(): return jsonify(status=200,\ code='http://github.com/teampopong/popong-api', issues='http://github.com/teampopong/popong-api/issues', documents='%s/#api' % settings.API_DOCS_URL) @app.route('/<version>/') def api_docs(version): if version in versions: return jsonify(status=200, documents='%s/api/%s' % (settings.API_DOCS_URL, version)) else: return jsonify(status=404, message="Please check the API version."), 404 @app.errorhandler(401) def error_401(error): return jsonify(status=401, message="Did you add an api_key?"), 401 @app.errorhandler(404) def error_404(error): return jsonify(status=404, exception="That request is not valid."), 404 init_db(app) register_api_v0_1(app) register_api_v0_2(app)