@@ -11,6 +11,7 @@ local Class = require(PATH .. ".lib.class")
1111local errorhandler = require (PATH .. " .error_handler" )
1212local get_current_dir = require (PATH .. " .utils" ).get_current_dir
1313local Performance = require (PATH .. " .plugins.performance" )
14+ local FeatherPluginManager = require (PATH .. " .plugin_manager" )
1415
1516local performance = Performance ()
1617
@@ -75,6 +76,7 @@ function Feather:init(config)
7576 self .autoRegisterErrorHandler = conf .autoRegisterErrorHandler or false
7677 self .plugins = conf .plugins or {}
7778 self .lastDelivery = 0
79+ self .lastError = 0
7880 self .observers = {}
7981
8082 if not self .debug then
@@ -126,6 +128,8 @@ function Feather:init(config)
126128 selfRef .print (self , ... )
127129 end
128130 end
131+
132+ self .pluginManager = FeatherPluginManager (self , logs )
129133end
130134
131135function Feather :__buildResponse (body )
@@ -148,15 +152,24 @@ function Feather:__getConfig()
148152 root_path = root_path .. " /" .. self .baseDir
149153 end
150154
155+ local pluginKeys = {}
156+
157+ for _ , plugin in ipairs (self .plugins ) do
158+ table.insert (pluginKeys , plugin .identifier )
159+ end
160+
151161 local config = {
152- plugins = self . plugins ,
162+ plugins = pluginKeys ,
153163 root_path = root_path ,
154164 version = FEATHER_VERSION ,
155165 }
156166
157167 return config
158168end
159169
170+ --- Builds a request object from a raw request string
171+ --- @param request string
172+ --- @return table
160173function Feather :__buildRequest (request )
161174 local method , pathWithQuery = request :match (" ^(%u+)%s+([^%s]+)" )
162175 local path , queryString = pathWithQuery :match (" ^([^?]+)%??(.*)$" )
@@ -196,28 +209,6 @@ function Feather:__defaultObservers()
196209 self :observe (" Lua Version" , _VERSION )
197210end
198211
199- --- Tracks the value of a key in the observers table
200- --- @alias FeatherObserve fun ( self : Feather , key : string , value : table | string | number | boolean )
201- --- @type FeatherObserve
202- function Feather :observe (key , value )
203- if not self .debug then
204- return
205- end
206-
207- self .observers = self .observers or {}
208-
209- local curr = self :__format (value )
210-
211- for _ , observer in ipairs (self .observers ) do
212- if observer .key == key then
213- observer .value = curr
214- return
215- end
216- end
217-
218- table.insert (self .observers , { key = key , value = curr })
219- end
220-
221212function Feather :__errorTraceback (msg )
222213 local trace = debug.traceback ()
223214
@@ -253,18 +244,6 @@ function Feather:__errorTraceback(msg)
253244 return p
254245end
255246
256- --- @alias FeatherClear fun ( self : Feather )
257- --- @type FeatherClear
258- function Feather :clear ()
259- logs = {}
260- end
261-
262- --- @alias FeatherFinish fun ( self : Feather )
263- --- @type FeatherFinish
264- function Feather :finish ()
265- self :log ({ type = " feather:finish" })
266- end
267-
268247function Feather :__onerror (msg , finish )
269248 if not self .debug then
270249 return
@@ -276,12 +255,49 @@ function Feather:__onerror(msg, finish)
276255 self .logger (" [Feather] ERROR: " .. err )
277256 end
278257 self .lastError = os.time ()
258+ self .pluginManager :onerror (msg , self )
279259
280260 if finish then
281261 self :finish ()
282262 end
283263end
284264
265+ --- Tracks the value of a key in the observers table
266+ --- @alias FeatherObserve fun ( self : Feather , key : string , value : table | string | number | boolean )
267+ --- @type FeatherObserve
268+ function Feather :observe (key , value )
269+ if not self .debug then
270+ return
271+ end
272+
273+ self .observers = self .observers or {}
274+
275+ local curr = self :__format (value )
276+
277+ for _ , observer in ipairs (self .observers ) do
278+ if observer .key == key then
279+ observer .value = curr
280+ return
281+ end
282+ end
283+
284+ table.insert (self .observers , { key = key , value = curr })
285+ end
286+
287+ --- @alias FeatherClear fun ( self : Feather )
288+ --- @type FeatherClear
289+ function Feather :clear ()
290+ logs = {}
291+ end
292+
293+ --- @alias FeatherFinish fun ( self : Feather )
294+ --- @type FeatherFinish
295+ function Feather :finish ()
296+ self :log ({ type = " feather:finish" })
297+
298+ self .pluginManager :finish (self )
299+ end
300+
285301--- @alias FeatherError fun ( self : Feather , msg : string )
286302--- @type FeatherError
287303function Feather :error (msg )
@@ -342,11 +358,19 @@ function Feather:update(dt)
342358 response = self :__buildResponse (body )
343359 end
344360
361+ if request .path == " /plugins" then
362+ local pluginResponse = self .pluginManager :handleRequest (request , self )
363+
364+ local body = json .encode (pluginResponse )
365+ response = self :__buildResponse (body )
366+ end
367+
345368 client :send (response )
346369 end
347370
348371 client :close ()
349372 end
373+ self .pluginManager :update (dt , self )
350374end
351375
352376--- @alias FeatherTrace fun ( self : Feather , ... )
0 commit comments