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
Add middlewares [Investigation] #23
Comments
Hi @vitorcamachoo caching will not be covered by hemera core. You can create a plugin which exposed a caching interface (Redis, memory ...). PR Welcome :) hemera.add({ topic: 'globalCache', cmd: 'get', key: 'fooBar' }, ....
hemera.add({ topic: 'math', cmd: 'add' }, (req, cb) => {
hemera.act({ topic: 'globalCache', cmd: 'get', key: 'foobar' }, (err, result) => {
if(err) {
return cb(err)
}
if(result) {
return cb(null, result)
}
var r = req.a + req.b
hemera.act({ topic: 'globalCache', cmd: 'set', key: 'foobar', value: r })
cb(null, r)
})
}) Here is a simple example how to do it actually. hemera.add({ topic: 'math', cmd: 'add' }, function (req, cb) {
var key = req.a + '_' + req.b
//memory, redis ...
if(cache[key]) {
return cb(null, cache[key])
}
cb(null, req.a + req.b);
}); |
I close it. Feel free to reopen. Thanks. |
Hiii, I've been thinking about that and I'd love to have the same mechanism of
cache pseudo code:
I could try this implementation, but I image that I will change a lot of hemera core. |
Hi @vitorcamachoo the core should be small as possible. The design principle of hemera is to hide complexity and do only one job right. Functionality can be extended by plugins. Caching should not be land in the core. You can write a caching wrapper on top of hemera to reach your goal. Hemera has extension there you can hook into. Everything is an |
Hii, maybe I didn't explain myself clearly.. Imagine hemera have the possibility to have middlewares, like expressjs have, and with that, we can define whatever we want. The This approach can open hemera to other plugins, like cache, limiter, restrict route, etc etc... But as I said, it's my opinion, and if it's to implement, much of the core would be changed... Thank you. |
Hi I don't want to change the interface of hemera.ext('onClientPreRequest', function (next) {
let self = this // hemera context
next() //accept null or error object
}) |
Of what I know, first you can't answer a request through that event, and second, that's to much generic, if I want to add a specific function behaviour as a middleware for a specific route, I can't. Correct me if I'm wrong. |
I agree that could be useful I am thinking about I will inform you.
hemera.middleware(function(req, reply, next) {
})
hemera.add({
topic: 'math'
})
.middleware(function(req, reply, next) {
}) or similiar WDYT? |
That could be a possibility, in that way, you avoid to change |
@vitorcamachoo its now possible to reply from the extension points Reply an error and finish the request hemera.ext('onServerPreHandler', function (next, i) {
let self = this
next(new Error('fail'))
}) Reply a message and finish the request hemera.ext('onServerPreHandler', function (next, i) {
let self = this
next(null, { msg: 'unauthorized' })
}) Reply a message and continue with the next extension handler hemera.ext('onServerPreRequest', function (next, i) {
let self = this
next(null, { msg: 'unauthorized' }, true)
}) which informations are available you can take from the default extensions. https://github.com/hemerajs/hemera/blob/master/packages/hemera/lib/extensions.js I think this will covering the most use cases. If you want to do preprocessing for add specific things you can do it in the add self. |
Hii, thanks for the changes. Calling Maybe I can create a branch to implement that and create a real complete use case for what I expect. Thanks a lot. |
No calling |
You can manipulate the request with that approach. hemera.ext('onServerPreRequest', function (next, i) {
let req = this._request
req.foo = "bar"
next()
}) Your approach would implicit to create seperate objects for request and response. This would change the whole core but of course you can prepare something. But it must bring a lot improvements in contract to the current solution before I will update all plugins :) |
@vitorcamachoo so finally :) hemera.ext('<serverExtension>', function (req, res, next, prevValue, i) {
next()
})
|
Cool, nice work @StarpTech. |
@vitorcamachoo thanks for the PR but I will close it because I address all problems. Give me feedback thanks. |
I change the core a little to accept what I was trying to say in old posts. You can check that in my repository. I have a little test simulating the use of a 'external lib' with that middleware, in this case, to cache response. It isn't finished, due to the lack of time, but I would like receive your opinion. |
Where can I find this? Can you explain exactly what you want to solve? |
As I said in old posts, the chance to do something like this I've made a simple change on You can find my changes here: https://github.com/vitorcamachoo/hemera Thanks. |
HI @vitorcamachoo I like the idea that you can define middlewares per I also prefer a chaining syntax instead parameter overloading. hemera.add({
topic: 'test',
cmd: 'add'
}).use(function(req, next) {
//process request
})
.end(function(req, cb) {
cb()
}) |
I create a new issue #40 |
Nice work, thanks! |
Hii, is there any way to use cache for certains hemera services.
I'm looking for something that I've used in Express, a middleware function that caches end points.
Thanks!
The text was updated successfully, but these errors were encountered: