properly organize and configure gloom
This commit is contained in:
@@ -23,4 +23,6 @@ To run the plugin host, run the following command:
|
||||
|
||||
- `pluginPath` - The path to the plugin to load.
|
||||
- `socketPath` - The path to the socket that the plugin will use to listen for http requests through.
|
||||
- `controlPath` - (Optional) The path to the control socket. If not provided, the host will not send errors or status messages to the control socket and instead log them to stdout and stderr.
|
||||
- `controlPath` - (Optional) The path to the control socket. If not provided, the host will not send errors or status
|
||||
messages to the control socket and instead log them to stdout and stderr. This connection is destroyed when a message
|
||||
is sent to the socket.
|
||||
@@ -46,15 +46,35 @@ func main() {
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, os.Interrupt)
|
||||
|
||||
var listener net.Listener
|
||||
var router *fiber.App
|
||||
|
||||
go func() {
|
||||
<-signalChan
|
||||
// TODO: maybe do something graceful here
|
||||
fmt.Println("Received SIGINT, shutting down...")
|
||||
|
||||
if router != nil {
|
||||
if err := router.Shutdown(); err != nil {
|
||||
log.Printf("Error shutting down router: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if listener != nil {
|
||||
if err := listener.Close(); err != nil {
|
||||
log.Printf("Error closing listener: %v", err)
|
||||
}
|
||||
|
||||
os.Remove(socketPath)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
var writer io.Writer
|
||||
writer = os.Stderr
|
||||
var Print = func(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
fmt.Fprintf(os.Stderr, "\n")
|
||||
}
|
||||
|
||||
if controlPath != "" {
|
||||
fmt.Printf("Waiting for control connection on %s\n", controlPath)
|
||||
|
||||
@@ -72,7 +92,15 @@ func main() {
|
||||
defer conn.Close()
|
||||
|
||||
var ok bool
|
||||
var writer io.Writer
|
||||
writer, ok = conn.(io.Writer)
|
||||
// send the one message we can send and kill the connection
|
||||
Print = func(format string, args ...any) {
|
||||
fmt.Fprintf(writer, format, args...)
|
||||
fmt.Fprintf(writer, "\n")
|
||||
conn.Close()
|
||||
os.Remove(controlPath)
|
||||
}
|
||||
if !ok {
|
||||
log.Printf("Control connection is not a writer")
|
||||
return
|
||||
@@ -80,37 +108,37 @@ func main() {
|
||||
}
|
||||
|
||||
if _, err := os.Stat(socketPath); err == nil {
|
||||
fmt.Fprintf(writer, "Error: Socket %s already exists\n", socketPath)
|
||||
Print("Error: Socket %s already exists", socketPath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
realPluginPath, err := filepath.Abs(pluginPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(writer, "Error: could not get absolute plugin path: %v\n", err)
|
||||
Print("Error: could not get absolute plugin path: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
p, err := plugin.Open(realPluginPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(writer, "Error: could not open plugin %s: %v\n", realPluginPath, err)
|
||||
Print("Error: could not open plugin %s: %v", realPluginPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
symbol, err := p.Lookup("Plugin")
|
||||
if err != nil {
|
||||
fmt.Fprintf(writer, "Error: could not find 'Plugin' symbol in %s: %v\n", realPluginPath, err)
|
||||
Print("Error: could not find 'Plugin' symbol in %s: %v", realPluginPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pluginLib, ok := symbol.(Plugin)
|
||||
if !ok {
|
||||
fmt.Fprintf(writer, "Error: symbol 'Plugin' in %s is not a Plugin interface\n", realPluginPath)
|
||||
Print("Error: symbol 'Plugin' in %s is not a Plugin interface", realPluginPath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pluginConfig, err := pluginLib.Init()
|
||||
if err != nil {
|
||||
fmt.Fprintf(writer, "Error: error initializing plugin %s: %v\n", realPluginPath, err)
|
||||
Print("Error: error initializing plugin %s: %v", realPluginPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -118,21 +146,25 @@ func main() {
|
||||
if pluginConfig != nil {
|
||||
config = *pluginConfig
|
||||
}
|
||||
router := fiber.New(config)
|
||||
router = fiber.New(config)
|
||||
|
||||
pluginLib.RegisterRoutes(router)
|
||||
|
||||
// listen for connections on the socket
|
||||
listener, err := net.Listen("unix", socketPath)
|
||||
listener, err = net.Listen("unix", socketPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(writer, "Error: error listening on socket %s: %v\n", socketPath, err)
|
||||
Print("Error: error listening on socket %s: %v", socketPath, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "ready\n")
|
||||
|
||||
// technically this can still error
|
||||
router.Listener(listener, fiber.ListenConfig{
|
||||
if err := router.Listener(listener, fiber.ListenConfig{
|
||||
DisableStartupMessage: true,
|
||||
})
|
||||
BeforeServeFunc: func(app *fiber.App) error {
|
||||
Print("ready")
|
||||
return nil
|
||||
},
|
||||
}); err != nil {
|
||||
Print("Error: error starting server: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user