Skip to content

Commit

Permalink
http: replace io with fio module
Browse files Browse the repository at this point in the history
In tarantool the standart built-in module for working with files is "fio".
So we should use it instead of "io".

Patch adds a commit 'Get rid of io module'
(e0d8f82) that was implemented for http
v2 and later reverted in scope of issue with discard v2.

Follows up #90
Part of #134
  • Loading branch information
ligurio committed Oct 26, 2021
1 parent 2677dea commit ad500ea
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

local lib = require('http.lib')

local io = io
local fio = require('fio')
local require = require
local package = package
local mime_types = require('http.mime_types')
Expand Down Expand Up @@ -349,8 +349,17 @@ local function load_template(self, r, format)


local tpl = catfile(self.options.app_dir, 'templates', file)
local fh = io.input(tpl)
local template = fh:read('*a')
local fh, err = fio.open(tpl)
if err ~= nil then
errorf("Can not load template for '%s': '%s'", r.path, err)
end

local template
template, err = fh:read()
if err ~= nil then
errorf("Can not load template for '%s': '%s'", r.path, err)
end

fh:close()

if self.options.cache_templates then
Expand Down Expand Up @@ -540,14 +549,18 @@ local function static_file(self, request, format)
}
end

local s, fh = pcall(io.input, file)
local fh, err = fio.open(file, {'O_RDONLY'})
if err ~= nil then
return { status = 404 }
end

if not s then
return { status = 404 }
end
local body
body, err = fh:read()
if err ~= nil then
errorf("Can not return static file for '%s': '%s'", request:path(), err)
end

local body = fh:read('*a')
io.close(fh)
fh:close()

if self.options.cache_static then
self.cache.static[ file ] = body
Expand Down

0 comments on commit ad500ea

Please sign in to comment.