All checks were successful
continuous-integration/drone/push Build is passing
All the things fixed where reported by golangci-lint.
91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package web
|
|
|
|
import (
|
|
"git.quimbo.fr/odwrtw/canape/backend/auth"
|
|
"git.quimbo.fr/odwrtw/canape/backend/config"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/jmoiron/sqlx"
|
|
polochon "github.com/odwrtw/polochon/lib"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/unrolled/render"
|
|
"github.com/urfave/negroni"
|
|
)
|
|
|
|
// Env describes an environement object passed to all handlers
|
|
type Env struct {
|
|
Backend DetailerTorrenter
|
|
Database *sqlx.DB
|
|
Log *logrus.Entry
|
|
Router *mux.Router
|
|
Render *render.Render
|
|
Auth *auth.Authorizer
|
|
Config *config.Config
|
|
}
|
|
|
|
// EnvParams represents parameters for NewEnv
|
|
type EnvParams struct {
|
|
Database *sqlx.DB
|
|
Auth *auth.Authorizer
|
|
Log *logrus.Entry
|
|
Config *config.Config
|
|
Backend DetailerTorrenter
|
|
}
|
|
|
|
// DetailerTorrenter represents an object implementing polochon.Detailer and
|
|
// polochon.Torrenter
|
|
type DetailerTorrenter struct {
|
|
Detailer polochon.Detailer
|
|
Torrenter polochon.Torrenter
|
|
}
|
|
|
|
// NewEnv returns a new *Env
|
|
func NewEnv(p EnvParams) *Env {
|
|
return &Env{
|
|
Database: p.Database,
|
|
Log: p.Log,
|
|
Router: mux.NewRouter(),
|
|
Auth: p.Auth,
|
|
Config: p.Config,
|
|
Render: render.New(),
|
|
Backend: p.Backend,
|
|
}
|
|
}
|
|
|
|
// Route represents a route
|
|
type Route struct {
|
|
env *Env
|
|
mRoute *mux.Route
|
|
}
|
|
|
|
// Name returns the name of a route
|
|
func (r *Route) Name(name string) *Route {
|
|
r.mRoute.Name(name)
|
|
return r
|
|
}
|
|
|
|
// Methods returns a list of methods for a route
|
|
func (r *Route) Methods(methods ...string) *Route {
|
|
r.mRoute.Methods(methods...)
|
|
return r
|
|
}
|
|
|
|
// WithRole sets a given role for a route
|
|
func (r *Route) WithRole(role string) *Route {
|
|
handler := r.mRoute.GetHandler()
|
|
newHandler := negroni.New(
|
|
auth.NewMiddlewareRole(r.env.Auth, r.env.Log, role),
|
|
negroni.Wrap(handler))
|
|
r.mRoute.Handler(newHandler)
|
|
return r
|
|
}
|
|
|
|
// Handle add route
|
|
func (e *Env) Handle(route string, H HandlerFunc) *Route {
|
|
mRoute := e.Router.Handle(route, Handler{e, H})
|
|
return &Route{
|
|
env: e,
|
|
mRoute: mRoute,
|
|
}
|
|
}
|