加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
zhanghan 提交于 2023-09-13 15:15 . modify plugin version
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)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化