Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps to decouple routes from app.js #97

Merged
merged 7 commits into from Jul 16, 2017

Conversation

FlorianSW
Copy link
Member

At the moment, all routes are setup and handled inside the main entry point app.js. This works great from a functional point of view, but makes the file big, hard to read and understand and therefore hard to maintain. The whole app.js file is also already a big container of code, which makes it even harder to write and review changes in it. A more separated objeect/module structure for handlers, initializers and callbacks would make it easier to maintain the code and bring new features into it.

This is a first step of achieving this (hopefully), by, more or less:

  • Moving all routes to a new module (Routes), which is, in it's current size, an intermediate step only.
  • Extracting callbacks, routes and functions to their own objects (paired with other functions, which does another task in the same area), e.g. a new class RequestTracker and the mobile phone registration services

The next steps in the next time will be to move on this way. However, I stopped here for now, as it's already quite a big PR (and I would understand, if no-one want to review it, however, I hope someone will take the time to do :]).

This is just a first step, which will result in a cleaner app.js entry
point, which just instructs other, more streamlined, objects when they've
to do the "real" work. Routes are a big part of it.

This commit for now only extracts the routes to a Routes object, which
currently is as big as the app.js routes part and will be minimized in
future commits.
These services are used to register a mobile device to the currently logged
in user account. By this, the methods are decoupled from the routes.
@FlorianSW
Copy link
Member Author

This is a quite big PR, however, if I can help in any way to better understand what I did or to answer questions, feel free to ask and comment :]

@marziman
Copy link
Contributor

@FlorianSW,
I reserved myself some time to review this. I will contact you if I got lost or need your feedback. Thanks alot!

@marziman marziman self-assigned this May 22, 2017
@digitaldan
Copy link
Contributor

@FlorianSW you even have tests!!!!! Awesome! I'll give this a shot today!

@digitaldan
Copy link
Contributor

Hi I'm getting this error when I first startup node, I have not looked at what's causing the error yet.

2017-05-27T16:21:44.202Z - error: uncaughtException: Cannot read property 'isAuthenticated' of undefined date=Sat May 27 2017 16:21:44 GMT+0000 (UTC), pid=30630, uid=1001, gid=1001, cwd=/home/openhabcloud/openhabcloud, execPath=/home/openhabcloud/.nvm/versions/node/v7.5.0/bin/node, version=v7.5.0, argv=[/home/openhabcloud/.nvm/versions/node/v7.5.0/bin/node, /home/openhabcloud/openhabcloud/app.js], rss=138780672, heapTotal=105340928, heapUsed=72720872, external=18246039, loadavg=[0.080078125, 0.0166015625, 0.00537109375], uptime=12082768, trace=[column=12, file=/home/openhabcloud/openhabcloud/routes/index.js, function=Routes.ensureAuthenticated, line=249, method=ensureAuthenticated, native=false, column=52, file=/home/openhabcloud/openhabcloud/routes/index.js, function=Routes.setupAppRoutes, line=240, method=setupAppRoutes, native=false, column=10, file=/home/openhabcloud/openhabcloud/routes/index.js, function=Routes.setupRoutes, line=57, method=setupRoutes, native=false, column=4, file=/home/openhabcloud/openhabcloud/app.js, function=, line=337, method=null, native=false, column=32, file=module.js, function=Module._compile, line=571, method=_compile, native=false, column=10, file=module.js, function=Object.Module._extensions..js, line=580, method=Module._extensions..js, native=false, column=32, file=module.js, function=Module.load, line=488, method=load, native=false, column=12, file=module.js, function=tryModuleLoad, line=447, method=null, native=false, column=3, file=module.js, function=Function.Module._load, line=439, method=Module._load, native=false, column=10, file=module.js, function=Module.runMain, line=605, method=runMain, native=false, column=7, file=bootstrap_node.js, function=run, line=418, method=null, native=false, column=9, file=bootstrap_node.js, function=startup, line=139, method=null, native=false, column=3, file=bootstrap_node.js, function=null, line=533, method=null, native=false], stack=[TypeError: Cannot read property 'isAuthenticated' of undefined, at Routes.ensureAuthenticated (/home/openhabcloud/openhabcloud/routes/index.js:249:12), at Routes.setupAppRoutes (/home/openhabcloud/openhabcloud/routes/index.js:240:52), at Routes.setupRoutes (/home/openhabcloud/openhabcloud/routes/index.js:57:10), at Object. (/home/openhabcloud/openhabcloud/app.js:337:4), at Module._compile (module.js:571:32), at Object.Module._extensions..js (module.js:580:10), at Module.load (module.js:488:32), at tryModuleLoad (module.js:447:12), at Function.Module._load (module.js:439:3), at Module.runMain (module.js:605:10), at run (bootstrap_node.js:418:7), at startup (bootstrap_node.js:139:9), at bootstrap_node.js:533:3]
2017-05-27T16:21:44.204Z - error:

@marziman
Copy link
Contributor

marziman commented Jun 2, 2017

Hi @FlorianSW ,

