2020-04-16 22:12:38 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2020-04-19 05:01:09 +05:30
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/etherlabsio/healthcheck"
|
2020-04-16 22:12:38 +05:30
|
|
|
"github.com/goava/di"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
. "github.com/elyby/chrly/http"
|
|
|
|
"github.com/elyby/chrly/mojangtextures"
|
2020-04-16 22:12:38 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var handlers = di.Options(
|
2020-04-19 05:01:09 +05:30
|
|
|
di.Provide(newHandlerFactory, di.As(new(http.Handler))),
|
2020-04-16 22:12:38 +05:30
|
|
|
di.Provide(newSkinsystemHandler, di.WithName("skinsystem")),
|
2020-04-19 05:01:09 +05:30
|
|
|
di.Provide(newApiHandler, di.WithName("api")),
|
|
|
|
di.Provide(newUUIDsWorkerHandler, di.WithName("worker")),
|
2020-04-16 22:12:38 +05:30
|
|
|
)
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func newHandlerFactory(
|
|
|
|
container *di.Container,
|
|
|
|
config *viper.Viper,
|
|
|
|
emitter Emitter,
|
|
|
|
) (*mux.Router, error) {
|
|
|
|
enabledModules := config.GetStringSlice("modules")
|
|
|
|
|
|
|
|
// gorilla.mux has no native way to combine multiple routers.
|
|
|
|
// The hack used later in the code works for prefixes in addresses, but leads to misbehavior
|
|
|
|
// if you set an empty prefix. Since the main application should be mounted at the root prefix,
|
|
|
|
// we use it as the base router
|
|
|
|
var router *mux.Router
|
|
|
|
if hasValue(enabledModules, "skinsystem") {
|
|
|
|
if err := container.Resolve(&router, di.Name("skinsystem")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
router = mux.NewRouter()
|
|
|
|
}
|
|
|
|
|
|
|
|
router.StrictSlash(true)
|
|
|
|
requestEventsMiddleware := CreateRequestEventsMiddleware(emitter, "skinsystem")
|
|
|
|
router.Use(requestEventsMiddleware)
|
|
|
|
// NotFoundHandler doesn't call for registered middlewares, so we must wrap it manually.
|
|
|
|
// See https://github.com/gorilla/mux/issues/416#issuecomment-600079279
|
|
|
|
router.NotFoundHandler = requestEventsMiddleware(http.HandlerFunc(NotFoundHandler))
|
|
|
|
|
|
|
|
// Enable the worker module before api to allow gorilla.mux to correctly find the target router
|
|
|
|
// as it uses the first matching and /api overrides the more accurate /api/worker
|
|
|
|
if hasValue(enabledModules, "worker") {
|
|
|
|
var workerRouter *mux.Router
|
|
|
|
if err := container.Resolve(&workerRouter, di.Name("worker")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mount(router, "/api/worker", workerRouter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasValue(enabledModules, "api") {
|
|
|
|
var apiRouter *mux.Router
|
|
|
|
if err := container.Resolve(&apiRouter, di.Name("api")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var authenticator Authenticator
|
|
|
|
if err := container.Resolve(&authenticator); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiRouter.Use(CreateAuthenticationMiddleware(authenticator))
|
|
|
|
|
|
|
|
mount(router, "/api", apiRouter)
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:16:12 +05:30
|
|
|
err := container.Invoke(enableReporters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
// Resolve health checkers last, because all the services required by the application
|
|
|
|
// must first be initialized and each of them can publish its own checkers
|
2020-04-20 15:53:02 +05:30
|
|
|
var healthCheckers []*namedHealthChecker
|
2020-04-19 05:01:09 +05:30
|
|
|
if container.Has(&healthCheckers) {
|
|
|
|
if err := container.Resolve(&healthCheckers); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
checkersOptions := make([]healthcheck.Option, len(healthCheckers))
|
|
|
|
for i, checker := range healthCheckers {
|
2020-04-20 15:53:02 +05:30
|
|
|
checkersOptions[i] = healthcheck.WithChecker(checker.Name, checker.Checker)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
router.Handle("/healthcheck", healthcheck.Handler(checkersOptions...)).Methods("GET")
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return router, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
func newSkinsystemHandler(
|
|
|
|
config *viper.Viper,
|
2020-04-19 05:01:09 +05:30
|
|
|
emitter Emitter,
|
|
|
|
skinsRepository SkinsRepository,
|
|
|
|
capesRepository CapesRepository,
|
|
|
|
mojangTexturesProvider MojangTexturesProvider,
|
2020-04-16 22:12:38 +05:30
|
|
|
) *mux.Router {
|
2020-04-20 18:10:20 +05:30
|
|
|
config.SetDefault("textures.extra_param_name", "chrly")
|
|
|
|
config.SetDefault("textures.extra_param_value", "how do you tame a horse in Minecraft?")
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
return (&Skinsystem{
|
2020-04-16 22:12:38 +05:30
|
|
|
Emitter: emitter,
|
|
|
|
SkinsRepo: skinsRepository,
|
|
|
|
CapesRepo: capesRepository,
|
|
|
|
MojangTexturesProvider: mojangTexturesProvider,
|
|
|
|
TexturesExtraParamName: config.GetString("textures.extra_param_name"),
|
|
|
|
TexturesExtraParamValue: config.GetString("textures.extra_param_value"),
|
2020-04-19 05:01:09 +05:30
|
|
|
}).Handler()
|
|
|
|
}
|
2020-04-16 22:12:38 +05:30
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func newApiHandler(emitter Emitter, skinsRepository SkinsRepository) *mux.Router {
|
|
|
|
return (&Api{
|
|
|
|
Emitter: emitter,
|
|
|
|
SkinsRepo: skinsRepository,
|
|
|
|
}).Handler()
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func newUUIDsWorkerHandler(mojangUUIDsProvider *mojangtextures.BatchUuidsProvider) *mux.Router {
|
|
|
|
return (&UUIDsWorker{
|
|
|
|
MojangUuidsProvider: mojangUUIDsProvider,
|
|
|
|
}).Handler()
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasValue(slice []string, needle string) bool {
|
|
|
|
for _, value := range slice {
|
|
|
|
if value == needle {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func mount(router *mux.Router, path string, handler http.Handler) {
|
|
|
|
router.PathPrefix(path).Handler(
|
|
|
|
http.StripPrefix(
|
|
|
|
strings.TrimSuffix(path, "/"),
|
|
|
|
handler,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type namedHealthChecker struct {
|
|
|
|
Name string
|
|
|
|
Checker healthcheck.Checker
|
|
|
|
}
|