Moonscript如何显示复选框

“/checkbok”: => @url_for: =>

  @html -> 
    form method: "POST", action: @url_for(), ->
      input type: "hidden",
        name: "csrf_token", value: @csrf_token
      input type: "checkbox", name: "username"
      input type: "checkbox", name: "password"

作者:糖果

MoonScript for GrayLog是之前写的一个基于Lapis与Simple HTTP的Graylog日志查询SDK, 支持Stream查询,最近为了做自动化分析,加入了新的接口中调用功能,加入了对Dashboard widgets和更新与查询,通过这个程序,实现一些反扫逻辑。


    @putRequest:(req_url, data) =>
        http = require "resty.http"
        httpc = http.new()
        metadata = {
          method:"PUT",
          body: data,
          headers: self.headers_info
        }

        res, err = httpc\request_uri(req_url, metadata)

        if not res
          ngx.say("failed to request: ", err)
          return
        return res.body


    @updateWidget: (dashboardId, widgetId,jsonBody) =>
        errList = {}
        if type(dashboardId) == 'nil'
            table.insert(errList, "dashboard id is nil\n")

        if type(widgetId) == 'nil'
            table.insert(errList, "widget id is nil\n")

        if type(jsonBody) == 'nil'
            table.insert(errList, "json body is nil\n")

        num = table.getn(errList) 
        if num > 0 
            return errList


        url = "http://"..self.host..":"..self.port
        req_url = url..'/dashboards/'..dashboardId..'/widgets/'..widgetId

        self.headers_info = {
            'Authorization': self.auth, 
            'Accept': '*/*',
            'Content-Type':'application/json'
        }

        self\putRequest req_url, jsonBody
        return 1


    @getRequest:(req_url) =>
        http = require "resty.http"
        httpc = http.new()
        metadata = {
          method:"GET",
          headers: self.headers_info
        }

        res, err = httpc\request_uri(req_url, metadata)

        if not res
          ngx.say("failed to request: ", err)
          return

        ngx.status = res.status
        return res.body


    @getWidgetValue: (dashboardId, widgetId) =>
        errList = {}
        if type(dashboardId) == 'nil'
            table.insert(errList, "dashboard id is nil\n")

        if type(widgetId) == 'nil'
            table.insert(errList, "widget id is nil\n")

        num = table.getn(errList) 
        if num > 0 
            return errList

        url = "http://"..self.host..":"..self.port
        req_url = url..'/dashboards/'..dashboardId..'/widgets/'..widgetId..'/value'

        self.headers_info = {
            'Authorization': self.auth, 
            'Accept': 'application/json',
        }

        ret = self\getRequest req_url
        return ret
        

这次没有使用过去端末加JSON数据请求的方式,把simple http换成了RESTY-HTTP,项目名改 了,叫“Finder”。

本文请不要用于商业目地,非商业转载请署名原作者与原文链接。 https://www.moonscript.cn/openresty/resty-http-for-graylog/

作者:糖果

在Lapis中处理GET、POST、PUT。

import respond_to from require "lapis.application"
class App extends lapis.Application
  @enable "etlua"
  "/login": respond_to {
    GET: =>
      return "login get"
    POST: =>
      return "login post"
    PUT: =>
      return "login put"
  }

GET、POST、PUT的使用一目了然。

在另一个路由中,调用GraylogSDK访问自己的/login方法,用PUT方法。


headers_info = {
    'Authorization': auth, 
    'Accept': '*/*',
    'Content-Type':'application/json'
}

class RestyGraylog 
    @putRequest:(req_url, data) =>
        http = require "resty.http"
        httpc = http.new()
        metadata = {
          method:"PUT",
          body: data,
          headers: self.headers_info
        }

        res, err = httpc\request_uri(req_url, metadata)
  
        if not res
          ngx.say("failed to request: ", err)
          return
          
        ngx.status = res.status  
        return res.body

在这个版本的Graylog for MoonScript ,没用使用internal proxy的方式,使用的是RESTY-HTTP 来完成这个工作,其达到的效果都是一样的。

本文请不要用于商业目地,非商业转载请署名原作者与原文链接。 https://www.moonscript.cn/lapis%E6%A1%86%E6%9E%B6/lapis-put-method/

作者:糖果

MoonScript库模板代码:

local *

configs = {}

config = (env, fn) ->
  print(env, fn)
  return 1