I was checking this PR, since I was making up my mind to bring in Google Home Actions.
While this I wanted to base on some clean ups and just found, that this PR is the way we can go.
So I wanted to base on this "clean up" to continue.

Short remarks:
Can it be, that your IDE made some code completion-tricks? I ve checked your function in index.js called setupAppRoutes... where there are some checks that need "ensureRestAuthenticated". There was only ensureAuthenticated() . Did you add the brackets or was that a unintentionally? I ve removed the empty signatures and it worked fine. Can you check if this is correct?
(If this is ok, do you want me to make a PR or you can fix it?)
This will also fix the uncaught exception which dan mentioned above (unless I am wrong).

Thanks a lot..

@FlorianSW
Copy link
Member Author

Hi @marziman,

just a quick note: I already read your comment, but had no time to look over the code, yet. It's totally possible, that my IDE tricked me, so I'll recheck all usages and correct them where necessary. Please give me some time for it, as I'm not sure, when in the next days I find the time to do that :/ Sorry for the delay!

Best,
Florian

@marziman
Copy link
Contributor

marziman commented Jun 7, 2017

@FlorianSW,
absolutely no problem. Dont worry! I am also pretty busy the next 2 weeks. Take your time.
Thanks & Greetings

@FlorianSW
Copy link
Member Author

@marziman Ok, yeah, this was an error, however, not from my IDE it seems, I just got confused while merging these changes in, iirc. However, I fixed it and merged the latest changes in :)

@digitaldan
Copy link
Contributor

Sorry about being away for so long! I will try and test again today.

@digitaldan
Copy link
Contributor

Throwing this error now

2017-07-16T14:01:19.172Z - error: uncaughtException: Cannot read property 'send' of undefined date=Sun Jul 16 2017 14:01:19 GMT+0000 (UTC), pid=12792, uid=1001, gid=1001, cwd=/home/openhabcloud/openhabcloud, execPath=/home/openhabcloud/.nvm/versions/node/v7.5.0/bin/node, version=v7.5.0, argv=[/home/openhabcloud/.nvm/versions/node/v7.5.0/bin/node, /home/openhabcloud/openhabcloud/app.js], rss=135163904, heapTotal=105340928, heapUsed=81062968, external=19355751, loadavg=[0.0615234375, 0.01513671875, 0.00390625], uptime=16394343, trace=[column=8, file=/home/openhabcloud/openhabcloud/routes/api.js, function=Object.exports.notificationssettingsget, line=36, method=exports.notificationssettingsget, native=false, column=127, file=/home/openhabcloud/openhabcloud/routes/index.js, function=Routes.setupAppRoutes, line=240, method=setupAppRoutes, native=false, column=10, file=/home/openhabcloud/openhabcloud/routes/index.js, function=Routes.setupRoutes, line=57, method=setupRoutes, native=false, column=4, file=/home/openhabcloud/openhabcloud/app.js, function=, line=317, method=null, native=false, column=32, file=module.js, function=Module._compile, line=571, method=_compile, native=false, column=10, file=module.js, function=Object.Module._extensions..js, line=580, method=Module._extensions..js, native=false, column=32, file=module.js, function=Module.load, line=488, method=load, native=false, column=12, file=module.js, function=tryModuleLoad, line=447, method=null, native=false, column=3, file=module.js, function=Function.Module._load, line=439, method=Module._load, native=false, column=10, file=module.js, function=Module.runMain, line=605, method=runMain, native=false, column=7, file=bootstrap_node.js, function=run, line=418, method=null, native=false, column=9, file=bootstrap_node.js, function=startup, line=139, method=null, native=false, column=3, file=bootstrap_node.js, function=null, line=533, method=null, native=false], stack=[TypeError: Cannot read property 'send' of undefined, at Object.exports.notificationssettingsget (/home/openhabcloud/openhabcloud/routes/api.js:36:8), at Routes.setupAppRoutes (/home/openhabcloud/openhabcloud/routes/index.js:240:127), at Routes.setupRoutes (/home/openhabcloud/openhabcloud/routes/index.js:57:10), at Object. (/home/openhabcloud/openhabcloud/app.js:317:4), at Module._compile (module.js:571:32), at Object.Module._extensions..js (module.js:580:10), at Module.load (module.js:488:32), at tryModuleLoad (module.js:447:12), at Function.Module._load (module.js:439:3), at Module.runMain (module.js:605:10), at run (bootstrap_node.js:418:7), at startup (bootstrap_node.js:139:9), at bootstrap_node.js:533:3]

@FlorianSW
Copy link
Member Author

My fault (seems to be a merge error from my side :(). We really need some smoke tests to catch these kind of problems :/ I probably take a look into this in the next time and figure out how we can do that :) However, I'll fix that now.

The function should not be called, but should be passed as a callback
app.all().
@digitaldan
Copy link
Contributor

That looks good, I just went through a quick smoke test routes seem correct.

We really need some smoke tests to catch these kind of problems

Agreed, it would be nice to have something like travis ci running tests on every PR and merge.

@digitaldan digitaldan merged commit 791f2c0 into openhab:master Jul 16, 2017
@FlorianSW
Copy link
Member Author

I opened #113, so that we don't forget that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants