-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
92 lines (76 loc) · 3.65 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Demo script for setting up Hydrus with any db and any API Doc."""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker,scoped_session
from hydrus.utils import Getter_setter
from hydrus.app import app_factory
from hydrus.data import doc_parse
from hydrus.hydraspec import doc_maker
from hydrus.data.db_models import Base
from hydrus.data.user import add_user
from doc import doc
from gevent.wsgi import WSGIServer
if __name__ == "__main__":
# The database connection URL
# See http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlalchemy.create_engine for more info
# DB_URL = 'sqlite:///database.db'
DB_URL = 'sqlite:///:memory:'
# Define the server URL, this is what will be displayed on the Doc
HYDRUS_SERVER_URL = "http://localhost:8080/"
# The name of the API or the EntryPoint, the api will be at http://localhost/<API_NAME>
API_NAME = "serverapi"
print("Setting up the database")
# Create a connection to the database you want to use
engine = create_engine(DB_URL)
print("Creating models")
# Add the required Models to the database
Base.metadata.create_all(engine)
# Define the Hydra API Documentation
# NOTE: You can use your own API Documentation and create a HydraDoc object using doc_maker
# Or you may create your own HydraDoc Documentation using doc_writer [see hydrus/hydraspec/doc_writer_sample]
print("Creating the API Documentation")
apidoc = doc_maker.create_doc(doc, HYDRUS_SERVER_URL, API_NAME)
# Start a session with the DB and create all classes needed by the APIDoc
session = scoped_session(sessionmaker(bind=engine))
print("Adding Classes and Properties")
# Get all the classes from the doc
classes = doc_parse.get_classes(apidoc.generate()) # You can also pass dictionary defined in hydrus/hydraspec/doc_writer_sample_output.py
# Get all the properties from the classes
properties = doc_parse.get_all_properties(classes)
# Insert them into the database
doc_parse.insert_classes(classes, session)
doc_parse.insert_properties(properties, session)
print("Adding authorized users")
add_user(id_=1, paraphrase="test", session=session)
# Insert them into the database
doc_parse.insert_classes(classes, session)
doc_parse.insert_properties(properties, session)
getter_setter = Getter_setter(session, HYDRUS_SERVER_URL, API_NAME, apidoc, True)
print("Creating the application")
# Create a Hydrus app with the API name you want, default will be "api"
app = app_factory(API_NAME, getter_setter)
# Set the name of the API
print("Starting the application")
http_server = WSGIServer(('', 8080), app)
print("Server running at:")
print(HYDRUS_SERVER_URL + API_NAME)
try:
http_server.serve_forever()
except KeyboardInterrupt:
pass
# with set_authentication(app, True):
# # Use authentication for all requests
# with set_api_name(app, "serverapi"):
# # Set the API Documentation
# with set_doc(app, apidoc):
# # Set HYDRUS_SERVER_URL
# with set_hydrus_server_url(app, HYDRUS_SERVER_URL):
# # Set the Database session
# with set_session(app, session):
# # Start the Hydrus app
# http_server = WSGIServer(('', 8080), app)
# print("Server running at:")
# print(HYDRUS_SERVER_URL + API_NAME)
# try:
# http_server.serve_forever()
# except KeyboardInterrupt:
# pass