-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (105 loc) · 3.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"io"
"net/http"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/ZiXian92/my-helper-bot/plugins"
"github.com/ZiXian92/my-helper-bot/plugins/interfaces"
"github.com/gorilla/mux"
"github.com/hashicorp/go-plugin"
"github.com/rs/zerolog/log"
)
func main() {
// Initialize the logger
log.Info().Msg("Starting my-helper-bot")
// Create a context for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Load plugins
// TODO: Implement a loop to load all enabled plugins found in the application's
// installed plugins folder.
healthCheckPluginClient := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: plugins.HandshakeConfig,
Plugins: plugins.PluginMap,
Cmd: exec.Command("./healthcheck"),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
})
defer healthCheckPluginClient.Kill()
// Create a router using gorilla/mux
router := mux.NewRouter()
// Start an HTTP server
httpServer := &http.Server{
Addr: ":8080",
Handler: router,
}
// Process plugins
healthCheckDispenser, err := healthCheckPluginClient.Client()
if err != nil {
log.Warn().Err(err).Msg("Failed to get healthcheck plugin dispenser.")
} else {
webHandlerPlugin, err := healthCheckDispenser.Dispense(plugins.PluginMapKeyWebHandler)
if err != nil {
log.Warn().Err(err).Msg("Failed to get dispense web handler plugin.")
} else if webHandler, ok := webHandlerPlugin.(interfaces.WebHandler); !ok {
log.Warn().Msg("Web handler plugin does not implement web handler interface.")
} else {
eps := webHandler.GetEndpoints()
log.Info().Any("web endpoints", eps).Msg("Found web endpoints.")
for _, ep := range eps {
router.HandleFunc(ep.Path, func(w http.ResponseWriter, r *http.Request) {
rBodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Err(err).Msg("Error reading HTTP request body.")
http.Error(w, "Error processing request.", http.StatusInternalServerError)
return
}
res := webHandler.HandleRequest(interfaces.WebRequest{
EndPointName: ep.Name,
Headers: r.Header,
URIParams: mux.Vars(r),
QueryParams: r.URL.Query(),
Body: rBodyBytes,
})
for k, vList := range res.Headers {
for _, v := range vList {
w.Header().Add(k, v)
}
}
w.Write(res.Body)
w.WriteHeader(res.Code)
}).Methods(ep.Methods...)
}
}
}
defer healthCheckDispenser.Close()
go func() {
log.Info().Msg("Starting HTTP server")
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("HTTP server stopped unexpectedly")
}
}()
// Listen for OS signals to trigger graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigChan:
log.Info().Msgf("Received signal: %s. Shutting down gracefully...", sig)
cancel()
// Close any resources related to your plugins here
// Wait for a grace period before shutting down
shutdownTimeout := 10 * time.Second
ctx, cancel := context.WithTimeout(ctx, shutdownTimeout)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("Error during HTTP server shutdown")
}
case <-ctx.Done():
// Your plugin-specific shutdown logic can go here
}
log.Info().Msg("Your Go application has gracefully stopped")
}