refactor template directory to be artemis root dir

This commit is contained in:
Hay1tsme 2023-02-20 21:55:12 -05:00
parent db6b950c29
commit b343228072
8 changed files with 40 additions and 18 deletions

View File

@ -1,5 +1,5 @@
import logging, coloredlogs import logging, coloredlogs
from typing import Any, Dict from typing import Any, Dict, List
from twisted.web import resource from twisted.web import resource
from twisted.web.util import redirectTo from twisted.web.util import redirectTo
from twisted.web.http import Request from twisted.web.http import Request
@ -12,7 +12,6 @@ from core.data import Data
from core.utils import Utils from core.utils import Utils
class FrontendServlet(resource.Resource): class FrontendServlet(resource.Resource):
children: Dict[str, Any] = {}
def getChild(self, name: bytes, request: Request): def getChild(self, name: bytes, request: Request):
self.logger.debug(f"{request.getClientIP()} -> {name.decode()}") self.logger.debug(f"{request.getClientIP()} -> {name.decode()}")
if name == b'': if name == b'':
@ -24,7 +23,9 @@ class FrontendServlet(resource.Resource):
log_fmt_str = "[%(asctime)s] Frontend | %(levelname)s | %(message)s" log_fmt_str = "[%(asctime)s] Frontend | %(levelname)s | %(message)s"
log_fmt = logging.Formatter(log_fmt_str) log_fmt = logging.Formatter(log_fmt_str)
self.logger = logging.getLogger("frontend") self.logger = logging.getLogger("frontend")
self.environment = jinja2.Environment(loader=jinja2.FileSystemLoader("core/frontend")) self.environment = jinja2.Environment(loader=jinja2.FileSystemLoader("."))
self.game_list: List[Dict[str, str]] = []
self.children: Dict[str, Any] = {}
fileHandler = TimedRotatingFileHandler("{0}/{1}.log".format(self.config.server.log_dir, "frontend"), when="d", backupCount=10) fileHandler = TimedRotatingFileHandler("{0}/{1}.log".format(self.config.server.log_dir, "frontend"), when="d", backupCount=10)
fileHandler.setFormatter(log_fmt) fileHandler.setFormatter(log_fmt)
@ -43,10 +44,13 @@ class FrontendServlet(resource.Resource):
for game_dir, game_mod in games.items(): for game_dir, game_mod in games.items():
if hasattr(game_mod, "frontend"): if hasattr(game_mod, "frontend"):
try: try:
fe_game.putChild(game_dir.encode(), game_mod.frontend(cfg, self.environment, config_dir)) game_fe = game_mod.frontend(cfg, self.environment, config_dir)
self.game_list.append({"url": game_dir, "name": game_fe.nav_name})
fe_game.putChild(game_dir.encode(), game_fe)
except: except:
raise raise
self.environment.globals["game_list"] = self.game_list
self.putChild(b"gate", FE_Gate(cfg, self.environment)) self.putChild(b"gate", FE_Gate(cfg, self.environment))
self.putChild(b"user", FE_User(cfg, self.environment)) self.putChild(b"user", FE_User(cfg, self.environment))
self.putChild(b"game", fe_game) self.putChild(b"game", fe_game)
@ -55,8 +59,8 @@ class FrontendServlet(resource.Resource):
def render_GET(self, request): def render_GET(self, request):
self.logger.debug(f"{request.getClientIP()} -> {request.uri.decode()}") self.logger.debug(f"{request.getClientIP()} -> {request.uri.decode()}")
template = self.environment.get_template("index.jinja") template = self.environment.get_template("core/frontend/index.jinja")
return template.render(server_name=self.config.server.name, title=self.config.server.name).encode("utf-16") return template.render(server_name=self.config.server.name, title=self.config.server.name, game_list=self.game_list).encode("utf-16")
class FE_Base(resource.Resource): class FE_Base(resource.Resource):
""" """
@ -65,11 +69,12 @@ class FE_Base(resource.Resource):
It is expected that game implementations of this class overwrite many of these It is expected that game implementations of this class overwrite many of these
""" """
isLeaf = True isLeaf = True
def __init__(self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str = None) -> None: def __init__(self, cfg: CoreConfig, environment: jinja2.Environment) -> None:
self.core_config = cfg self.core_config = cfg
self.data = Data(cfg) self.data = Data(cfg)
self.logger = logging.getLogger('frontend') self.logger = logging.getLogger('frontend')
self.environment = environment self.environment = environment
self.nav_name = "nav_name"
class FE_Gate(FE_Base): class FE_Gate(FE_Base):
def render_GET(self, request: Request): def render_GET(self, request: Request):
@ -86,7 +91,7 @@ class FE_Gate(FE_Base):
else: err = 0 else: err = 0
template = self.environment.get_template("gate/gate.jinja") template = self.environment.get_template("core/frontend/gate/gate.jinja")
return template.render(title=f"{self.core_config.server.name} | Login Gate", error=err).encode("utf-16") return template.render(title=f"{self.core_config.server.name} | Login Gate", error=err).encode("utf-16")
def render_POST(self, request: Request): def render_POST(self, request: Request):
@ -153,12 +158,12 @@ class FE_Gate(FE_Base):
ac = request.args[b'ac'][0].decode() ac = request.args[b'ac'][0].decode()
template = self.environment.get_template("gate/create.jinja") template = self.environment.get_template("core/frontend/gate/create.jinja")
return template.render(title=f"{self.core_config.server.name} | Create User", code=ac).encode("utf-16") return template.render(title=f"{self.core_config.server.name} | Create User", code=ac).encode("utf-16")
class FE_User(FE_Base): class FE_User(FE_Base):
def render_GET(self, request: Request): def render_GET(self, request: Request):
template = self.environment.get_template("user/index.jinja") template = self.environment.get_template("core/frontend/user/index.jinja")
return template.render().encode("utf-16") return template.render().encode("utf-16")
if b'session' not in request.cookies: if b'session' not in request.cookies:
return redirectTo(b"/gate", request) return redirectTo(b"/gate", request)

