grafana/pkg/middleware/dashboard_redirect.go

55 lines
1.3 KiB
Go
Raw Normal View History

package middleware
import (
"fmt"
"strings"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
)
2018-03-22 16:13:46 -05:00
func getDashboardURLBySlug(orgID int64, slug string) (string, error) {
query := m.GetDashboardQuery{Slug: slug, OrgId: orgID}
if err := bus.Dispatch(&query); err != nil {
return "", m.ErrDashboardNotFound
}
return m.GetDashboardUrl(query.Result.Uid, query.Result.Slug), nil
}
2018-03-22 06:37:35 -05:00
func RedirectFromLegacyDashboardURL() macaron.Handler {
2018-03-07 10:54:50 -06:00
return func(c *m.ReqContext) {
slug := c.Params("slug")
if slug == "" {
return
}
if url, err := getDashboardURLBySlug(c.OrgId, slug); err == nil {
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
c.Redirect(url, 301)
}
}
}
2018-03-22 16:13:46 -05:00
func RedirectFromLegacyDashboardSoloURL() macaron.Handler {
2018-03-07 10:54:50 -06:00
return func(c *m.ReqContext) {
slug := c.Params("slug")
renderRequest := c.QueryBool("render")
if slug == "" {
return
}
if url, err := getDashboardURLBySlug(c.OrgId, slug); err == nil {
if renderRequest && strings.Contains(url, setting.AppSubUrl) {
url = strings.Replace(url, setting.AppSubUrl, "", 1)
}
url = strings.Replace(url, "/d/", "/d-solo/", 1)
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
c.Redirect(url, 301)
}
}
}