-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
79 lines (64 loc) · 1.41 KB
/
plugin.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
package gin_daemon
import (
"context"
"github.com/gin-gonic/gin"
"net"
"net/http"
"os"
)
type Plugin struct {
unixHttpServer *http.Server
l net.Listener
}
func NewPlugin() *Plugin {
p := &Plugin{}
f := os.NewFile(uintptr(3), "")
l, err := net.FileListener(f)
if err != nil {
panic(err)
}
p.l = l
r := gin.Default()
r.Use(RunAsPlugin(p))
unixHttpServer := &http.Server{
Handler: r,
}
p.unixHttpServer = unixHttpServer
return p
}
func (p *Plugin) Run() error {
err := p.unixHttpServer.Serve(p.l)
if err != nil {
return err
}
return nil
}
func (p *Plugin) Stop() {
p.unixHttpServer.Shutdown(context.TODO())
}
func RunAsPlugin(plugin *Plugin) gin.HandlerFunc {
return func(c *gin.Context) {
/* TODO
xx/stop 停止父进程,子进程的心跳根据父进程的pid判断,是否要优雅关闭
xx/reload 新创建一个子进程,父进程把请求打给新进程,老的子进程优雅关闭
父进程监听http请求
子进程监听http请求
钩子,当子进程成功创建,父进程执行握手的钩子
unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test")
defer os.Remove(unixTestSocket)
r := New()
r.RunUnix(unixTestSocket)
c, err := net.Dial("unix", unixTestSocket)
*/
fullPath := c.FullPath()
switch fullPath {
case "/health_check":
c.JSON(200, gin.H{
"message": "ok",
})
return
case "/stop":
plugin.Stop()
}
}
}