2021-09-01 09:38:56 -05:00
|
|
|
//go:build go1.3
|
2021-07-08 07:19:40 -05:00
|
|
|
// +build go1.3
|
|
|
|
|
|
|
|
// Copyright 2014 The Macaron Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
|
|
|
// Package macaron is a high productive and modular web framework in Go.
|
2021-12-13 08:56:14 -06:00
|
|
|
package web
|
2021-07-08 07:19:40 -05:00
|
|
|
|
|
|
|
import (
|
2021-07-25 23:11:43 -05:00
|
|
|
"context"
|
2021-07-08 07:19:40 -05:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2023-01-30 02:25:58 -06:00
|
|
|
_ "unsafe"
|
2021-07-08 07:19:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const _VERSION = "1.3.4.0805"
|
|
|
|
|
|
|
|
const (
|
|
|
|
DEV = "development"
|
|
|
|
PROD = "production"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Env is the environment that Macaron is executing in.
|
|
|
|
// The MACARON_ENV is read on initialization to set this variable.
|
|
|
|
Env = DEV
|
|
|
|
)
|
|
|
|
|
|
|
|
func Version() string {
|
|
|
|
return _VERSION
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler can be any callable function.
|
|
|
|
// Macaron attempts to inject services into the handler's argument list,
|
2021-12-13 08:56:14 -06:00
|
|
|
// and panics if an argument could not be fulfilled via dependency injection.
|
2023-08-30 10:46:47 -05:00
|
|
|
type Handler any
|
2021-07-08 07:19:40 -05:00
|
|
|
|
2022-05-20 11:45:18 -05:00
|
|
|
//go:linkname hack_wrap github.com/grafana/grafana/pkg/api/response.wrap_handler
|
|
|
|
func hack_wrap(Handler) http.HandlerFunc
|
2021-07-08 07:19:40 -05:00
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
// wrapHandler turns any supported handler type into a http.Handler by wrapping it accordingly
|
|
|
|
func wrapHandler(h Handler) http.Handler {
|
2022-05-20 11:45:18 -05:00
|
|
|
return hack_wrap(h)
|
2021-07-08 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Macaron represents the top level web application.
|
|
|
|
// Injector methods can be invoked to map services on a global level.
|
|
|
|
type Macaron struct {
|
2022-08-09 07:58:50 -05:00
|
|
|
// handlers []http.Handler
|
|
|
|
mws []Middleware
|
2021-07-08 07:19:40 -05:00
|
|
|
|
2021-08-30 04:48:34 -05:00
|
|
|
urlPrefix string // For suburl support.
|
2021-07-08 07:19:40 -05:00
|
|
|
*Router
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a bare bones Macaron instance.
|
|
|
|
// Use this method if you want to have full control over the middleware that is used.
|
|
|
|
func New() *Macaron {
|
2021-08-30 04:48:34 -05:00
|
|
|
m := &Macaron{Router: NewRouter()}
|
2021-07-08 07:19:40 -05:00
|
|
|
m.Router.m = m
|
|
|
|
m.NotFound(http.NotFound)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeHandler represents a handler executes at beginning of every request.
|
|
|
|
// Macaron stops future process when it returns true.
|
|
|
|
type BeforeHandler func(rw http.ResponseWriter, req *http.Request) bool
|
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
// macaronContextKey is used to store/fetch web.Context inside context.Context
|
2021-07-25 23:11:43 -05:00
|
|
|
type macaronContextKey struct{}
|
|
|
|
|
|
|
|
// FromContext returns the macaron context stored in a context.Context, if any.
|
|
|
|
func FromContext(c context.Context) *Context {
|
|
|
|
if mc, ok := c.Value(macaronContextKey{}).(*Context); ok {
|
|
|
|
return mc
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:34:56 -05:00
|
|
|
type paramsKey struct{}
|
|
|
|
|
|
|
|
// Params returns the named route parameters for the current request, if any.
|
|
|
|
func Params(r *http.Request) map[string]string {
|
|
|
|
if rv := r.Context().Value(paramsKey{}); rv != nil {
|
|
|
|
return rv.(map[string]string)
|
|
|
|
}
|
|
|
|
return map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetURLParams sets the named URL parameters for the given request. This should only be used for testing purposes.
|
|
|
|
func SetURLParams(r *http.Request, vars map[string]string) *http.Request {
|
|
|
|
return r.WithContext(context.WithValue(r.Context(), paramsKey{}, vars))
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
type Middleware = func(next http.Handler) http.Handler
|
|
|
|
|
|
|
|
// UseMiddleware registers the given Middleware
|
|
|
|
func (m *Macaron) UseMiddleware(mw Middleware) {
|
|
|
|
m.mws = append(m.mws, mw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use registers the provided Handler as a middleware.
|
|
|
|
// The argument may be any supported handler or the Middleware type
|
|
|
|
// Deprecated: use UseMiddleware instead
|
|
|
|
func (m *Macaron) Use(h Handler) {
|
|
|
|
m.mws = append(m.mws, mwFromHandler(h))
|
2021-07-25 23:11:43 -05:00
|
|
|
}
|
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
func mwFromHandler(handler Handler) Middleware {
|
|
|
|
if mw, ok := handler.(Middleware); ok {
|
|
|
|
return mw
|
|
|
|
}
|
|
|
|
|
|
|
|
h := wrapHandler(handler)
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
mrw, ok := w.(*responseWriter)
|
|
|
|
if !ok {
|
|
|
|
mrw = NewResponseWriter(r.Method, w).(*responseWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(mrw, r)
|
|
|
|
if mrw.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context().Value(macaronContextKey{}).(*Context)
|
|
|
|
next.ServeHTTP(ctx.Resp, ctx.Req)
|
|
|
|
})
|
|
|
|
}
|
2021-07-08 07:19:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Context {
|
2022-11-08 09:58:04 -06:00
|
|
|
// NOTE: we have to explicitly copy the middleware chain here to avoid
|
|
|
|
// passing a shared slice to the *Context, which leads to racy behavior in
|
|
|
|
// case of later appends
|
|
|
|
mws := make([]Middleware, len(m.mws))
|
|
|
|
copy(mws, m.mws)
|
|
|
|
|
2021-07-08 07:19:40 -05:00
|
|
|
c := &Context{
|
2022-11-08 09:58:04 -06:00
|
|
|
mws: mws,
|
2022-08-16 05:25:27 -05:00
|
|
|
Resp: NewResponseWriter(req.Method, rw),
|
2021-07-08 07:19:40 -05:00
|
|
|
}
|
2022-05-24 14:35:08 -05:00
|
|
|
|
|
|
|
c.Req = req.WithContext(context.WithValue(req.Context(), macaronContextKey{}, c))
|
2021-07-08 07:19:40 -05:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP is the HTTP Entry point for a Macaron instance.
|
|
|
|
// Useful if you want to control your own HTTP server.
|
|
|
|
// Be aware that none of middleware will run without registering any router.
|
|
|
|
func (m *Macaron) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2021-08-30 04:48:34 -05:00
|
|
|
req.URL.Path = strings.TrimPrefix(req.URL.Path, m.urlPrefix)
|
2021-07-08 07:19:40 -05:00
|
|
|
m.Router.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetURLPrefix sets URL prefix of router layer, so that it support suburl.
|
|
|
|
func (m *Macaron) SetURLPrefix(prefix string) {
|
|
|
|
m.urlPrefix = prefix
|
|
|
|
}
|