totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

HTTP server #577

Closed michalCapo closed 6 years ago

michalCapo commented 6 years ago

Hello,

I want to integrate horizon with totaljs. But I dont know how to get http server instance from total.js.

When i'm trying to get F.server i got:

/home/capo/n/bin/node[20610]: ../src/util-inl.h:243:TypeName* node::Unwrap(v8::Local<v8::Object>) [with TypeName = node::LibuvStreamWrap]: Assertion `(object->InternalFieldCount()) > (0)' failed.
 1: node::Abort() [total: total.js]
 2: node::Assert(char const* const (*) [4]) [total: total.js]
 3: void node::StreamBase::GetBytesRead<node::LibuvStreamWrap>(v8::Local<v8::String>, v8::PropertyCallbackInfo<v8::Value> const&) [total: total.js]
 4: v8::internal::PropertyCallbackArguments::Call(void (*)(v8::Local<v8::Name>, v8::PropertyCallbackInfo<v8::Value> const&), v8::internal::Handle<v8::internal::Name>) [total: total.js]
 5: v8::internal::Object::GetPropertyWithAccessor(v8::internal::LookupIterator*) [total: total.js]
 6: v8::internal::Object::GetProperty(v8::internal::LookupIterator*) [total: total.js]
 7: v8::internal::JSReceiver::GetOwnPropertyDescriptor(v8::internal::LookupIterator*, v8::internal::PropertyDescriptor*) [total: total.js]
 8: v8::internal::JSReceiver::GetOwnPropertyDescriptor(v8::internal::Isolate*, v8::internal::Handle<v8::internal::JSReceiver>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyDescriptor*) [total: total.js]
 9: v8::internal::Builtin_ObjectGetOwnPropertyDescriptor(int, v8::internal::Object**, v8::internal::Isolate*) [total: total.js]
10: 0x2dd2fa8441d
petersirka commented 6 years ago

Hi @michalCapo, can I see your initialization code?

michalCapo commented 6 years ago
const options = {};
require('total.js').http('release', options);
F.on('load', function () {
    F.server
});
petersirka commented 6 years ago

And where is the horizon integration?

michalCapo commented 6 years ago

Hmm i'm really sorry. It just happens only when i try "watch it" in my IDE (webstorm). Just printing it to console console.log(F.server) it works.

Right now there is no integration code. I was just trying find out 'how it could be done'.

petersirka commented 6 years ago

Does horizon use WebSocket?

michalCapo commented 6 years ago

Yes, internally.

I'm sorry for this issue, but at the end it works like a charm:

First you need to start rethinkdb

const options = {};

require('total.js').http('release', options);

F.on('load', function () {
    const horizon = require('@horizon/server');

    const options = {
        project_name: 'myProject',
        auto_create_collection: true,
        auto_create_index: true,
        auth: {
            token_secret: 'my_super_secret_secret'
        }
    };
    const horizonServer = horizon(F.server, options);

    console.log('Listening on port 8181.');
});
michalCapo commented 6 years ago

Hmm but debug doesn't work:

const options = {};

require('total.js/debug')(options);

F.on('load', function () {
    const horizon = require('@horizon/server');

    const options = {
        project_name: 'myProject',
        auto_create_collection: true,
        auto_create_index: true,
        auth: {
            token_secret: 'my_super_secret_secret'
        }
    };
    const horizonServer = horizon(F.server, options);

    console.log('Listening on port 8181.');
});

exception:

ReferenceError: F is not defined
    at Object.<anonymous> (/home/capo/my/code/emptyproject-website-master/debug.js:18:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
-------------------------> DEBUG PID: 31468 (v2.8.0)
ReferenceError: F is not defined
    at Object.<anonymous> (/home/capo/my/code/emptyproject-website-master/debug.js:18:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
petersirka commented 6 years ago

You need to create a definition file e.g. definitions/horizon.js:

const Horizon = require('@horizon/server');
const Options = {
    project_name: 'myProject',
    auto_create_collection: true,
    auto_create_index: true,
    auth: {
        token_secret: 'my_super_secret_secret'
    }
};

ON('load', function() {
    F.global.horizon = Horizon(F.server, Options);
    // or global.horizon = Horizon(...)
});

Total.js/Debug creates a separated thread, it's a bit complicated to explain it.

michalCapo commented 6 years ago

ok, thanks. BTW: impresive response time.

petersirka commented 6 years ago

Thank you, Sometimes I'm faster, but today is weekend.