Skip to content

Application and File Structure

kiennt edited this page Mar 30, 2013 · 5 revisions

Application File Structure

.
├── app
│   ├── config
│   │   ├── client
│   │   │   ├── bootstrap.coffee
│   │   │   └── watch.coffee
│   │   ├── server
│   │   │   ├── environments
│   │   │   │   ├── development.coffee
│   │   │   │   ├── production.coffee
│   │   │   │   └── test.coffee
│   │   │   ├── initializers
│   │   │   ├── assets.coffee
│   │   │   ├── bootstrap.coffee
│   │   │   ├── credentials.coffee
│   │   │   ├── databases.coffee
│   │   │   └── session.coffee
│   │   └── shared
│   │       ├── locales
│   │       │   └── en.coffee
│   │       ├── application.coffee
│   │       └── routes.coffee
│   ├── controllers
│   │   ├── client
│   │   │   ├── applicationController.coffee
│   │   │   └── postsController.coffee
│   │   └── server
│   │       ├── applicationController.coffee
│   │       └── postsController.coffee
│   ├── models
│   │   ├── client
│   │   ├── server
│   │   └── shared
│   │       └── post.coffee
│   ├── stylesheets
│   │   ├── client
│   │   │   └── application.styl
│   │   └── server
│   │       └── email.styl
│   ├── templates
│   │   ├── server
│   │   │   └── layout
│   │   │       ├── _meta.coffee
│   │   │       └── application.coffee
│   │   └── shared
│   │       ├── layout
│   │       │   ├── _body.coffee
│   │       │   ├── _flash.coffee
│   │       │   ├── _footer.coffee
│   │       │   ├── _header.coffee
│   │       │   ├── _navigation.coffee
│   │       │   └── _sidebar.coffee
│   │       ├── posts
│   │       │   ├── _flash.coffee
│   │       │   ├── _form.coffee
│   │       │   ├── _item.coffee
│   │       │   ├── _list.coffee
│   │       │   ├── _table.coffee
│   │       │   ├── edit.coffee
│   │       │   ├── index.coffee
│   │       │   ├── new.coffee
│   │       │   └── show.coffee
│   │       └── welcome.coffee
│   └── views
│       └── client
│           ├── layout
│           │   └── application.coffee
│           └── posts
│               ├── form.coffee
│               ├── index.coffee
│               └── show.coffee
├── data
│   └── seeds.coffee
├── lib
├── log
├── public
│   ├── fonts
│   ├── images
│   ├── javascripts
│   ├── stylesheets
│   ├── swfs
│   ├── uploads
│   ├── 404.html
│   ├── 500.html
│   ├── favicon.ico
│   ├── humans.txt
│   └── robots.txt
├── scripts
│   └── tower
├── test
│   ├── cases
│   │   ├── controllers
│   │   │   ├── client
│   │   │   └── server
│   │           └── postsControllerTest.coffee
│   │   ├── features
│   │   │   └── client
│   │   └── models
│   │       ├── client
│   │       ├── server
│   │       └── shared
│   │           └── postTest.coffee
│   ├── factories
│   │   └── postFactory.coffee
│   ├── client.coffee
│   ├── mocha.opts
│   └── server.coffee
├── tmp
├── wiki
│   ├── _sidebar.md
│   └── home.md
├── Cakefile
├── grunt.coffee
├── package.json
├── Procfile
├── README.md
└── server.js

The Application Object

«FIXME: There is no config/application.coffee (see above!)» The main application is defined in config/application.coffee and defaults to this:

class App extends Tower.Application
  @configure ->
    @use "favicon", Tower.publicPath + "/favicon.ico"
    @use "static",  Tower.publicPath, maxAge: Tower.publicCacheDuration
    @use "profiler" if Tower.env != "production"
    @use "logger"
    @use "query"
    @use "cookieParser", Tower.session.secret
    @use "session", Tower.session.key
    @use "bodyParser"
    @use "csrf"
    @use "methodOverride", "_method"
    @use Tower.Middleware.Agent
    @use Tower.Middleware.Location
    @use Tower.Middleware.Router

The configure function is used to configure the application.

  • favicon points to the icon file displayed in the browser address bar.
  • static is used to set where public (static) files reside and define their cache duration on the client.
  • profiler is by default configured to only used only when the app is not in production mode (environment).

You can of course extend the configure function with your own application configuration logic as needed.

The Client

The client application is defined exactly like the server application, they just tend to require different middleware and slightly different configuration.

Auto-reloading Changed Files

Internally Tower.js knows when a file was changed. So when you refresh http://localhost:3000, it passes through the Tower.Middleware.Dependencies which re-requires any file that has changed. This makes development uber fast, and prevents you from having to restart the server whenever a file changes (i.e. nodemon). I mean, you can use nodemon if you want, it's a great project. But you don't need to.

Configuration

Environments

Tower by default comes with the following environments:

  • development
  • test
  • production

Additional environments can be defined if needed (such as staging).

Internationalization (I18n)

# app/config/shared/locales/en.coffee
module.exports =
  hello: 'world'
  forms:
    titles:
      signup: 'Signup'
  pages:
    titles:
      home: 'Welcome to %{site}'
  posts:
    comments:
      none: 'No comments'
      one: '1 comment'
      other: '%{count} comments'
  messages:
    past:
      none: 'You never had any messages'
      one: 'You had 1 message'
      other: 'You had %{count} messages'
    present:
      one: 'You have 1 message'
    future:
      one: 'You might have 1 message'

Tower also has some more internal locale files for system generated text such as dates and times etc. You can provide your own locale files for these as well (see the Tower src code and look for /locale folders).

Gruntfile

  • Automatically compile assets when they are added, saved, or removed. See the Tower Asset Pipeline
  • Automatically restart the server in development mode when you modify a file
  • Push compiled CSS and JavaScripts to the browser as you're developing without refreshing the browser!
  • Automatically run tests across browsers, platforms, and devices as your developing.

Dotfiles

Dotfiles are for the most part just hidden files that different platforms use to store configuration data about your project.

In Tower.js, there's 3 by default:

  • .gitignore Tells git the files and directories it should ignore.
  • .npmignore Tells npm what to ignore when your module is published.
  • .slugignore Tells Heroku what to ignore about your project.
Clone this wiki locally