Giter VIP home page Giter VIP logo

Comments (8)

GUI avatar GUI commented on August 30, 2024 2

Well, better late than never, but this should finally be easier in the v0.12.0 release. You can now access the redis connection via auto_ssl.storage.adapter:get_connection(), and the auto_ssl instance is passed as the second argument to the allow_domain callback. Thanks for the idea!

from lua-resty-auto-ssl.

GUI avatar GUI commented on August 30, 2024

Not a dumb question at all!

You could establish a new redis connection inside the allow_domain callback, but it doesn't look like we have a super-easy way to reuse the same Redis connection that the storage mechanism would already have instantiated. But I think that's a good idea, so we could certainly look at ways to better expose that part of the storage API to the allow_domain callback for a future release (or pull requests are always welcome).

In the meantime, here's a workaround that I think might work to allow you to leverage the same connection in both places:

local function get_redis_instance(redis_options)
  local instance = ngx.ctx.auto_ssl_redis_instance
  if instance then
    return instance
  end

  instance = redis:new()
  local ok, err

  if redis_options["socket"] then
    ok, err = instance:connect(redis_options["socket"])
  else
    ok, err = instance:connect(redis_options["host"], redis_options["port"])
  end
  if not ok then
    return false, err
  end

  if redis_options["auth"] then
    ok, err = instance:auth(redis_options["auth"])
    if not ok then
      return false, err
    end
  end

  ngx.ctx.auto_ssl_redis_instance = instance
  return instance
end

auto_ssl = (require "resty.auto-ssl").new()
local redis_options = {
  host = "10.10.10.1"
}
auto_ssl:set("redis", redis_options)
auto_ssl:set("allow_domain", function(domain)
  local redis_instance, instance_err = get_redis_instance(redis_options)
  if instance_err then
    return nil, instance_err
  end

  local res, err = redis_instance:get("WHATEVER...")
  -- Do whatever else you need with redis_instance.
end)
auto_ssl:init()

It's a bit ugly, but basically I'm just copying the private get_redis_instance method out of the Redis storage adapter: https://github.com/GUI/lua-resty-auto-ssl/blob/v0.10.3/lib/resty/auto-ssl/storage_adapters/redis.lua#L5-L32 Since both functions will cache the redis connection on the ngx.ctx.auto_ssl_redis_instance context variable, then which ever version of get_redis_instance gets called first should be the only one that actually establishes a connection.

But again, I think providing a cleaner way to get at this connection object directly from the storage API would be better long term.

from lua-resty-auto-ssl.

waynerobinson avatar waynerobinson commented on August 30, 2024

Thanks for this! I'll check it out.

from lua-resty-auto-ssl.

 avatar commented on August 30, 2024

Hey @GUI , has anything been done to enable this ?

from lua-resty-auto-ssl.

luto avatar luto commented on August 30, 2024

@aviatrix I don't know of any changes regarding this. Right now nobody knows what reusing that object would even look like, for a users point of view. Is there an interface idea you'd like to work out and propose? I'll be glad to give you some pointers on how to implement it.

from lua-resty-auto-ssl.

 avatar commented on August 30, 2024

@luto Right now i've settled for the get_redis_instance example given by @GUI in the comments above, however that is sub optimal in my opinion. On first thought maybe exposing the get_redis_instance as public static method should suffice so we can call that function inside the auto_ssl like this : auto_ssl:get_redis_instance(). I'm not familiar with Lua or how it works or if this is even possible.

from lua-resty-auto-ssl.

 avatar commented on August 30, 2024

One small caveat : if you don't put local redis = require "resty.redis" on the top of init_by_lua_block there is a chance it will blow up when trying to init redis :)

from lua-resty-auto-ssl.

gohai avatar gohai commented on August 30, 2024

@GUI Is it possible to get to ssl_options through the auto_ssl instance? Thanks!

from lua-resty-auto-ssl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.