Chore: replace macaron with web package (#40136)

* replace macaron with web package

* add web.go
This commit is contained in:
Serge Zaitsev
2021-10-11 14:30:59 +02:00
committed by GitHub
parent 78ebac0116
commit 57fcfd578d
70 changed files with 369 additions and 375 deletions
+20 -20
View File
@@ -57,17 +57,17 @@ import (
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/grafana/grafana/pkg/web"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/macaron.v1"
)
type HTTPServer struct {
log log.Logger
macaron *macaron.Macaron
web *web.Mux
context context.Context
httpSrv *http.Server
middlewares []macaron.Handler
middlewares []web.Handler
PluginContextProvider *plugincontext.Provider
RouteRegister routing.RouteRegister
@@ -136,8 +136,8 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
socialService social.Service, oauthTokenService oauthtoken.OAuthTokenService,
encryptionService encryption.Service, searchUsersService searchusers.Service,
dataSourcesService *datasources.Service) (*HTTPServer, error) {
macaron.Env = cfg.Env
m := macaron.New()
web.Env = cfg.Env
m := web.New()
hs := &HTTPServer{
Cfg: cfg,
@@ -177,7 +177,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
tracingService: tracingService,
internalMetricsSvc: internalMetricsSvc,
log: log.New("http.server"),
macaron: m,
web: m,
Listener: opts.Listener,
SocialService: socialService,
OAuthTokenService: oauthTokenService,
@@ -196,7 +196,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
return hs, nil
}
func (hs *HTTPServer) AddMiddleware(middleware macaron.Handler) {
func (hs *HTTPServer) AddMiddleware(middleware web.Handler) {
hs.middlewares = append(hs.middlewares, middleware)
}
@@ -209,7 +209,7 @@ func (hs *HTTPServer) Run(ctx context.Context) error {
host := strings.TrimSuffix(strings.TrimPrefix(hs.Cfg.HTTPAddr, "["), "]")
hs.httpSrv = &http.Server{
Addr: net.JoinHostPort(host, hs.Cfg.HTTPPort),
Handler: hs.macaron,
Handler: hs.web,
ReadTimeout: hs.Cfg.ReadTimeout,
}
switch hs.Cfg.Protocol {
@@ -386,15 +386,15 @@ func (hs *HTTPServer) applyRoutes() {
// start with middlewares & static routes
hs.addMiddlewaresAndStaticRoutes()
// then add view routes & api routes
hs.RouteRegister.Register(hs.macaron)
hs.RouteRegister.Register(hs.web)
// then custom app proxy routes
hs.initAppPluginRoutes(hs.macaron)
hs.initAppPluginRoutes(hs.web)
// lastly not found route
hs.macaron.NotFound(middleware.ReqSignedIn, hs.NotFoundHandler)
hs.web.NotFound(middleware.ReqSignedIn, hs.NotFoundHandler)
}
func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
m := hs.macaron
m := hs.web
m.Use(middleware.RequestTracing())
@@ -420,7 +420,7 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
m.SetURLPrefix(hs.Cfg.AppSubURL)
}
m.UseMiddleware(macaron.Renderer(filepath.Join(hs.Cfg.StaticRootPath, "views"), "[[", "]]"))
m.UseMiddleware(web.Renderer(filepath.Join(hs.Cfg.StaticRootPath, "views"), "[[", "]]"))
// These endpoints are used for monitoring the Grafana instance
// and should not be redirected or rejected.
@@ -444,7 +444,7 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
}
}
func (hs *HTTPServer) metricsEndpoint(ctx *macaron.Context) {
func (hs *HTTPServer) metricsEndpoint(ctx *web.Context) {
if !hs.Cfg.MetricsEndpointEnabled {
return
}
@@ -464,7 +464,7 @@ func (hs *HTTPServer) metricsEndpoint(ctx *macaron.Context) {
}
// healthzHandler always return 200 - Ok if Grafana's web server is running
func (hs *HTTPServer) healthzHandler(ctx *macaron.Context) {
func (hs *HTTPServer) healthzHandler(ctx *web.Context) {
notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead
if notHeadOrGet || ctx.Req.URL.Path != "/healthz" {
return
@@ -480,7 +480,7 @@ func (hs *HTTPServer) healthzHandler(ctx *macaron.Context) {
// apiHealthHandler will return ok if Grafana's web server is running and it
// can access the database. If the database cannot be accessed it will return
// http status code 503.
func (hs *HTTPServer) apiHealthHandler(ctx *macaron.Context) {
func (hs *HTTPServer) apiHealthHandler(ctx *web.Context) {
notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead
if notHeadOrGet || ctx.Req.URL.Path != "/api/health" {
return
@@ -513,19 +513,19 @@ func (hs *HTTPServer) apiHealthHandler(ctx *macaron.Context) {
}
}
func (hs *HTTPServer) mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) {
headers := func(c *macaron.Context) {
func (hs *HTTPServer) mapStatic(m *web.Mux, rootDir string, dir string, prefix string) {
headers := func(c *web.Context) {
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
}
if prefix == "public/build" {
headers = func(c *macaron.Context) {
headers = func(c *web.Context) {
c.Resp.Header().Set("Cache-Control", "public, max-age=31536000")
}
}
if hs.Cfg.Env == setting.Dev {
headers = func(c *macaron.Context) {
headers = func(c *web.Context) {
c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
}
}