Browse Source

web page showing the counter

boyska 2 years ago
parent
commit
20c30d0b05
4 changed files with 89 additions and 7 deletions
  1. 43 0
      pizzicore/pages/index.html
  2. 17 7
      pizzicore/pizzicore.py
  3. 24 0
      pizzicore/static/js/app.js
  4. 5 0
      pizzicore/static/js/fitty.min.js

+ 43 - 0
pizzicore/pages/index.html

@@ -0,0 +1,43 @@
+<!doctype html>
+<html>
+    <head>
+<style>
+span { width: 100%; margin: 0 auto; }
+header { width: 100%; margin: 0px auto; }
+body {
+    background: #ddd;
+    margin: 0;
+}
+#container {
+    text-align: center;
+    width: 100%;
+    margin: 0;
+}
+img, svg, text {
+    margin: 0 auto;
+    text-align: center;
+    height: auto !important;
+    width: auto !important;
+    display: block;
+}
+.counter { line-height: 1; margin: 0; }
+
+</style>
+    </head>
+    <body data-cid="0">
+        <!--h1 id="counter">asd</h1-->
+
+
+        <div id="container">
+            <img>
+            <svg viewBox="0 0 40 18">
+                <text x="0" y="15">Fit Me</text>
+            </svg>
+            </img>
+        </div>
+
+
+        <script src="/static/js/fitty.min.js"></script>
+        <script type="application/javascript" src="/static/js/app.js"></script>
+    </body>
+</html>

+ 17 - 7
pizzicore/pizzicore.py

@@ -2,17 +2,14 @@ import secrets
 import logging
 import dbm
 from collections import defaultdict
+from mimetypes import guess_type
 
 from asyncio.queues import Queue
-from fastapi import (
-    FastAPI,
-    WebSocket,
-    HTTPException,
-    Depends,
-    status,
-)
+from fastapi import FastAPI, WebSocket, HTTPException, Depends, status, Response
 from starlette.websockets import WebSocketDisconnect
 from fastapi.security import HTTPBasic, HTTPBasicCredentials
+from fastapi.encoders import jsonable_encoder
+from fastapi.staticfiles import StaticFiles
 from pydantic import BaseModel
 
 
@@ -86,6 +83,7 @@ class Manager:
 
 
 app = FastAPI()
+app.mount("/static", StaticFiles(directory="static"), name="static")
 manager = Manager()
 counter_store = Store(
     n=1, db_path="/var/lib/pizzicore/pizzicore.dbm", manager=manager
@@ -148,3 +146,15 @@ async def websocket_counter(websocket: WebSocket, cid: int):
             manager.unsubscribe(cid, q)
             return
         await q.get()
+
+
+async def get_page(fname):
+    with open(fname) as f:
+        content = f.read()
+    content_type, _ = guess_type(fname)
+    return Response(content, media_type=content_type)
+
+
+@app.get("/")
+async def root_page():
+    return await get_page("pages/index.html")

+ 24 - 0
pizzicore/static/js/app.js

@@ -0,0 +1,24 @@
+function get_url()
+{
+    var url = "";
+    if(window.location.protocol == "http:") {
+        url += "ws://"
+    } else {
+        url = "wss://"
+    }
+    url += window.location.host + '/v1/ws/counter/';
+    url += document.getElementsByTagName('body')[0].dataset.cid;
+    return url;
+}
+
+function do_websocket()
+{
+    const socket = new WebSocket(get_url());
+    socket.addEventListener('message', function(evt) {
+        console.log('received', evt.data)
+        var msg = JSON.parse(evt.data)
+        counter = document.getElementsByTagName('text')[0];
+        counter.textContent = msg.value.toString()
+    })
+}
+do_websocket()

File diff suppressed because it is too large
+ 5 - 0
pizzicore/static/js/fitty.min.js


Some files were not shown because too many files changed in this diff