Skip to content

Commit

Permalink
Support online railway deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpin committed Mar 25, 2023
1 parent 04fec4a commit 2d09357
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ nohup python3 app.py & tail -f nohup.out # 在后台运行程序并通

参考文档 [Docker部署](https://github.com/limccn/chatgpt-on-wechat/wiki/Docker%E9%83%A8%E7%BD%B2) (Contributed by [limccn](https://github.com/limccn))。

### 4. Railway部署
[Use with Railway](#use-with-railway)(PaaS, Free, Stable, ✅Recommended)
> Railway offers $5 (500 hours) of runtime per month
1. Click the [Railway](https://railway.app/) button to go to the Railway homepage
2. Click the `Start New Project` button.
3. Click the `Deploy from Github repo` button.
4. Choose your repo (you can fork this repo firstly)
5. Set environment variable to override settings in config-template.json, such as: model, open_ai_api_base, open_ai_api_key, use_azure_chatgpt etc.

## 常见问题

Expand Down
6 changes: 5 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from common.log import logger

from plugins import *
if __name__ == '__main__':

def run():
try:
# load config
config.load_config()
Expand All @@ -21,3 +22,6 @@
except Exception as e:
logger.error("App startup failed!")
logger.exception(e)

if __name__ == '__main__':
run()
17 changes: 8 additions & 9 deletions common/expired_dict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time
from datetime import datetime, timedelta


class ExpiredDict(dict):
Expand All @@ -8,8 +8,7 @@ def __init__(self, expires_in_seconds):

def __getitem__(self, key):
value, expiry_time = super().__getitem__(key)
# 如果元素已过期,则从字典中删除该元素并抛出 KeyError 异常
if time.monotonic() > expiry_time:
if datetime.now() > expiry_time:
del self[key]
raise KeyError("expired {}".format(key))
self.__setitem__(key, value)
Expand All @@ -24,20 +23,20 @@ def get(self, key, default=None):
return self[key]
except KeyError:
return default

def __contains__(self, key):
try:
self[key]
return True
except KeyError:
return False

def keys(self):
keys=list(super().keys())
keys = list(super().keys())
return [key for key in keys if key in self]

def items(self):
return [(key, self[key]) for key in self.keys()]

def __iter__(self):
return self.keys().__iter__()
return self.keys().__iter__()
9 changes: 8 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ def load_config():
global config
config_path = "./config.json"
if not os.path.exists(config_path):
raise Exception('配置文件不存在,请根据config-template.json模板创建config.json文件')
logger.info('配置文件不存在,将使用config-template.json模板')
config_path = "./config-template.json"

config_str = read_file(config_path)
# 将json字符串反序列化为dict类型
config = json.loads(config_str)

# override config with environment variables.
# Some online deployment platforms (e.g. Railway) deploy project from github directly. So you shouldn't put your secrets like api key in a config file, instead use environment variables to override the default config.
for name, value in os.environ.items():
config[name] = value

logger.info("[INIT] load config: {}".format(config))


Expand Down
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# entry point for online railway deployment
from app import run

if __name__ == '__main__':
run()
File renamed without changes.

0 comments on commit 2d09357

Please sign in to comment.