Skip to content

Commit

Permalink
support access token switching in server mode
Browse files Browse the repository at this point in the history
Signed-off-by: pengzhile <[email protected]>
  • Loading branch information
pengzhile committed Apr 13, 2023
1 parent 378677d commit 9888dce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
## HTTP服务文档

* 如果你以`http`服务方式启动,现在你可以打开一个极简版的`ChatGPT`了。通过你指定的`http://ip:port`来访问。
* 通过`http://ip:port/?token=xxx`,传递一个Token的名字,可以切换到对应的`Access Token`
* API文档见:[doc/HTTP-API.md](https://github.com/pengzhile/pandora/blob/master/doc/HTTP-API.md)

## 操作命令
Expand Down
2 changes: 1 addition & 1 deletion src/pandora/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = '1.0.0'
__version__ = '1.0.1'
24 changes: 18 additions & 6 deletions src/pandora/bots/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import logging
from datetime import timedelta
from os.path import join, abspath, dirname

from flask import Flask, jsonify, make_response, request, Response, render_template
Expand Down Expand Up @@ -102,18 +103,29 @@ def __handle_error(self, e):
'message': str(e.original_exception if self.debug and hasattr(e, 'original_exception') else e.name)
}), 500)

@staticmethod
def __set_cookie(resp, token_key, max_age):
resp.set_cookie('token-key', token_key, max_age=max_age, path='/', domain=None, httponly=True, samesite='Lax')

@staticmethod
def __get_token_key():
return request.headers.get('X-Use-Token', None)
return request.headers.get('X-Use-Token', request.cookies.get('token-key'))

def chat(self, conversation_id=None):
query = {'chatId': [conversation_id]} if conversation_id else {}

return render_template('chat.html',
pandora_base=request.url_root.strip('/'),
pandora_sentry=self.sentry,
query=query
)
token_key = request.args.get('token')
rendered = render_template('chat.html',
pandora_base=request.url_root.strip('/'),
pandora_sentry=self.sentry,
query=query
)
resp = make_response(rendered)

if token_key:
self.__set_cookie(resp, token_key, timedelta(days=30))

return resp

@staticmethod
def session():
Expand Down

0 comments on commit 9888dce

Please sign in to comment.