2015-12-23 23:58:16 +01:00
|
|
|
#!/usr/bin/env python3
|
2015-12-24 20:34:34 +01:00
|
|
|
import bottle
|
2015-12-23 23:58:16 +01:00
|
|
|
from bottle.ext import sqlalchemy
|
2015-12-24 20:34:34 +01:00
|
|
|
from sqlalchemy import create_engine, event
|
|
|
|
from sqlalchemy.engine import Engine
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
import database
|
|
|
|
import routes
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
# Initialize db and include the SQLAlchemy plugin in bottle
|
|
|
|
engine = create_engine('sqlite:///:memory:', echo=True)
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
app = bottle.Bottle()
|
|
|
|
plugin = sqlalchemy.Plugin(
|
|
|
|
# SQLAlchemy engine created with create_engine function.
|
|
|
|
engine,
|
|
|
|
# SQLAlchemy metadata, required only if create=True.
|
|
|
|
database.Base.metadata,
|
|
|
|
# Keyword used to inject session database in a route (default 'db').
|
|
|
|
keyword='db',
|
|
|
|
# If it is true, execute `metadata.create_all(engine)` when plugin is
|
|
|
|
# applied (default False).
|
|
|
|
create=True,
|
|
|
|
# If it is true, plugin commit changes after route is executed (default
|
|
|
|
# True).
|
|
|
|
commit=True,
|
|
|
|
# If it is true and keyword is not defined, plugin uses **kwargs argument
|
|
|
|
# to inject session database (default False).
|
|
|
|
use_kwargs=False
|
|
|
|
)
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
app.install(plugin)
|
2015-12-23 23:58:16 +01:00
|
|
|
|
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
# Auto enable foreign keys for SQLite
|
|
|
|
@event.listens_for(Engine, "connect")
|
|
|
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
|
|
|
cursor = dbapi_connection.cursor()
|
|
|
|
cursor.execute("PRAGMA foreign_keys=ON")
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
|
|
|
# Routes
|
|
|
|
app.get("/papers", callback=routes.get.fetch_papers)
|
|
|
|
app.get("/papers/<id:int>", callback=routes.get.fetch_by_id)
|
|
|
|
|
|
|
|
# TODO: Fetch relationships
|
|
|
|
|
|
|
|
|
|
|
|
app.post("/papers", callback=routes.post.create_paper)
|
|
|
|
|
|
|
|
# TODO: Update relationships
|
2015-12-23 23:58:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-12-24 20:34:34 +01:00
|
|
|
app.run(host='localhost', port=8080, debug=True)
|