Moonshark/moonshark.go

55 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"Moonshark/modules/http"
"Moonshark/state"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <script.lua>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
scriptPath := os.Args[1]
// Create state configured for the script
luaState, err := state.NewFromScript(scriptPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer luaState.Close()
// Execute the script
if err := luaState.ExecuteFile(scriptPath); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Check if HTTP servers are running
if http.HasActiveServers() {
// Set up signal handling for graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("HTTP servers running. Press Ctrl+C to exit.")
// Wait for either signal or servers to close
go func() {
<-sigChan
fmt.Println("\nShutting down...")
os.Exit(0)
}()
// Wait for all servers to finish
http.WaitForServers()
}
}