Skip to content

Commit

Permalink
reimplement startup mechanism and env module
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Mar 15, 2024
1 parent c30091c commit b128b93
Show file tree
Hide file tree
Showing 42 changed files with 821 additions and 920 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ SRC_FILE = \
silly_timer.c \
silly_run.c \
silly_daemon.c \
silly_env.c \
silly_malloc.c \
silly_log.c \
silly_trace.c \
Expand Down Expand Up @@ -120,7 +119,7 @@ $(LUACLIB_PATH)/test.so: $(LIB_PATH)/lualib-test.c | $(LUACLIB_PATH)
test: CCFLAG += -fsanitize=address -fno-omit-frame-pointer -DSILLY_TEST
test: LDFLAG += -fsanitize=address -fno-omit-frame-pointer
test: $(PLATS)
./$(TARGET) test/test.conf
./$(TARGET) test/test.lua --lualib_path="test/?.lua"

clean:
-rm $(SRC:.c=.o) *.so $(TARGET)
Expand Down
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,16 @@
- `make`
- `make TLS=off` (disable TLS function)

## Configuration

- `daemon`, 1 --> run as daemon, 0 --> normal
- `bootstrap`, lua entry file
- `lualib_path`, will append the package.path (in luaVM)
- `lualib_cpath`, will append the package.cpath (int luaVM)
- `logpath`, if running in daemon mode, all print messages will be written to the `[logpath]/silly-[pid].log` file
- `loglevel`, is used to control the log level limits, it can be set to `debug`, `info`, `warn`, or `error`, default is `info`
- `pidfile`, if running in daemon mode, `pidfile` will used by run only once in a system

## Running
./silly <config>
./silly <main.lua> [options]

## Options
./silly -h

## Test

- Tests are in the test folder
- Run `./silly test/test.conf` to test all modules
- Run `./silly test/test.lua --lualib_path="test/?.lua` to test all modules

## Examples

Expand Down
2 changes: 1 addition & 1 deletion examples/patch.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local console = require "sys.console"
local run = require "patch.run"
local run = require "examples.patch.run"

console {
addr = "127.0.0.1:2345",
Expand Down
2 changes: 1 addition & 1 deletion examples/socket.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local core = require "sys.core"
local crypto = require "sys.crypto"
local socket = require "sys.socket"
local socket = require "sys.net.tcp"

socket.listen("127.0.0.1:9999", function(fd, addr)
print("accept", fd, addr)
Expand Down
4 changes: 2 additions & 2 deletions examples/start.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash

module() {
./silly --lualib_path="lualib/?.lua;examples/?.lua" --lualib_cpath="luaclib/?.so" --bootstrap="examples/$1.lua"
./silly "examples/$1.lua"
}

all() {
array=($(ls examples/*.lua))
for i in "${array[@]}"; do
echo "---------------$i-------------------"
timeout 5 ./silly --lualib_path="lualib/?.lua;examples/?.lua" --lualib_cpath="luaclib/?.so" --bootstrap="$i"
timeout 5 ./silly "$i"
done
exit 0
}
Expand Down
30 changes: 21 additions & 9 deletions examples/websocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ local core = require "sys.core"
local crypto = require "sys.crypto"
local websocket = require "http.websocket"

local handler = function(sock)
local dat, typ = sock:read()
print("server read:", dat, typ)
sock:write(dat, "pong")
sock:close()
end


websocket.listen {
port = "127.0.0.1:9999",
tls_port = "127.0.0.1:8888",
tls_cert = "test/cert.pem",
tls_key = "test/key.pem",
handler = function(sock)
local dat, typ = sock:read()
print("server read:", dat, typ)
sock:write(dat, "pong")
sock:close()
end
handler = handler
}

websocket.listen {
tls = true,
port = "127.0.0.1:8888",
certs = {
{
cert = "test/cert.pem",
cert_key = "test/key.pem",
}
},
handler = handler,
}

core.start(function()
Expand Down
114 changes: 101 additions & 13 deletions lualib-src/lualib-env.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,131 @@

#include "silly.h"
#include "compiler.h"
#include "silly_env.h"
#include "silly_log.h"
#include "silly_worker.h"

static const char *load_config = "\
local env, file = ...\
local config = {}\
local function eval(parent, tbl)\
for k, v in pairs(tbl) do\
if #parent > 0 then\
k = parent .. '.' .. tostring(k)\
end\
if type(v) == 'table' then\
eval(k, v)\
elseif not env[k] then\
env[k] = v\
end\
end\
return t\
end\
local function include(name)\
local f = io.open(name, 'r')\
if not f then\
error('open config error of file:' .. name)\
end\
local code = f:read('a')\
if not code then\
error('read config error of file:' .. name)\
end\
f:close()\
assert(load(code, name, 't', config))()\
return \
end\
config.include = include\
config.ENV = os.getenv\
include(file)\
config.include = nil\
config.ENV = nil\
eval('', config)\
";

static const char *
skipcode(const char *str)
{
while (*str && *str++ != ']')
;
if (*str)
str += 3;
return str;
}

static int
lget(lua_State *L)
lload(lua_State *L)
{
const char *key = luaL_checkstring(L, 1);
const char *value = silly_env_get(key);
if (value)
lua_pushstring(L, value);
else
int err;
err = luaL_loadstring(L, load_config);
assert(err == LUA_OK);
lua_pushvalue(L, lua_upvalueindex(1)); //env_table
lua_pushvalue(L, 1);
err = lua_pcall(L, 2, 0, 0);
if (err != LUA_OK) {
const char *err = lua_tostring(L, -1);
err = skipcode(err);
lua_pushstring(L, err);
lua_replace(L, -2);
} else {
lua_pushnil(L);
}
return 1;
}

static int
lget(lua_State *L)
{
lua_pushvalue(L, lua_upvalueindex(1)); //env_table
lua_pushvalue(L, 1);
lua_gettable(L, -2);
return 1;
}

static int
lset(lua_State *L)
{
const char *key = luaL_checkstring(L, 1);
const char *value = luaL_checkstring(L, 2);
silly_env_set(key, value);
lua_pushvalue(L, lua_upvalueindex(1)); //env_table
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_settable(L, -3);
return 0;
}

static void
load_args(lua_State *L)
{
int i, argc;
char **argv;
argv = silly_worker_args(&argc);
for (i = 0; i < argc; i++) {
const char *k, *v;
char *str = argv[i];
if (strlen(str) <= 2 || str[0] != '-' || str[1] != '-') {
continue;
}
k = strtok(&str[2], "=");
v = strtok(NULL, "=");
if (k != NULL && v != NULL) {
lua_pushstring(L, k);
lua_pushstring(L, v);
lua_settable(L, -3);
}
}
}

int
luaopen_sys_env(lua_State *L)
{
luaL_Reg tbl[] = {
{"load", lload},
{"get", lget},
{"set", lset},
//end
{NULL, NULL},
};

luaL_checkversion(L);
luaL_newlib(L, tbl);
luaL_newlibtable(L,tbl);
lua_newtable(L);
load_args(L);
luaL_setfuncs(L,tbl,1);
return 1;
}

Loading

0 comments on commit b128b93

Please sign in to comment.