代码拉取完成,页面将自动刷新
同步操作将从 openEuler/PilotGo-plugin-grafana 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"fmt"
"net/http/httputil"
"net/url"
"os"
"strings"
"gitee.com/openeuler/PilotGo-plugins/sdk/logger"
"gitee.com/openeuler/PilotGo-plugins/sdk/plugin/client"
"github.com/gin-gonic/gin"
"openeuler.org/PilotGo/grafana-plugin/conf"
)
const Version = "1.0.1"
var PluginInfo = &client.PluginInfo{
Name: "grafana",
Version: Version,
Description: "grafana可视化工具支持",
Author: "guozhengxin",
Email: "guozhengxin@kylinos.cn",
Url: "",
ReverseDest: "",
}
func main() {
fmt.Println("hello grafana")
InitLogger()
PluginInfo.Url = "http://" + conf.Config().Http.Addr + "/plugin/grafana"
PluginInfo.ReverseDest = "http://" + conf.Config().Grafana.Addr
server := gin.Default()
client := client.DefaultClient(PluginInfo)
client.RegisterHandlers(server)
InitRouter(server)
if err := server.Run(conf.Config().Http.Addr); err != nil {
logger.Fatal("failed to run server")
}
}
func InitLogger() {
if err := logger.Init(conf.Config().Logopts); err != nil {
fmt.Printf("logger init failed, please check the config file: %s", err)
os.Exit(-1)
}
}
func InitRouter(router *gin.Engine) {
// 所有grafana请求反向代理到grafana服务器
pg := router.Group("/plugin/" + PluginInfo.Name)
{
pg.Any("/*any", func(c *gin.Context) {
c.Set("__internal__reverse_dest", PluginInfo.ReverseDest)
ReverseProxyHandler(c)
})
}
}
func ReverseProxyHandler(c *gin.Context) {
remote := c.GetString("__internal__reverse_dest")
if remote == "" {
fmt.Println("get reverse dest failed!")
return
}
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.ModifyResponse = func(r *http.Response) error {
// if setCookie := r.Header.Get("Set-Cookie"); setCookie != "" {
// r.Header.Set("Set-Cookie", setCookie+" ; Secure ")
// }
// return nil
// }
proxy.ServeHTTP(c.Writer, c.Request)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。