Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General advice and recommended folder structure - Sinatra

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.

like image 586
Daryll Santos Avatar asked Sep 04 '13 10:09

Daryll Santos


People also ask

What is Sinatra framework?

Sinatra is a free and open source software web application library and domain-specific language written in Ruby. It is an alternative to other Ruby web application frameworks such as Ruby on Rails, Merb, Nitro, and Camping. It is dependent on the Rack web server interface.

What is Sinatra :: Base?

Sinatra::Base is the Sinatra without delegation. Consider the following code with delegation: # app.rb require 'sinatra' get '/' do render :template end.


1 Answers

Sinatra is not opinionated when it comes to your file structure, you can place files however you like. When I first started I just dropped everything in the top level, but over time reading how people structure their code, reading over the source code of gems I've broken up my code into smaller .rb files that fulfill a specific function and places all of them under /lib, it's a convention carried over from rails perhaps but does not have any of the magic associated with it in rails. If you use scss or coffee script they depend on certain folders to exist, you will discover for yourself over time (and even then you can reconfigure them however you wish) and from this you will figure out what works best for you.

if you write a restful api, check out grape - https://github.com/intridea/grape

you will also find sinatra-contrib to be very useful - https://github.com/sinatra/sinatra-contrib

As for what to do with your config.ru - https://github.com/rack/rack/wiki/%28tutorial%29-rackup-howto

like image 179
Theta Avatar answered Sep 18 '22 06:09

Theta