Skip to content

Instantly share code, notes, and snippets.

@Kruptein
Created June 17, 2019 17:27
Show Gist options
  • Save Kruptein/012eda4813b2985da05f648b0edcc968 to your computer and use it in GitHub Desktop.
Save Kruptein/012eda4813b2985da05f648b0edcc968 to your computer and use it in GitHub Desktop.
"""
PlanarAlly backend server code.
This is the code responsible for starting the backend and reacting to socket IO events.
"""
# Mimetype recognition for js files apparently is not always properly setup out of the box for some users out there.
import mimetypes
js_mime = mimetypes.guess_type(".js")[0]
if js_mime == "text/plain" or js_mime is None:
mimetypes.add_type("application/javascript", ".js")
import save
save.check_save()
import asyncio
import configparser
import sys
from aiohttp import web
import api.http
import routes
# Force loading of socketio routes
from api.socket import *
from app import app, logger, sio, state
from config import config
# This is a fix for asyncio problems on windows that make it impossible to do ctrl+c
if sys.platform.startswith("win"):
def _wakeup():
asyncio.get_event_loop().call_later(0.1, _wakeup)
asyncio.get_event_loop().call_later(0.1, _wakeup)
async def on_shutdown(_):
for sid in list(state.sid_map.keys()):
await sio.disconnect(sid, namespace="/planarally")
app.router.add_static("/static", "static")
app.router.add_get("/api/auth", api.http.auth.is_authed)
app.router.add_post("/api/login", api.http.auth.login)
app.router.add_post("/api/register", api.http.auth.register)
app.router.add_post("/api/logout", api.http.auth.logout)
app.router.add_get("/api/rooms", api.http.rooms.get_list)
app.router.add_post("/api/rooms", api.http.rooms.create)
app.router.add_post("/api/invite", api.http.claim_invite)
if "dev" in sys.argv:
app.router.add_route("*", "/{tail:.*}", routes.root_dev)
else:
app.router.add_route("*", "/{tail:.*}", routes.root)
app.on_shutdown.append(on_shutdown)
def start_http(host, port):
logger.warning(" RUNNING IN NON SSL CONTEXT ")
web.run_app(
app,
host=host,
port=config.getint("Webserver", "port"),
)
def start_https(host, port, chain, key):
import ssl
ctx = ssl.SSLContext()
try:
ctx.load_cert_chain(chain, key)
except FileNotFoundError:
logger.critical("SSL FILES ARE NOT FOUND. ABORTING LAUNCH.")
sys.exit(2)
web.run_app(
app,
host=host,
port=port,
ssl_context=ctx,
)
def start_socket(sock):
web.run_app(app, path=sock)
if __name__ == "__main__":
socket = config.get("Webserver", "socket", fallback=None)
if socket:
start_socket(socket)
else:
host = config.get("Webserver", "host")
port=config.getint("Webserver", "port")
if config.getboolean("Webserver", "ssl"):
try:
chain = config.get("Webserver", "ssl_fullchain")
key = config.get("Webserver", "ssl_privkey")
except configparser.NoOptionError:
logger.critical(
"SSL CONFIGURATION IS NOT CORRECTLY CONFIGURED. ABORTING LAUNCH."
)
sys.exit(2)
start_https(host, port, chain, key)
else:
start_http(host, port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment