-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
56 lines (42 loc) · 1.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Starts the main server to host the static production build files
as well as set up the Flask-Restless API routes for the database models.
"""
import os
import flask
import flask_sqlalchemy
import flask_restless
from sqlalchemy.schema import ForeignKey
from flask_restless import APIManager
from flask import Flask, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS, cross_origin
from app.models import db, Character, Anime, Actor, Manga
BUILD_DIR = 'build'
app = Flask(__name__, static_folder=BUILD_DIR)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app/weeb.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Suppress warning
CORS(app, headers=['Content-Type']) # Enable Cross-Origin Resource Sharing
db.init_app(app)
manager = flask_restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Character, collection_name='characters', allow_functions=True, methods=['GET'], page_size=60)
manager.create_api(Anime, collection_name='animes', allow_functions=True, methods=['GET'], page_size=60)
manager.create_api(Actor, collection_name='actors', allow_functions=True, methods=['GET'], page_size=60)
manager.create_api(Manga, collection_name='mangas', allow_functions=True, methods=['GET'], page_size=60)
# Serve React App, handle all unhandled paths
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path == "":
return send_from_directory(BUILD_DIR, 'index.html')
else:
if os.path.exists(BUILD_DIR + '/' + path):
return send_from_directory(BUILD_DIR, path)
else:
return send_from_directory(BUILD_DIR, 'index.html')
# Testing purposes only
@app.route('/hello')
def hello():
return "Hello World\n"
if __name__ == '__main__':
app.run(use_reloader=True, port=5000, threaded=True)