模块化、基于组件的 Sinatra 应用程序的架构
我正在开发一个 Sinatra 应用程序,其中包含大约 10 个不同的功能组件。我们希望能够将这些组件混合并匹配到应用程序的单独实例中,完全通过 config.yaml 文件进行配置,如下所示:
components:
- route: '/chunky'
component_type: FoodLister
component_settings:
food_type: bacon
max_items: 400
- route: 'places/paris'
component_type: Mapper
component_settings:
latitude: 48.85387273165654
longitude: 2.340087890625
- route: 'places/losangeles'
component_type: Mapper
component_settings:
latitude: 34.043556504127466
longitude: -118.23486328125
如您所见,组件可以多次实例化,每个组件都有自己的实例上下文设置。
每个组件至少包含一个路由,其中配置文件中的“route”属性用于基础组件。
组织和实例化模块代码的最佳方法是什么?
I'm working on a Sinatra app that contains about 10 different components of functionality. We'd like to be able to mix and match these components into separate instances of the application, configured entirely from a config.yaml file that looks something like:
components:
- route: '/chunky'
component_type: FoodLister
component_settings:
food_type: bacon
max_items: 400
- route: 'places/paris'
component_type: Mapper
component_settings:
latitude: 48.85387273165654
longitude: 2.340087890625
- route: 'places/losangeles'
component_type: Mapper
component_settings:
latitude: 34.043556504127466
longitude: -118.23486328125
As you can see, components can be instantiated more than once, each with their own contextual settings.
Each component consists of at least one route, with the "route" property from the config file used for the base.
What is the best way to organize and instantiate the module code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与 include 的提议类似,但它不需要访问rackup 文件。
像这样编写各种处理程序:
然后在主应用程序文件中:
我已经使用这种结构来构建一些相当复杂的 Sinatra 应用程序。它的扩展性与 Rails 一样好。
编辑
要让您的配置文件定义路由,您可以执行以下操作:
This is similar to include's proposal, but it doesn't require access to the rackup file.
Write your various Handlers like:
Then in your main application file:
I've used this kind of structure to build some fairly complex Sinatra apps. It scales just as well as Rails.
EDIT
To have your config file define the routes, you could do something like this:
TIMTOWTDI - 还有一种方法可以做:) 这就是其中之一。但实际上我用的是另一种方式。我使用 Sinatra/Base 来开发模块化应用程序。
我有通往每个应用程序的简单路线。
您可以为每个实例设置变量集。
您可以在其模块上开发每个“组件”。
就看这里吧。 http://codex.heroku.com/
玩得开心:)
TIMTOWTDI - There's_more_than_one_way_to_do_it :) and that is one. But in fact I use another way. I use Sinatra/Base to dev modular apps.
I have simgle routes to each app.
You can have variable sets for each instance.
You can develop each 'component' on its Module.
That a look here. http://codex.heroku.com/
have fun :)