Browse Source

* WireGuard servers should now automatically start if last state was running before shutdown.

pull/5/head
Per-Arne 5 years ago
parent
commit
c28636d5aa
  1. 23
      wg_dashboard_backend/main.py

23
wg_dashboard_backend/main.py

@ -1,12 +1,14 @@
import logging
import os
import typing
from sqlalchemy_utils import database_exists
from starlette.middleware.base import BaseHTTPMiddleware
import middleware
from database import engine, SessionLocal
from routers.v1 import user, server, peer, wg
import script.wireguard
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
if not logger.hasHandlers():
@ -27,10 +29,13 @@ import models
app = FastAPI()
app.add_middleware(BaseHTTPMiddleware, dispatch=middleware.db_session_middleware)
_db: Session = SessionLocal()
# Ensure database existence
if not database_exists(engine.url):
models.Base.metadata.create_all(engine)
# Create default user
_db: Session = SessionLocal()
_db.add(models.User(
username=os.getenv("ADMIN_USERNAME", "admin"),
password=middleware.get_password_hash(os.getenv("ADMIN_PASSWORD", "admin")),
@ -39,6 +44,16 @@ if not database_exists(engine.url):
email=""
))
_db.commit()
servers: typing.List[models.WGServer] = _db.query(models.WGServer).all()
for s in servers:
try:
last_state = s.is_running
if script.wireguard.is_installed() and last_state and not script.wireguard.is_running(s):
script.wireguard.start_interface(s)
except Exception as e:
print(e)
_db.close()
@ -86,13 +101,11 @@ def root():
app.mount("/", StaticFiles(directory=pkg_resources.resource_filename(__name__, 'build')), name="static")
@app.on_event("startup")
async def startup():
pass
@app.on_event("shutdown")
async def shutdown():
pass

Loading…
Cancel
Save