diff --git a/go.work b/go.work index 2b056256c9e45e2af20603da40bfae7260da10f8..533c8143db7f8359b195fc710827194435bd6411 100644 --- a/go.work +++ b/go.work @@ -3,4 +3,6 @@ go 1.19 use ( ./grafana/ ./prometheus + ./sdk + ./PilotGo ) diff --git a/grafana/go.mod b/grafana/go.mod index 83aaf652834c00201a13631ea68653f08129e572..c2d5fbe47668452a00529789bf02d47d1be162d3 100644 --- a/grafana/go.mod +++ b/grafana/go.mod @@ -1,3 +1,3 @@ -module PilotGo/grafana-plugin +module openeuler.org/PilotGo/grafana-plugin go 1.19 diff --git a/grafana/main.go b/grafana/main.go index b6a025a1adb5ca0d9f20756b33b7b3407027199d..b9964a71e3661a99167b56b9dc006691d81282c8 100644 --- a/grafana/main.go +++ b/grafana/main.go @@ -1,7 +1,22 @@ package main -import "fmt" +import ( + "fmt" + + "openeuler.org/PilotGo/plugin-sdk/plugin" +) + +const Version = "0.0.1" func main() { fmt.Println("hello grafana") + client := plugin.DefaultClient(&plugin.PluginInfo{ + Name: "grafana", + Version: "Version", + Description: "grafana可视化工具支持", + Author: "guozhengxin", + Email: "guozhengxin@kylinos.cn", + }) + + client.Serve() } diff --git a/prometheus/go.mod b/prometheus/go.mod index 68bf1a37a130e204d5ca8dcea5a4dd22a379fc8d..4cc4828924ac9bf713e83d9e893539013941e9f5 100644 --- a/prometheus/go.mod +++ b/prometheus/go.mod @@ -1,3 +1,3 @@ -module PilotGo/prometheus-plugin +module openeuler.org/PilotGo/prometheus-plugin go 1.19 diff --git a/sdk/go.mod b/sdk/go.mod index 95cc393d8736b77ae1d6d67b5efa401572f416a4..b322fe04c15afb0fe2c5fce237e16426d85402b0 100644 --- a/sdk/go.mod +++ b/sdk/go.mod @@ -1,3 +1,3 @@ -module PilotGo/plugin-sdk +module openeuler.org/PilotGo/plugin-sdk go 1.19 diff --git a/sdk/plugin/plugin.go b/sdk/plugin/plugin.go new file mode 100644 index 0000000000000000000000000000000000000000..e2249b12681cb04bb153342bcb18094994bb535b --- /dev/null +++ b/sdk/plugin/plugin.go @@ -0,0 +1,70 @@ +package plugin + +import ( + "fmt" + "net/http" + "net/http/httputil" + "net/url" + "strings" + + "github.com/gin-gonic/gin" +) + +type PluginInfo struct { + Name string `json:"name"` + Version string `json:"version"` + Description string `json:"description"` + Author string `json:"author"` + Email string `json:"email"` + IndexUrl string `json:"index_url"` + ManageUrl string `json:"manage_url"` +} + +type Client struct { + Router *gin.Engine +} + +var BaseInfo *PluginInfo + +func InfoHandler(c *gin.Context) { + + c.JSON(http.StatusOK, BaseInfo) +} + +func ReverseProxyHandler(c *gin.Context) { + fmt.Println("reverse to grafana server") + remote := "http://10.1.167.104:3000/" //转向的host + target, err := url.Parse(remote) + if err != nil { + return + } + + proxy := httputil.NewSingleHostReverseProxy(target) + c.Request.URL.Path = strings.Replace(c.Request.URL.Path, "/plugin/grafana", "", 1) //请求API + + proxy.ServeHTTP(c.Writer, c.Request) +} + +func DefaultClient(desc *PluginInfo) *Client { + BaseInfo = desc + + router := gin.Default() + mg := router.Group("plugin_manage/") + { + mg.GET("/info", InfoHandler) + } + + pg := router.Group("plugin/" + desc.Name) + { + pg.Any("/*any", ReverseProxyHandler) + } + + return &Client{ + Router: router, + } +} + +func (c *Client) Serve() { + // TODO: 启动http服务 + c.Router.Run() +}