generated from Eddie/Backend-Boilerplate
Compare commits
No commits in common. 'cd267700affac1186c5548a7054adda1dcd3e611' and '381a7d43a5d5fa3f5ca27a845b870b31817abd6e' have entirely different histories.
cd267700af
...
381a7d43a5
14 changed files with 170 additions and 37 deletions
@ -1,22 +1,13 @@ |
|||||||
import os |
from services import (UserService, UserListService) |
||||||
from flask import Flask, render_template, send_file |
from config import app, api, docs, CORS |
||||||
from planets import createRandomPlanet |
|
||||||
|
|
||||||
app = Flask(__name__) |
#*______________ Service Registration ______________ |
||||||
|
api.add_resource(UserService, '/api/user/<user_id>') |
||||||
|
docs.register(UserService) |
||||||
|
api.add_resource(UserListService, '/api/list/user') |
||||||
|
docs.register(UserListService) |
||||||
|
#*______________ Application Creation ______________ |
||||||
|
if __name__ == '__main__': |
||||||
|
app.run(debug=True) |
||||||
|
|
||||||
IMG_FOLDER = os.path.join('static', 'IMG') |
|
||||||
app.config['UPLOAD_FOLDER'] = IMG_FOLDER |
|
||||||
|
|
||||||
@app.route('/') |
|
||||||
def index(): |
|
||||||
planet = os.path.join(app.config['UPLOAD_FOLDER'], 'planet.png') |
|
||||||
return render_template('index.html', planet=planet) |
|
||||||
|
|
||||||
@app.route('/<seed>') |
|
||||||
def hello(seed): |
|
||||||
createRandomPlanet(seed) |
|
||||||
planet = os.path.join(app.config['UPLOAD_FOLDER'], 'planet.png') |
|
||||||
return send_file(planet) |
|
||||||
|
|
||||||
if __name__=='__main__': |
|
||||||
app.run(debug=True, host='0.0.0.0') |
|
@ -0,0 +1,40 @@ |
|||||||
|
from flask import Flask |
||||||
|
from flask_restful import Api |
||||||
|
from apispec import APISpec |
||||||
|
from apispec.ext.marshmallow import MarshmallowPlugin |
||||||
|
from flask_apispec.extension import FlaskApiSpec |
||||||
|
from flask_marshmallow import Marshmallow |
||||||
|
from flask_sqlalchemy import SQLAlchemy |
||||||
|
from flask_cors import CORS |
||||||
|
#!______________ App Setup _____________ _ |
||||||
|
app = Flask(__name__, static_url_path='/static') |
||||||
|
api_v1_cors_config = { |
||||||
|
"origins": ["http://localhost:5000"] |
||||||
|
} |
||||||
|
|
||||||
|
#!______________ CORS Setup _____________ _ |
||||||
|
CORS(app, resources={"/api/*": api_v1_cors_config}) |
||||||
|
|
||||||
|
#!______________ DB Setup ______________ |
||||||
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///markdown.db' |
||||||
|
app.config['SECRET_KEY'] = 'InputSecretKeyHere' |
||||||
|
db = SQLAlchemy(app) |
||||||
|
|
||||||
|
#!______________ Marshmallow Setup ______________ |
||||||
|
ma = Marshmallow(app) |
||||||
|
|
||||||
|
#!______________ API & Swagger Setup ______________ |
||||||
|
api = Api(app) |
||||||
|
app.config.update({ |
||||||
|
'APISPEC_SPEC': APISpec( |
||||||
|
title='Markdown Presenter', |
||||||
|
version='v0.0.1', |
||||||
|
plugins=[MarshmallowPlugin()], |
||||||
|
openapi_version='2.0.0' |
||||||
|
), |
||||||
|
'APISPEC_SWAGGER_URL': '/swagger/', |
||||||
|
'APISPEC_SWAGGER_UI_URL': '/swagger-ui/' |
||||||
|
}) |
||||||
|
|
||||||
|
#!______________ Docs Setup ______________ |
||||||
|
docs = FlaskApiSpec(app) |
@ -0,0 +1,10 @@ |
|||||||
|
from config import db |
||||||
|
|
||||||
|
#!______________ DB Models ______________ |
||||||
|
class User(db.Model): |
||||||
|
__tablename__ = "User" |
||||||
|
id = db.Column(db.Integer, primary_key=True) |
||||||
|
name = db.Column(db.String(50)) |
||||||
|
image = db.Column(db.String()) |
||||||
|
description = db.Column(db.String()) |
||||||
|
|
After Width: | Height: | Size: 462 KiB |
After Width: | Height: | Size: 415 KiB |
@ -0,0 +1,18 @@ |
|||||||
|
import os |
||||||
|
from config import db |
||||||
|
from termcolor import colored |
||||||
|
from config import app |
||||||
|
|
||||||
|
def RebuildDatabase(): |
||||||
|
if os.path.exists('acceth.db'): |
||||||
|
print(colored('Removing existing DB', 'blue')) |
||||||
|
os.remove('acceth.db') |
||||||
|
|
||||||
|
with app.app_context(): |
||||||
|
db.create_all() |
||||||
|
db.session.commit() |
||||||
|
|
||||||
|
print(colored('New DB created.', 'green')) |
||||||
|
|
||||||
|
if __name__ == '__main__': |
||||||
|
RebuildDatabase() |
@ -0,0 +1,25 @@ |
|||||||
|
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema |
||||||
|
from config import db |
||||||
|
from models import User |
||||||
|
from marshmallow import fields |
||||||
|
|
||||||
|
#*______________ Base Schema ______________ |
||||||
|
class BaseScheme(SQLAlchemyAutoSchema): |
||||||
|
def __str__(self): |
||||||
|
return str(self.__class__) + ": " + str(self.__dict__) |
||||||
|
class Meta: |
||||||
|
ordered = True |
||||||
|
sqla_session = db.session |
||||||
|
include_fk = True |
||||||
|
load_instance = True |
||||||
|
|
||||||
|
#*______________ User Schemes ______________ |
||||||
|
class UserSchema(BaseScheme): |
||||||
|
class Meta(BaseScheme.Meta): |
||||||
|
model = User |
||||||
|
id = fields.Int() |
||||||
|
name = fields.Str() |
||||||
|
class UserInsertSchema(UserSchema): |
||||||
|
user_id = fields.Int() |
||||||
|
class UserResponseSchema(UserSchema): |
||||||
|
name = fields.Str() |
@ -0,0 +1,46 @@ |
|||||||
|
from flask_apispec import marshal_with, doc, use_kwargs |
||||||
|
from flask_apispec.views import MethodResource |
||||||
|
from flask_restful import Resource |
||||||
|
from schemes import (UserSchema, UserResponseSchema) |
||||||
|
from config import db |
||||||
|
from models import User |
||||||
|
|
||||||
|
#!______________ User ______________ |
||||||
|
class UserService(MethodResource, Resource): |
||||||
|
@doc(description='Get User by User_id', tags=['User']) |
||||||
|
@marshal_with(UserResponseSchema) |
||||||
|
def get(self, user_id): |
||||||
|
quser = db.session.query(User).get(user_id) |
||||||
|
return UserSchema().dump(quser) |
||||||
|
|
||||||
|
@doc(description='Add new User', tags=['User']) |
||||||
|
@use_kwargs(UserSchema, location=('json')) |
||||||
|
@marshal_with(UserResponseSchema()) |
||||||
|
def post(self, user, user_id): |
||||||
|
db.session.add(user) |
||||||
|
db.session.commit() |
||||||
|
return UserSchema().dump(user) |
||||||
|
|
||||||
|
@doc(description='Update User with PUT', tags=['User']) |
||||||
|
@use_kwargs(UserSchema, location=('json')) |
||||||
|
@marshal_with(UserResponseSchema()) |
||||||
|
def put(self, user, user_id): |
||||||
|
db.session.add(user) |
||||||
|
db.session.commit() |
||||||
|
return UserSchema().dump(user) |
||||||
|
|
||||||
|
@doc(description='Delete existing User', tags=['User']) |
||||||
|
@use_kwargs(UserSchema, location=('json')) |
||||||
|
@marshal_with(UserResponseSchema()) |
||||||
|
def delete(self, user, user_id): |
||||||
|
user = db.session.query(User).get(user_id) |
||||||
|
db.session.delete(user) |
||||||
|
db.session.commit() |
||||||
|
return UserSchema().dump(user) |
||||||
|
|
||||||
|
class UserListService(MethodResource, Resource): |
||||||
|
@doc(description='Get a List of all User', tags=['List']) |
||||||
|
@marshal_with(UserResponseSchema(many=True)) |
||||||
|
def get(self): |
||||||
|
users = db.session.query(User).all() |
||||||
|
return UserSchema(many=True).dump(users) |
Before Width: | Height: | Size: 443 KiB |
@ -1,11 +0,0 @@ |
|||||||
<!DOCTYPE html> |
|
||||||
<html lang="en"> |
|
||||||
<head> |
|
||||||
<meta charset="UTF-8"> |
|
||||||
<title>Planets</title> |
|
||||||
</head> |
|
||||||
<body> |
|
||||||
<h1>Welcome to Planets</h1> |
|
||||||
<img src="{{ planet }}" alt="Planet"> |
|
||||||
</body> |
|
||||||
</html> |
|
After Width: | Height: | Size: 282 KiB |
After Width: | Height: | Size: 615 KiB |
Loading…
Reference in new issue