Skip to content

Commit b9a3740

Browse files
committed
Поправил rockspec.
Добавил аргумент корневого каталога.
1 parent fca2a8d commit b9a3740

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- `BACKLOG` - количество одновременных запросов серверу
2525
- `ROOT_DIR` - корневая директория сервера
2626

27+
Если Вы установили сервер через `luarocks`, запустить его можно командой `http_server <root_dir>`. Вписав вместо `<root_dir>` полный путь к желаемой папке с файлами.
28+
2729
## Пример
2830

2931
Файлы в папке:

http_server-dev-1.rockspec

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ dependencies = {
1313
}
1414
build = {
1515
type = "builtin",
16-
modules = {
17-
},
16+
modules = {},
1817
install = {
1918
bin = {
2019
http_server = "http_server.lua"
21-
},
22-
lua = {
23-
["aliases"] = "aliases.lua",
2420
}
2521
}
2622
}

http_server.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/lua
22
local socket = require("socket")
33
local ltn12 = require("ltn12")
4+
local args = {...}
45

56
--{{Options
67
---The port number for the HTTP server. Default is 80
@@ -13,6 +14,8 @@ BACKLOG=5
1314
ROOT_DIR="./"
1415
--}}Options
1516

17+
local root_dir = args[1] or ROOT_DIR
18+
1619
-- Загружаем короткие имена, если есть.
1720
local aliases = {}
1821

@@ -64,7 +67,7 @@ local function read_request (client)
6467
local reading = true
6568
while reading do
6669
local header_line, err = client:receive("*l")
67-
reading = (header_line ~= "")
70+
reading = (header_line ~= "" and header_line ~= nil)
6871
if reading then
6972
table.insert(raw_headers, header_line)
7073
local key, value = string.match (header_line, "(%g+): ([%g ]+)")
@@ -150,9 +153,9 @@ while 1 do
150153
env.request = request
151154
env.response = response
152155

153-
f, err = loadfile(ROOT_DIR..request.filename, "t", env) -- загрузка скрипта
156+
f, err = loadfile(root_dir..request.filename, "t", env) -- загрузка скрипта
154157
if f then
155-
stat, ret = pcall(f, request) -- выполняем скрипт
158+
stat, ret = pcall(f) -- выполняем скрипт
156159
if stat then
157160
tmp_f:seek ("set")
158161
f = tmp_f
@@ -168,7 +171,7 @@ while 1 do
168171
response.mess = "Open file error"
169172
end
170173
else -- иначе
171-
f = io.open(ROOT_DIR..request.filename) -- открываем файл для чтения
174+
f = io.open(root_dir..request.filename) -- открываем файл для чтения
172175
end
173176
end
174177

0 commit comments

Comments
 (0)