-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
55 lines (48 loc) · 1.18 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
package main
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path"
"github.com/heedy/heedy/api/golang/plugin"
"github.com/heedy/heedy/plugins/dashboard/backend/dashboard"
"github.com/sirupsen/logrus"
)
func main() {
logrus.Info(fmt.Sprintf("%s plugin starting", dashboard.PluginName))
p, err := plugin.Init()
if err != nil {
logrus.Error(err)
os.Exit(1)
}
err = p.InitSQL(dashboard.PluginName, dashboard.SQLVersion, dashboard.SQLUpdater)
if err != nil {
p.Logger().Error(fmt.Errorf("Failed to set up database: %w", err))
os.Exit(1)
}
pluginMiddleware := plugin.NewMiddleware(p, dashboard.Handler)
server := http.Server{
Handler: pluginMiddleware,
}
sockPath := fmt.Sprintf("%s.sock", dashboard.PluginName)
unixListener, err := net.Listen("unix", path.Join(p.Meta.DataDir, sockPath))
if err != nil {
p.Logger().Error(fmt.Errorf("Failed to listen on socket: %w", err))
p.Close()
os.Exit(1)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
server.Close()
}
}()
p.Logger().Info("Plugin Ready")
server.Serve(unixListener)
p.Logger().Debug("Closing")
p.Close()
os.Remove(path.Join(p.Meta.DataDir, sockPath))
}