Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/manatlan/htagweb
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Sep 26, 2023
2 parents dd2b3ca + 3b89dd4 commit f280855
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ So developping a htag app which work the same on desktop and on the web, should
* multiple ways to handle sessions (file, mem, etc ...)
* compatible with **uvloop** !!!
* compatible with multiple gunicorn/uvicorn/webworkers !!!
* compatible with **tag.update()**
* compatible with [tag.update()](https://manatlan.github.io/htag/tag_update/)
* works on gnu/linux, ios or windows !
* real starlette session available (in tag.state, and starlette request.session)
* compatible with oauth2 authent ( [authlib](https://pypi.org/project/Authlib/) )
Expand Down
3 changes: 3 additions & 0 deletions examples/oauth_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def user(self): # -> dict or empty dict
return self._rootstate.get(OAUTH_SESSION_NAME,{})

def render(self): # dynamic rendering
self.clear()
self+= self.title + self.btn
if self.user:
self.title.set(self.user['name'])
Expand All @@ -83,6 +84,8 @@ def init(self):
self.oa = TagOAuth(self.state)

def render(self): # dynamic rendering
self.clear()

self += self.oa
self += f"You are {self.oa.user.get('name') or 'unknown'}"

Expand Down
55 changes: 40 additions & 15 deletions htagweb/appserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@ async def send_wrapper(message: Message) -> None:
await self.app(scope, receive, send_wrapper)


def fqn2hr(fqn:str,js:str,init,session): # fqn is a "full qualified name", full !
def fqn2hr(fqn:str,js:str,init,session,fullerror=False): # fqn is a "full qualified name", full !
if ":" not in fqn:
# replace last "." by ":"
fqn="".join( reversed("".join(reversed(fqn)).replace(".",":",1)))

klass=getClass(fqn)

return HRenderer( klass, js, init=init, session = session)
styles=Tag.style("body.htagoff * {cursor:not-allowed !important;}")

return HRenderer( klass, js, init=init, session = session, fullerror=fullerror, statics=[styles,])

class HRSocket(WebSocketEndpoint):
encoding = "text"
Expand Down Expand Up @@ -197,7 +199,7 @@ async def on_connect(self, websocket):
"""

try:
hr=fqn2hr(fqn,js,commons.url2ak(str(websocket.url)),websocket.session)
hr=fqn2hr(fqn,js,commons.url2ak(str(websocket.url)),websocket.session,fullerror=websocket.app.debug)
except Exception as e:
await self._sendback( websocket, str(e) )
await websocket.close()
Expand Down Expand Up @@ -262,22 +264,45 @@ async def serve(self, request, obj ) -> HTMLResponse:
jsbootstrap="""
%(jsparano)s
// instanciate the WEBSOCKET
var _WS_ = new WebSocket("%(protocol)s://"+location.host+"/_/%(fqn)s"+location.search);
_WS_.onmessage = async function(e) {
// when connected -> the full HTML page is returned, installed & start'ed !!!
let html = await _read_(e.data);
html = html.replace("<body ","<body onload='start()' ");
document.open();
document.write(html);
document.close();
};
let _WS_=null;
let retryms=500;
function connect() {
_WS_= new WebSocket("%(protocol)s://"+location.host+"/_/%(fqn)s"+location.search);
_WS_.onopen=function(evt) {
console.log("** WS connected")
document.body.classList.remove("htagoff");
retryms=500;
_WS_.onmessage = async function(e) {
// when connected -> the full HTML page is returned, installed & start'ed !!!
let html = await _read_(e.data);
html = html.replace("<body ","<body onload='start()' ");
document.open();
document.write(html);
document.close();
};
}
_WS_.onclose = function(evt) {
console.log("** WS disconnected");
//console.log("** WS disconnected, retry in (ms):",retryms);
document.body.classList.add("htagoff");
//setTimeout( function() {
// connect();
// retryms=retryms*2;
//}, retryms);
};
}
connect();
""" % locals()

# here return a first rendering (only for SEO)
# the hrenderer is DESTROYED just after
hr=fqn2hr(fqn,jsbootstrap,commons.url2ak(str(request.url)),request.session)
hr=fqn2hr(fqn,jsbootstrap,commons.url2ak(str(request.url)),request.session, fullerror=self.debug)

return HTMLResponse( str(hr) )

Expand Down

0 comments on commit f280855

Please sign in to comment.