From 57263b7a3c9f6430a3ac5bee6308749177c937b5 Mon Sep 17 00:00:00 2001 From: Gzx1999 Date: Tue, 18 Oct 2022 18:23:38 +0800 Subject: [PATCH] add basic plugin defines and grafana plugin implement --- go.work | 2 ++ grafana/go.mod | 2 +- grafana/main.go | 17 ++++++++++- prometheus/go.mod | 2 +- sdk/go.mod | 2 +- sdk/plugin/plugin.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 sdk/plugin/plugin.go diff --git a/go.work b/go.work index 2b056256..533c8143 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 83aaf652..c2d5fbe4 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 b6a025a1..b9964a71 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 68bf1a37..4cc48289 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 95cc393d..b322fe04 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 00000000..e2249b12 --- /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() +} -- Gitee