register.lua 1.62 KB
Newer Older
Yu Gan's avatar
Yu Gan committed
1
local _M = {}
2
local xtracer = require "luaxtrace"
Yu Gan's avatar
Yu Gan committed
3 4 5 6 7 8 9 10 11 12 13

local function _StrIsEmpty(s)
  return s == nil or s == ''
end

function _M.RegisterUser()
  local bridge_tracer = require "opentracing_bridge_tracer"
  local GenericObjectPool = require "GenericObjectPool"
  local UserServiceClient = require 'media_service_UserService'
  local ngx = ngx

14 15
  xtracer.StartLuaTrace("NginxWebServer", "Register")
  xtracer.LogXTrace("Processing Request")
Yu Gan's avatar
Yu Gan committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  local req_id = tonumber(string.sub(ngx.var.request_id, 0, 15), 16)
  local tracer = bridge_tracer.new_from_global()
  local parent_span_context = tracer:binary_extract(ngx.var.opentracing_binary_context)
  local span = tracer:start_span("RegisterUser", {["references"] = {{"child_of", parent_span_context}}})
  local carrier = {}
  tracer:text_map_inject(span:context(), carrier)

  ngx.req.read_body()
  local post = ngx.req.get_post_args()

  if (_StrIsEmpty(post.first_name) or _StrIsEmpty(post.last_name) or
      _StrIsEmpty(post.username) or _StrIsEmpty(post.password)) then
    ngx.status = ngx.HTTP_BAD_REQUEST
    ngx.say("Incomplete arguments")
    ngx.log(ngx.ERR, "Incomplete arguments")
31 32
    xtracer.LogXTrace("Incomplete arguments")
    xtracer.DeleteBaggage()
Yu Gan's avatar
Yu Gan committed
33 34 35 36 37
    ngx.exit(ngx.HTTP_BAD_REQUEST)
  end

  local client = GenericObjectPool:connection(UserServiceClient, "user-service", 9090)

38
  carrier["baggage"] = xtracer.BranchBaggage()
39
  local status, err = client:RegisterUser(req_id, post.first_name, post.last_name,
Yu Gan's avatar
Yu Gan committed
40
      post.username, post.password, carrier)
41 42

  xtracer.JoinBaggage(err.baggage)
Yu Gan's avatar
Yu Gan committed
43 44 45
  GenericObjectPool:returnConnection(client)

  span:finish()
46
  xtracer.DeleteBaggage()
Yu Gan's avatar
Yu Gan committed
47 48
end

49
return _M