View File

@ -1,4 +1,4 @@
{% extends "index.jinja" %} {% extends "core/frontend/index.jinja" %}
{% block content %} {% block content %}
<h1>Create User</h1> <h1>Create User</h1>
<form id="create" style="max-width: 240px; min-width: 10%;" action="/gate/gate.create" method="post"> <form id="create" style="max-width: 240px; min-width: 10%;" action="/gate/gate.create" method="post">

View File

@ -1,7 +1,7 @@
{% extends "index.jinja" %} {% extends "core/frontend/index.jinja" %}
{% block content %} {% block content %}
<h1>Gate</h1> <h1>Gate</h1>
{% include "widgets/err_banner.jinja" %} {% include "core/frontend/widgets/err_banner.jinja" %}
<form id="login" style="max-width: 240px; min-width: 10%;" action="/gate/gate.login" method="post"> <form id="login" style="max-width: 240px; min-width: 10%;" action="/gate/gate.login" method="post">
<div class="form-group row"> <div class="form-group row">
<label for="access_code">Card Access Code</label><br> <label for="access_code">Card Access Code</label><br>

View File

@ -80,7 +80,7 @@
</style> </style>
</head> </head>
<body> <body>
{% include "widgets/topbar.jinja" %} {% include "core/frontend/widgets/topbar.jinja" %}
{% block content %} {% block content %}
<h1>{{ server_name }}</h1> <h1>{{ server_name }}</h1>
{% endblock content %} {% endblock content %}

View File

@ -1,4 +1,4 @@
{% extends "index.jinja" %} {% extends "core/frontend/index.jinja" %}
{% block content %} {% block content %}
<h1>testing</h1> <h1>testing</h1>
{% endblock content %} {% endblock content %}

View File

@ -1,6 +1,13 @@
<div style="background: #333; color: #f9f9f9; width: 10%; height: 50px; line-height: 50px; text-align: center; float: left;"> <div style="background: #333; color: #f9f9f9; width: 10%; height: 50px; line-height: 50px; text-align: center; float: left;">
Navigation Navigation
</div> </div>
<div style="background: #333; color: #f9f9f9; width: 90%; height: 50px; line-height: 50px; text-align: center; float: right;"> <div style="background: #333; color: #f9f9f9; width: 80%; height: 50px; line-height: 50px; padding-left: 10px; float: left;">
<a href=/><button class="btn btn-primary">Home</button></a>&nbsp;
{% for game in game_list %}
<a href=game/{{ game.url }}><button class="btn btn-success">{{ game.name }}</button></a>&nbsp;
{% endfor %}
</div>
</div>
<div style="background: #333; color: #f9f9f9; width: 10%; height: 50px; line-height: 50px; text-align: center; float: left;">
<a href="/gate"><button class="btn btn-primary">Gate</button></a> <a href="/gate"><button class="btn btn-primary">Gate</button></a>
</div> </div>

View File

@ -6,6 +6,7 @@ from core.frontend import FE_Base
from core.config import CoreConfig from core.config import CoreConfig
from titles.wacca.database import WaccaData from titles.wacca.database import WaccaData
from titles.wacca.config import WaccaConfig from titles.wacca.config import WaccaConfig
from titles.wacca.const import WaccaConstants
class WaccaFrontend(FE_Base): class WaccaFrontend(FE_Base):
def __init__(self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str) -> None: def __init__(self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str) -> None:
@ -13,6 +14,11 @@ class WaccaFrontend(FE_Base):
self.data = WaccaData(cfg) self.data = WaccaData(cfg)
self.game_cfg = WaccaConfig() self.game_cfg = WaccaConfig()
self.game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/wacca.yaml"))) self.game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/wacca.yaml")))
self.nav_name = "Wacca"
def render_GET(self, request: Request) -> bytes: def render_GET(self, request: Request) -> bytes:
return b"" template = self.environment.get_template("titles/wacca/frontend/wacca_index.jinja")
return template.render(
title=f"{self.core_config.server.name} | {self.nav_name}",
game_list=self.environment.globals["game_list"]
).encode("utf-16")

View File

@ -0,0 +1,4 @@
{% extends "core/frontend/index.jinja" %}
{% block content %}
<h1>Wacca</h1>
{% endblock content %}