mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
* Upgrade buffalo * Switch to go modules everywhere * Fixes from buffalo fix * Add missing modules from module list * Update vendored modules in /vendor * Stop using vendor directory for tests * Check go.mod and go.sum files on verify * Upgrade Buffalo from v0.13.0 to v0.13.1 * Fix test for new Buffalo Allow for new Buffalo code * Add test for endpoint with trailing slash
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package buffalo
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/gobuffalo/envy"
|
|
"github.com/gobuffalo/events"
|
|
"github.com/gorilla/mux"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// App is where it all happens! It holds on to options,
|
|
// the underlying router, the middleware, and more.
|
|
// Without an App you can't do much!
|
|
type App struct {
|
|
Options
|
|
// Middleware returns the current MiddlewareStack for the App/Group.
|
|
Middleware *MiddlewareStack `json:"-"`
|
|
ErrorHandlers ErrorHandlers `json:"-"`
|
|
ErrorMiddleware MiddlewareFunc `json:"-"`
|
|
router *mux.Router
|
|
moot *sync.RWMutex
|
|
routes RouteList
|
|
root *App
|
|
children []*App
|
|
filepaths []string
|
|
}
|
|
|
|
// Muxer returns the underlying mux router to allow
|
|
// for advance configurations
|
|
func (a *App) Muxer() *mux.Router {
|
|
return a.router
|
|
}
|
|
|
|
// New returns a new instance of App and adds some sane, and useful, defaults.
|
|
func New(opts Options) *App {
|
|
events.LoadPlugins()
|
|
envy.Load()
|
|
opts = optionsWithDefaults(opts)
|
|
|
|
a := &App{
|
|
Options: opts,
|
|
ErrorHandlers: ErrorHandlers{
|
|
404: defaultErrorHandler,
|
|
500: defaultErrorHandler,
|
|
},
|
|
router: mux.NewRouter(),
|
|
moot: &sync.RWMutex{},
|
|
routes: RouteList{},
|
|
children: []*App{},
|
|
}
|
|
|
|
dem := a.defaultErrorMiddleware
|
|
if a.ErrorMiddleware != nil {
|
|
dem = a.ErrorMiddleware
|
|
}
|
|
a.Middleware = newMiddlewareStack(dem)
|
|
|
|
notFoundHandler := func(errorf string, code int) http.HandlerFunc {
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
c := a.newContext(RouteInfo{}, res, req)
|
|
err := errors.Errorf(errorf, req.Method, req.URL.Path)
|
|
a.ErrorHandlers.Get(code)(code, err, c)
|
|
}
|
|
}
|
|
|
|
a.router.NotFoundHandler = notFoundHandler("path not found: %s %s", 404)
|
|
a.router.MethodNotAllowedHandler = notFoundHandler("method not found: %s %s", 405)
|
|
|
|
if a.MethodOverride == nil {
|
|
a.MethodOverride = MethodOverride
|
|
}
|
|
a.Use(a.PanicHandler)
|
|
a.Use(RequestLogger)
|
|
a.Use(sessionSaver)
|
|
|
|
return a
|
|
}
|