2018-01-31 07:05:24 -06:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-02-04 10:29:16 -06:00
|
|
|
"fmt"
|
2018-01-31 07:05:24 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2018-03-14 09:27:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-01-31 07:05:24 -06:00
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2018-01-31 07:05:24 -06:00
|
|
|
slug := c.Params("slug")
|
|
|
|
|
|
|
|
if slug != "" {
|
|
|
|
if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil {
|
2018-02-04 10:29:16 -06:00
|
|
|
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
|
2018-01-31 07:05:24 -06:00
|
|
|
c.Redirect(url, 301)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RedirectFromLegacyDashboardSoloUrl() macaron.Handler {
|
2018-03-07 10:54:50 -06:00
|
|
|
return func(c *m.ReqContext) {
|
2018-01-31 07:05:24 -06:00
|
|
|
slug := c.Params("slug")
|
2018-03-14 09:27:18 -05:00
|
|
|
renderRequest := c.QueryBool("render")
|
2018-01-31 07:05:24 -06:00
|
|
|
|
|
|
|
if slug != "" {
|
|
|
|
if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil {
|
2018-03-14 09:27:18 -05:00
|
|
|
if renderRequest && strings.Contains(url, setting.AppSubUrl) {
|
|
|
|
url = strings.Replace(url, setting.AppSubUrl, "", 1)
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:05:24 -06:00
|
|
|
url = strings.Replace(url, "/d/", "/d-solo/", 1)
|
2018-02-04 10:29:16 -06:00
|
|
|
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
|
2018-01-31 07:05:24 -06:00
|
|
|
c.Redirect(url, 301)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|