5

How would you structure a simple Sinatra app?

I'm making it right now and I want the app to have these features:

  1. The "app" is more of an admin dashboard for all the information inside it. Then another app will access the information via REST. I have not created the dashboard yet, just the getting of things from the database
  2. Sessions and authentication (have not implemented this yet)
  3. You can upload pictures, and other the other app can display those pictures
  4. I have created a testing file using RSpec
  5. Reports generation via Prawn

Currently the setup is just this:

app.rb
test_app.rb

because I have literally just the app and testing file. So far I have used Datamapper for the ORM, SQLite for the database. This is my first Ruby/Sinatra project, so any and all advice is welcome - what other libraries should I be using, should I put stuff like config.ru, etc.

4

1 回答 1

9

Sinatra 在文件结构方面并不固执己见,您可以随意放置文件。当我刚开始的时候,我只是把所有东西都放在顶层,但是随着时间的推移,阅读人们如何构建他们的代码,阅读 gems 的源代码,我已经将我的代码分解成更小的 .rb 文件,这些文件实现了特定的功能并将所有在 /lib 下,这可能是从 rails 继承下来的约定,但在 rails 中没有任何与之相关的魔法。如果您使用 scss 或咖啡脚本,它们依赖于某些文件夹的存在,您会随着时间的推移自己发现(即使那样您也可以根据需要重新配置它们),从中您将找出最适合您的文件夹。

如果您编写一个宁静的 api,请查看葡萄 - https://github.com/intridea/grape

您还会发现 sinatra-contrib 非常有用 - https://github.com/sinatra/sinatra-contrib

至于如何处理您的 config.ru - https://github.com/rack/rack/wiki/%28tutorial%29-rackup-howto

于 2013-09-04T23:48:29.743 回答