-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
122 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
from . import server | ||
from . import colors | ||
from . import theme | ||
from . import system | ||
from . import wallpaper | ||
from . import theme | ||
from . import server | ||
|
||
__all__ = [ | ||
"server", | ||
"colors", | ||
"system", | ||
"theme", | ||
"wallpaper", | ||
"server", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,53 @@ | ||
import os | ||
import socket | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi import Request | ||
from fastapi import File, UploadFile | ||
from typing import List | ||
from fastapi import FastAPI, Request, File, UploadFile | ||
from fastapi.responses import HTMLResponse | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi.staticfiles import StaticFiles | ||
from fastapi.templating import Jinja2Templates | ||
|
||
from typing import List | ||
from uvicorn import run | ||
|
||
from pycloudflared import try_cloudflare | ||
|
||
from .settings import CACHE_DIR | ||
from .settings import CACHE_DIR, OS | ||
from .colors import Color | ||
from .theme import Theme | ||
from .wallpaper import Wallpaper | ||
from .system import System | ||
from . import util | ||
|
||
|
||
class Server: | ||
def __init__(self): | ||
self.wallpaper = Wallpaper(); | ||
self.color = Color(); | ||
self.theme = Theme(); | ||
self.system = System(); | ||
|
||
self.api_url = None | ||
self.server = FastAPI(title="Pywalc", | ||
description="Control your pywal", | ||
contact={ | ||
"name": "Phuong Nguyen", | ||
"url": "https://png261.github.io", | ||
"email": "[email protected]", | ||
}, | ||
openapi_tags=[ | ||
{ | ||
"name": "wallpaper", | ||
}, | ||
{ | ||
"name": "theme", | ||
}, | ||
{ | ||
"name": "color", | ||
}, | ||
{ | ||
"name": "system", | ||
}, | ||
], | ||
redoc_url=None, | ||
docs_url="/api") | ||
def __init__(self, port): | ||
self.port = port | ||
self.wallpaper = Wallpaper() | ||
self.theme = Theme() | ||
self.color = Color() | ||
self.server = FastAPI( | ||
title="Pywalc", | ||
description="Control your pywal", | ||
contact={ | ||
"name": "Phuong Nguyen", | ||
"url": "https://png261.github.io", | ||
"email": "[email protected]", | ||
}, | ||
openapi_tags=[ | ||
{ | ||
"name": "wallpaper", | ||
}, | ||
{ | ||
"name": "theme", | ||
}, | ||
{ | ||
"name": "color", | ||
}, | ||
{ | ||
"name": "system", | ||
}, | ||
], | ||
redoc_url=None, | ||
docs_url="/api", | ||
) | ||
|
||
self.server.add_middleware( | ||
CORSMiddleware, | ||
|
@@ -63,20 +61,18 @@ def __init__(self): | |
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static") | ||
self.server.mount("/static", StaticFiles(directory=static_dir), name="static") | ||
|
||
templates_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") | ||
templates_dir = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), "templates" | ||
) | ||
self.templates = Jinja2Templates(directory=templates_dir) | ||
|
||
self._setup_routes(); | ||
self._setup_routes() | ||
|
||
def _setup_routes(self): | ||
@self.server.get("/", response_class=HTMLResponse) | ||
async def home_page(request: Request): | ||
return self.templates.TemplateResponse("index.html", {"request": request}) | ||
|
||
@self.server.get("/wallpaper/{id}/color", tags=["wallpaper"]) | ||
def get_wallpaper_colors(id): | ||
return self.wallpaper.get_colors(id) | ||
|
||
@self.server.get("/wallpaper", tags=["wallpaper"]) | ||
def get_wallpapers(): | ||
return self.wallpaper.get() | ||
|
@@ -85,6 +81,10 @@ def get_wallpapers(): | |
def set_wallpaper(id: str): | ||
return self.wallpaper.set(id) | ||
|
||
@self.server.get("/wallpaper/{id}/color", tags=["wallpaper"]) | ||
def get_wallpaper_colors(id): | ||
return self.wallpaper.get_colors(id) | ||
|
||
@self.server.post("/wallpaper", tags=["wallpaper"]) | ||
async def upload_wallpaper(files: List[UploadFile] = File(...)): | ||
return await self.wallpaper.upload(files) | ||
|
@@ -114,18 +114,22 @@ def set_theme(name, category): | |
return self.theme.set(name, category) | ||
|
||
@self.server.get("/sys", tags=["system"]) | ||
def system_get_info(): | ||
return self.system.get() | ||
def get_system_info(): | ||
return {"os": OS, "name": socket.gethostname()} | ||
|
||
@self.server.get("/reset", tags=["system"]) | ||
def system_reset(): | ||
return self.system.reset(self.color, self. wallpaper) | ||
def reset(): | ||
self.color.reset() | ||
self.wallpaper.reset() | ||
|
||
def _start_localhost(self): | ||
run(self.server, host='127.0.0.1', port=8080) | ||
print(" * Localhost: http://127.0.0.1:{port}".format(port=self.port)) | ||
run(self.server, host="127.0.0.1", port=self.port, log_level="error") | ||
|
||
def _start_cloudfare_tunnel(self): | ||
self.api_url = try_cloudflare(port=8080).tunnel | ||
self.online_site = try_cloudflare(port=self.port, verbose=False).tunnel | ||
print(" * Online: {url}".format(url=self.online_site)) | ||
util.show_ascii_qrcode(self.online_site) | ||
|
||
def run(self): | ||
self._start_cloudfare_tunnel() | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.