Skip to content

Commit

Permalink
Make http and url global.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwindy committed Aug 6, 2023
1 parent deb2772 commit 97b993f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ var Loads = []Load{

art.Preload,
bcrypt.Preload,
http.Preload,
mail.Preload,
odbc.Preload,
pwdchecker.Preload,
re.Preload,
readline.Preload,
uuid.Preload,
url.Preload,
urlpath.Preload,

base64.Preload,
Expand All @@ -56,7 +54,9 @@ var Loads = []Load{
}

var Loaders = map[string]Loader{
"http": http.Loader,
"json": json.Loader,
"url": url.Loader,
}

func Open(L *lua.LState) {
Expand All @@ -65,7 +65,7 @@ func Open(L *lua.LState) {
}
for name, loader := range Loaders {
if loader(L) != 1 {
panic("loader must return only one value")
panic("loader must return and only return one value")
}
obj := L.Get(1)
L.SetGlobal(name, obj)
Expand Down
4 changes: 2 additions & 2 deletions lib/url/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Decode(L *lua.LState) int {
return 1
}

var URLExports = map[string]lua.LGFunction{
var urlAPI = map[string]lua.LGFunction{
"resolve": URLJoin,
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func URLGet(L *lua.LState) int {
case "username":
L.Push(lua.LString(u.User.Username()))
default:
if v, ok := URLExports[k]; ok {
if v, ok := urlAPI[k]; ok {
L.Push(L.NewFunction(v))
} else {
L.Push(lua.LNil)
Expand Down
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ var (
var DefaultPidFile = "mirai.pid"

func main() {
time.Sleep(100 * time.Millisecond)
app := cli.NewApp()
app.Usage = "Server for the Mirai Project"
app.Version = fmt.Sprintf("%s %s", version, build)
Expand All @@ -104,16 +103,16 @@ func main() {
{
Name: "start",
Usage: "Start the server (default)",
Description: "Start command finds project.lua in the specified project path. \n" +
"Then, it starts the server based on the configuration. \n" +
Description: "Start command finds project.lua in the specified project path.\n" +
"Then, it starts the server based on the configuration.\n" +
"If it is not found, it will set up a temporary server and database and enter interactive mode.",
ArgsUsage: "arguments are passed to Lua scripts without parsing",
SkipFlagParsing: true,
Action: start,
},
{
Name: "run",
Usage: "Run command",
Usage: "Run command specified in the project.lua.",
ArgsUsage: "arguments are passed to Lua scripts without parsing",
SkipFlagParsing: true,
Action: run,
Expand Down Expand Up @@ -216,7 +215,7 @@ func worker(ctx *cli.Context, c config.Config) error {

app := fiber.
New(fiber.Config{
ServerHeader: "Mirai Server",
ServerHeader: servername,
DisableStartupMessage: true,
})
app.
Expand Down Expand Up @@ -366,6 +365,7 @@ func worker(ctx *cli.Context, c config.Config) error {
Register("db", ledb.New(c.DB)).
Register("cli", lecli.New(ctx.Args().Slice(), colors)).
Run(c.Index)
defer G.Close()

if err := G.Err(); err != nil {
return err
Expand Down Expand Up @@ -415,6 +415,7 @@ func startInteractive(ctx *cli.Context) {
Register("app", leapp.New(capp)).
Register("db", ledb.New(db)).
Register("cli", lecli.New(ctx.Args().Slice(), colors))
defer G.Close()
if err := G.Err(); err != nil {
fail("%v\n", err)
}
Expand All @@ -441,6 +442,7 @@ func run(ctx *cli.Context) error {
Register("db", ledb.New(c.DB)).
Register("cli", lecli.New(args.Tail(), colors)).
Run(cmd)
defer G.Close()
if err := G.Err(); err != nil {
fail("%v\n", err)
}
Expand Down
4 changes: 1 addition & 3 deletions types/libraries/http.lua → types/globals/http.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@meta

---@class httplib
local http = {}
http = {}

--- 发送请求。
---@param verb string 方法
Expand Down Expand Up @@ -94,5 +94,3 @@ function Request:auth(username, password) end
---@param key string
---@param value string
function Request.set(key, value) end

return http
4 changes: 1 addition & 3 deletions types/libraries/url.lua → types/globals/url.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@meta

---@class uuidlib
local url = {}
url = {}

--- 解析 URL。
---@param input string 要解析的绝对或相对的输入网址。
Expand Down Expand Up @@ -89,5 +89,3 @@ URL.username = ""
---@param path string 路径
---@return url_URL
function URL.join(path) end

return url

0 comments on commit 97b993f

Please sign in to comment.