stellar-deprecated / stex

DEPRECATED. An opinionated framework built on top of expressjs used to build better web services for stellar
Other
30 stars 11 forks source link

A argument for an auto-loading stex #21

Closed nullstyle closed 10 years ago

nullstyle commented 10 years ago

tl;dr; We should both standardize the directory layout of stex for both "model" and "controller" files and automatically load them at stex initialization

Problem

The CommonJS model for adding module dependencies is nice for being explicit about inter-module dependencies and has overall led to much better javascript code. However, it can lead to very large sections of "require" blocks at the top of each file that can distract from the function of the module, and more importantly it leaves each module the freedom to name the symbols that it imports, leading to naming inconsistencies:

// in module 1:
var wallet = require('wallet');

// in module 2:
var Wallet = require('wallet');

Furthermore, within a single node project you are forced to use relative requires, leading to friction when you restructure your project or rename a file:

// when in a sibling module
var wallet = require("./wallet")

// when in a child module
var wallet = require("../wallet")

Proposal

There exists a set of modules within a stex application that are used quite often throughout nearly all the other modules in a stex application: the "models" and the "controllers". By automatically loading these files we will reduce duplication and increase clarity. I propose we automatically load files underneath /lib/controllers and /lib/models. For example, the routes file (app.js) could go from this:

var walletsCtrl = require("./controllers/wallets")
var totpCtrl    = require("./controllers/totp")
var recoverCtrl = require("./controllers/recovery")

router.post("/v2/wallets/create", walletsCtrl.create);
router.post("/v2/wallets/update", walletsCtrl.update);
router.post("/v2/totp/enable",    totpCtrl.enable);
// etc.

to:

router.post("/v2/wallets/create", stex.controllers.wallets.create)
router.post("/v2/wallets/update", stex.controllers.wallets.update)
router.post("/v2/totp/enable",    stex.controllers.totp.enable);
// etc.
thejollyrogers commented 10 years ago

sgtm :+1:

bartekn commented 10 years ago

:+1: for me too.

nullstyle commented 10 years ago

Implemented with: https://github.com/stellar/stex/commit/352695049fda3749e89a14e77385b2ccd9721ad5