1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-25 01:38:08 +00:00
gns3-server/gns3server/api/routes/index.py

91 lines
3.1 KiB
Python
Raw Normal View History

2020-10-02 06:37:50 +00:00
#
# Copyright (C) 2020 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from fastapi import APIRouter, Request, HTTPException, status
2020-10-02 06:37:50 +00:00
from fastapi.responses import RedirectResponse, HTMLResponse, FileResponse
from fastapi.templating import Jinja2Templates
from gns3server.version import __version__
2021-04-13 09:16:50 +00:00
from gns3server.utils.get_resource import get_resource
2020-10-02 06:37:50 +00:00
router = APIRouter()
templates = Jinja2Templates(directory=os.path.join("gns3server", "templates"))
@router.get("/")
async def root():
return RedirectResponse("/static/web-ui/bundled", status_code=308) # permanent redirect
2020-10-02 06:37:50 +00:00
2021-04-13 09:16:50 +00:00
@router.get("/debug", response_class=HTMLResponse, deprecated=True)
2020-10-02 06:37:50 +00:00
def debug(request: Request):
2021-04-13 09:16:50 +00:00
kwargs = {"request": request, "gns3_version": __version__, "gns3_host": request.client.host}
2020-10-02 06:37:50 +00:00
return templates.TemplateResponse("index.html", kwargs)
2021-04-13 09:16:50 +00:00
@router.get("/static/web-ui/{file_path:path}", description="Web user interface")
2020-10-02 06:37:50 +00:00
async def web_ui(file_path: str):
file_path = os.path.normpath(file_path).strip("/")
2021-04-13 09:16:50 +00:00
file_path = os.path.join("static", "web-ui", file_path)
2020-10-02 06:37:50 +00:00
# Raise error if user try to escape
if file_path[0] == ".":
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
2020-10-02 06:37:50 +00:00
static = get_resource(file_path)
if static is None or not os.path.exists(static):
2021-04-13 09:16:50 +00:00
static = get_resource(os.path.join("static", "web-ui", "index.html"))
2020-10-02 06:37:50 +00:00
# guesstype prefers to have text/html type than application/javascript
# which results with warnings in Firefox 66 on Windows
# Ref. gns3-server#1559
_, ext = os.path.splitext(static)
2021-04-13 09:16:50 +00:00
mimetype = ext == ".js" and "application/javascript" or None
2020-10-02 06:37:50 +00:00
return FileResponse(static, media_type=mimetype)
# class Version(BaseModel):
# version: str
# local: Optional[bool] = False
#
#
# @router.get("/v2/version",
# description="Retrieve the server version number",
# response_model=Version,
# )
# def version():
#
# config = Config.instance()
# local_server = config.get_section_config("Server").getboolean("local", False)
# return {"version": __version__, "local": local_server}
#
#
# @router.post("/v2/version",
# description="Check if version is the same as the server",
# response_model=Version,
# )
# def check_version(version: str):
#
# if version != __version__:
# raise HTTPException(status_code=409, detail="Client version {} is not the same as server version {}".format(version, __version__))
# return {"version": __version__}