Modern Web Framework - Integrating Flowbite and Radix.
This repository combines several Lua libraries to enhance functionality for various systems:
-
Matching System:
-
Session Manager System:
- lua-resty-session or lua-resty-cookie, you may set it in
src/configs/site_setting.lua
- lua-resty-session or lua-resty-cookie, you may set it in
-
Upload System:
-
Template System:
-
Utility Tools:
To manage dependencies efficiently, three repositories are included within the requirements
folder:
- base-cencoding
- argon2
- radixtree
I have modified the radixtree
library to introduce a feature that allows global routes with non-blocking high performance.
For the lua-resty-post
library, I have modified the multipart upload logic. Now, it always returns a number array, ensuring consistent structure whether one or multiple files are uploaded simultaneously.
This guide help you reimplement these features in other locations. Let's break it down into sections:
- File Upload
- File Download
- Stream Download
Here's a comprehensive guide:
The file upload functionality is implemented in the routes01_POST
function in the routes/admins.lua
file.
Key steps:
a) Set up the upload configuration:
local post = resty_post:new({
path = "src/uploads/",
chunk_size = 10240,
no_tmp = true,
name = function(name, field)
return jit_uuid()
end
})
b) Read the uploaded files:
local m = post:read()
c) Process the uploaded files:
for _, file_info in ipairs(m.files.uploads) do
local new_file = {
uuid = file_info.tmp_name,
name = file_info.name,
type = file_info.type,
size = file_info.size
}
insert(data, new_file)
end
d) Store the file information:
local success, err = uploads:set("uploads", cjson.encode(data))
The file download functionality is implemented in the routes00_GET
function in the routes/download.lua
file.
Key steps:
a) Retrieve the file UUID from the URL:
local uuid = opts.matched.uuid
b) Find the original filename:
local uploads_data = uploads:get("uploads")
local data = cjson.decode(uploads_data)
local filename = ""
for k, v in ipairs(data) do
if v.uuid == uuid then
filename = v.name
break
end
end
c) Perform an internal redirect to serve the file:
return {type = "exec", url = concat({"/internal/serve_file/", uuid, "?filename=", filename}, "")}
The stream download functionality is implemented in the routes01_GET
function in the routes/download.lua
file.
Key steps:
a) Set the filename:
local filename = ngx_escape_uri("def.pdf")
b) Return the stream data:
return {type = "steam", filename = filename, data = "1234567890"}
The rax.lua
file contains the main routing logic and response handlers. To implement these features:
- Add route handlers in the
routes
table:
routes = table_merge(routes, download.routes)
- Implement response handlers:
local response_handlers = {
-- ... other handlers ...
steam = function(raw)
ngx.header['Content-Type'] = 'application/octet-stream'
ngx.header['Content-Disposition'] = concat({'attachment; filename="' , raw.filename , '"' }, '')
print(raw.data)
end,
exec = function(raw)
return exec(raw.url or "/")
end,
-- ... other handlers ...
}
- Handle the response in the
handle_response
function:
local function handle_response(raw, lang, user_data)
if type(raw) == "table" then
local handler = response_handlers[raw.type]
if handler then
handler(raw, lang, user_data)
end
end
end
- Ensure the necessary dependencies are available (e.g.,
resty.post
,resty.jit-uuid
,ngx.shared.uploads
). - Set up the appropriate routes in your routing configuration.
- Implement the upload, download, and stream functions following the patterns shown above.
- Adjust the file storage location and shared dictionary names as needed for your specific implementation.
- Ensure proper error handling and security measures are in place, such as file type validation and user authentication.
- "ngx.shared.uploads" acts like database, you can use redis or other database (for example PostgreSQL) to store the file information.
- "resty.post" is a library for handling HTTP POST requests, you can use other libraries for this purpose.
- "resty.jit-uuid" is a library for generating UUIDs, you can use other libraries for this purpose.
- "ngx.escape_uri" is a function for escaping URI components, you can use other functions for this purpose.
- "ngx.header" is a table for setting HTTP headers, you can use other methods for this purpose.
- "print" is a function for printing output to the console, you can use other methods for this purpose.
- "exec" is a function for executing a URL, you can use other methods for this purpose.
- "table_merge" is a function for merging tables, you can use other functions for this purpose.
- "cjson" is a library for encoding and decoding JSON data, you can use other libraries for this purpose.
- "ipairs" is a function for iterating over the elements of a table, you can use other functions for this purpose.
- "concat" is a function for concatenating strings, you can use other functions for this purpose.
- "insert" is a function for inserting elements into a table, you can use other functions for this purpose.
- "type" is a function for checking the type of a variable, you can use other functions for this purpose.
- "ngx.header['Content-Type']" is used to set the content type of the response, you can use other methods for this purpose.
- "ngx.header['Content-Disposition']" is used to set the content disposition of the response, you can use other methods for this purpose.
- "ngx.shared.uploads" is a shared dictionary for storing file information, you can use other methods for this purpose.
- "resty.post" is a library for handling HTTP POST requests, you can use other libraries for this purpose.
- "resty.jit-uuid" is a library for generating UUIDs, you can use other libraries for this purpose.
- "ngx.escape_uri" is a function for escaping URI components, you can use other functions for this purpose.
Remember to adapt the code to your specific needs and environment, and always prioritize security when handling file uploads and downloads.