setmetatable {
    :config
}, {
    __call:(...) => config ...

Lua库模板代码:

local configs, config
configs = { }
config = function(env, fn)
  return 1
end
return setmetatable({
  config = config
}, {
  __call = function(self, ...)
    return config(...)
  end
})

MoonScript如何引用库

config = require "lib"

print(type(config))

for k,v in pairs(config)
    print(k,v)

config('pa', 'pb')
config:config('pa', 'pb')
config.config('pa', 'pb')
config\config('pa', 'pb')

Lua如何引用库

local config = require("lib")
print(type(config))
for k, v in pairs(config) do
  print(k, v)
end
config('pa', 'pb')
local _ = {
  config = config('pa', 'pb')
}
config.config('pa', 'pb')
return config:config('pa', 'pb')

返回结果

table
config  function: 0x11204b0
pa      pb
pa      pb
pa      pb
table: 0x10005c0        pa

注意一下“.”、“:”这两种调用的区别,“:”调用时会将自己作为第一个参数传给被 调用函数第一个形参。

上文提到的例子还是把函数作为元素包在table变量返回,而MoonScript本身是支持类的,所 以直接用class形式声明的库。

MoonScript类库模板代码:

class log
    @output: =>
        print("log")

Lua类库模板代码:

local log
do
  local _class_0
  local _base_0 = {
    output = function(self)
      return print("log")
    end
  }
  _base_0.__index = _base_0
  _class_0 = setmetatable({
    __init = function() end,
    __base = _base_0,
    __name = "log"
  }, {
    __index = _base_0,
    __call = function(cls, ...)
      local _self_0 = setmetatable({}, _base_0)
      cls.__init(_self_0, ...)
      return _self_0
    end
  })
  _base_0.__class = _class_0
  log = _class_0
  return _class_0
end

调用库

log = require "1lib"
print(type(log))
for k,v in pairs(log) 
    print(k,v)
log\output()

调用结果

table
__name  log
__init  function: 0x110a250
output  function: 0x110a0a0
__base  table: 0x10aa5b0
log

有了class关键字后,后发现require返回的变量依然是table,但多了几个变量,__name, __init初始化函数,__base基类表,自定义函数output。

对于没有继承关系库用table方式封装库直接,需要类依赖的库用class相对方便一些,不过最终 一切类都是table转化形式。

本文请不要用于商业目地,非商业转载请署名原作者与原文链接。 https://www.moonscript.cn/lapis%E6%A1%86%E6%9E%B6/lapis-lib/

作者:糖果

MoonScript代码:

board = { 
  ['0.0.0.1']:1,
  ['0.0.0.5']:5,
  ['0.0.0.3']:3,
  ['0.0.0.2']:2,
  ['0.0.0.9']:9,
  ['0.0.3.3']:3,
  ['0.0.0.6']:6
}


mapsort = (board)->
  b_len = 0 
  for k,v in pairs(board)
    b_len = b_len + 1 
  
  a1 = {}
  a2 = {}

  i = 0 
  for k,v in pairs(board)
    i = i + 1 
    a1[i] = k 
    a2[i] = v 

  for i = 1, b_len
     max = a2[i]
     for j = i + 1, b_len 
       if a2[j] > max 
         tmp = a2[j]
         a2[j] = max
         a2[i] = tmp
         max = tmp

         tmp1 = a1[j]
         a1[j]  = a1[i]
         a1[i] = tmp1

  ret = {}
  for k,v in ipairs(a1) 
    ret[k] = {a1[k], a2[k]}
  return ret


print("########")

ret = mapsort board

for k,v in ipairs(ret) 
  print(ret[k][1], ret[k][2])

LUA代码:

local board = { 
  ['0.0.0.1'] = 1,
  ['0.0.0.5'] = 5,
  ['0.0.0.3'] = 3,
  ['0.0.0.2'] = 2,
  ['0.0.0.9'] = 9,
  ['0.0.0.3'] = 3,
  ['0.0.0.6'] = 6 
}
local mapsort
mapsort = function(board)
  local b_len = 0 
  for k, v in pairs(board) do
    b_len = b_len + 1 
  end 
  local a1 = { } 
  local a2 = { } 
  local i = 0 
  for k, v in pairs(board) do
    i = i + 1 
    a1[i] = k 
    a2[i] = v 
  end 

  for i = 1, b_len do
    local max = a2[i]
    for j = i + 1, b_len do
      if a2[j] > max then
        local tmp = a2[j]
        a2[j] = max 
        a2[i] = tmp
        max = tmp
        local tmp1 = a1[j]
        a1[j] = a1[i]
        a1[i] = tmp1
      end
    end
  end
  local ret = { }
  for k, v in ipairs(a1) do
    ret[k] = {
      a1[k],
      a2[k]
    }
  end
  return ret
end

print("########")
local ret = mapsort(board)
for k, v in ipairs(ret) do
  print(ret[k][1], ret[k][2])
end