You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
6 years ago
|
#!/usr/bin/env python
|
||
|
|
||
5 years ago
|
import owntracker.functions as lambda_functions
|
||
|
import owntracker.config as app_config
|
||
6 years ago
|
|
||
|
from wsgiref.simple_server import make_server
|
||
|
from pyramid.view import view_config, view_defaults
|
||
|
from pyramid.config import Configurator
|
||
|
from configparser import ConfigParser
|
||
|
from pathlib import Path
|
||
|
|
||
|
|
||
6 years ago
|
# Generate routes and views and build the app
|
||
6 years ago
|
def configure_app():
|
||
|
with Configurator() as config:
|
||
5 years ago
|
# Generate simple index route
|
||
|
config.add_route("index", "/")
|
||
|
config.add_view(
|
||
|
lambda_functions.index,
|
||
|
route_name="index",
|
||
|
renderer="json"
|
||
|
)
|
||
|
|
||
6 years ago
|
# Generate routes for devices
|
||
6 years ago
|
for device_name in app_config.devices:
|
||
|
device_key = app_config.devices[device_name]
|
||
|
config.add_route(
|
||
|
device_name,
|
||
6 years ago
|
"/device/{}".format(device_key)
|
||
|
)
|
||
|
config.add_route(
|
||
|
"{}-status".format(device_name),
|
||
|
"/status/{}".format(device_name)
|
||
6 years ago
|
)
|
||
|
config.add_view(
|
||
|
lambda_functions.ingest,
|
||
|
route_name=device_name,
|
||
6 years ago
|
renderer="json"
|
||
|
)
|
||
|
config.add_view(
|
||
|
lambda_functions.status,
|
||
|
route_name="{}-status".format(device_name),
|
||
|
renderer="json"
|
||
6 years ago
|
)
|
||
5 years ago
|
|
||
|
# Return the app
|
||
6 years ago
|
app = config.make_wsgi_app()
|
||
|
return app
|
||
6 years ago
|
|
||
|
|
||
|
# API Gateway/Zappa function handler
|
||
|
def generate_wsgi_app(app, environ):
|
||
|
wsgi_app = configure_app()
|
||
|
return wsgi_app(app, environ)
|
||
|
|
||
|
|
||
|
# Local web server for development
|
||
6 years ago
|
if __name__ == "__main__":
|
||
6 years ago
|
print("[+] Starting local web server on port 8080...")
|
||
6 years ago
|
server = make_server("0.0.0.0", 8080, configure_app())
|
||
6 years ago
|
server.serve_forever()
|