69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"Moonshark/http"
|
|
|
|
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
fmt.Fprintf(os.Stderr, "Usage: %s <lua_file>\n", os.Args[0])
|
|
os.Exit(1)
|
|
}
|
|
|
|
luaFile := os.Args[1]
|
|
|
|
if _, err := os.Stat(luaFile); os.IsNotExist(err) {
|
|
fmt.Fprintf(os.Stderr, "Error: File '%s' not found\n", luaFile)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Create long-lived LuaJIT state
|
|
L := luajit.New(true)
|
|
if L == nil {
|
|
fmt.Fprintf(os.Stderr, "Error: Failed to create Lua state\n")
|
|
os.Exit(1)
|
|
}
|
|
defer func() {
|
|
L.Cleanup()
|
|
L.Close()
|
|
}()
|
|
|
|
// Register HTTP functions
|
|
if err := http.RegisterHTTPFunctions(L); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error registering HTTP functions: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Execute the Lua file
|
|
if err := L.DoFile(luaFile); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Handle return value for immediate exit
|
|
if L.GetTop() > 0 {
|
|
if L.IsNumber(1) {
|
|
exitCode := int(L.ToNumber(1))
|
|
if exitCode != 0 {
|
|
os.Exit(exitCode)
|
|
}
|
|
} else if L.IsBoolean(1) && !L.ToBoolean(1) {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// Keep running for HTTP server
|
|
fmt.Println("Script executed. Press Ctrl+C to exit.")
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
<-c
|
|
fmt.Println("\nShutting down...")
|
|
}
|