如何将外部服务器用作上行链路并同时托管 Web 应用程序?

How to use an external server as an Uplink and to host the web app simultaneously?

我不太熟悉 Anvil 服务器在服务器、上行链路和客户端之间的身份验证和通信方面的工作原理。不过,我会尽量解释这个问题,请原谅我的英语:

1- 我在 anvil 在线编辑器上创建了一个简单的“Hello World”应用程序,然后我对其进行了测试,它运行良好。

2- 我使用外部上行链路功能将我的服务器脚本保存在远程服务器上,再次一切正常,代码如下:

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.message_label.text = anvil.server.call('get_name', self.name_box.text)
    pass

  def text_box_3_pressed_enter(self, **event_args):
    """This method is called when the user presses Enter in this text box"""
    pass

  def sum_click(self, **event_args):
    """This method is called when the button is clicked"""
    self.result.text = anvil.server.call('add_nums', self.num1.text, self.num2.text)
    pass

这是外部上行链路上 运行 的服务器脚本:

import anvil.server
anvil.server.connect("UPLINK DERIVED FROM ANVIL ONLINE")

@anvil.server.callable
def get_name(name):
  response = "Hello from the server, %s!" % name
  return response

@anvil.server.callable
def add_nums(num1,num2):
  result = int(num1)+int(num2)
  return result

anvil.server.wait_forever()

3- 现在,我尝试通过 git 在线克隆 Anvil 上的我的应用程序,以便我可以完全从我的外部服务器托管它:

git clone ssh://bla@bla.bla@anvil.works:2222/RN35GHXNR4UHW77W.git Hello_World

anvil-app-server --app Hello_World --origin https://somewhere.domain.com

到目前为止一切顺利,应用程序可以通过外部在浏览器上正常加载 URL

4- 最后,当我输入一些数据并按下按钮时,我遇到了这个丑陋的错误:

[ERROR anvil.app-server.run] Error report from client code:
NoServerFunctionError: No server function matching "get_name" has been registered
Traceback:
  app/Hello_World/Form1/__init__.py:15

^CTraceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/melsayeh/env/lib/python3.8/site-packages/anvil_downlink_host/run.py", line 4, in <module>
    anvil_downlink_host.run_downlink_host()
  File "/home/melsayeh/env/lib/python3.8/site-packages/anvil_downlink_host/__init__.py", line 474, in run_downlink_host
    time.sleep(5)

当您在本地启动应用服务器时,您需要传递要用于该应用的上行链路密钥。

然后您需要将相同的密钥添加到上行链路脚本中的连接调用。您还需要在该调用中传递一个指向您的应用服务器的 URL 参数。

您可以在以下位置查看应用服务器的选项,包括上行链路密钥:

https://github.com/anvil-works/anvil-runtime#advanced-configuration