2020-04-16 22:12:38 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2020-04-19 05:01:09 +05:30
|
|
|
"net/http"
|
2024-02-07 22:04:57 +05:30
|
|
|
"slices"
|
2020-04-19 05:01:09 +05:30
|
|
|
"strings"
|
|
|
|
|
2023-12-13 21:59:12 +05:30
|
|
|
"github.com/defval/di"
|
|
|
|
"github.com/etherlabsio/healthcheck/v2"
|
2020-04-16 22:12:38 +05:30
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/viper"
|
2024-02-14 05:26:48 +05:30
|
|
|
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
|
2020-04-16 22:12:38 +05:30
|
|
|
|
2024-02-01 12:42:34 +05:30
|
|
|
. "ely.by/chrly/internal/http"
|
2024-03-05 17:37:54 +05:30
|
|
|
"ely.by/chrly/internal/security"
|
2020-04-16 22:12:38 +05:30
|
|
|
)
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
const ModuleSkinsystem = "skinsystem"
|
|
|
|
const ModuleProfiles = "profiles"
|
|
|
|
const ModuleSigner = "signer"
|
|
|
|
|
2024-02-07 22:04:57 +05:30
|
|
|
var handlersDiOptions = di.Options(
|
2020-04-19 05:01:09 +05:30
|
|
|
di.Provide(newHandlerFactory, di.As(new(http.Handler))),
|
2024-03-05 17:37:54 +05:30
|
|
|
di.Provide(newSkinsystemHandler, di.WithName(ModuleSkinsystem)),
|
|
|
|
di.Provide(newProfilesApiHandler, di.WithName(ModuleProfiles)),
|
|
|
|
di.Provide(newSignerApiHandler, di.WithName(ModuleSigner)),
|
2020-04-16 22:12:38 +05:30
|
|
|
)
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func newHandlerFactory(
|
|
|
|
container *di.Container,
|
|
|
|
config *viper.Viper,
|
|
|
|
) (*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
|
2024-03-05 17:37:54 +05:30
|
|
|
if slices.Contains(enabledModules, ModuleSkinsystem) {
|
|
|
|
if err := container.Resolve(&router, di.Name(ModuleSkinsystem)); err != nil {
|
2020-04-19 05:01:09 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
router = mux.NewRouter()
|
|
|
|
}
|
|
|
|
|
|
|
|
router.StrictSlash(true)
|
2024-02-14 05:26:48 +05:30
|
|
|
router.Use(otelmux.Middleware("chrly"))
|
2024-02-07 23:03:06 +05:30
|
|
|
router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
if slices.Contains(enabledModules, ModuleProfiles) {
|
|
|
|
var profilesApiRouter *mux.Router
|
|
|
|
if err := container.Resolve(&profilesApiRouter, di.Name(ModuleProfiles)); err != nil {
|
2020-04-19 05:01:09 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var authenticator Authenticator
|
|
|
|
if err := container.Resolve(&authenticator); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
profilesApiRouter.Use(NewAuthenticationMiddleware(authenticator, security.ProfilesScope))
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
mount(router, "/api/profiles", profilesApiRouter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if slices.Contains(enabledModules, ModuleSigner) {
|
|
|
|
var signerApiRouter *mux.Router
|
|
|
|
if err := container.Resolve(&signerApiRouter, di.Name(ModuleSigner)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var authenticator Authenticator
|
|
|
|
if err := container.Resolve(&authenticator); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
authMiddleware := NewAuthenticationMiddleware(authenticator, security.SignScope)
|
|
|
|
conditionalAuth := NewConditionalMiddleware(func(req *http.Request) bool {
|
|
|
|
return req.Method != "GET"
|
|
|
|
}, authMiddleware)
|
|
|
|
signerApiRouter.Use(conditionalAuth)
|
|
|
|
|
|
|
|
mount(router, "/api/signer", signerApiRouter)
|
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
|
2023-12-13 21:59:12 +05:30
|
|
|
if has, _ := container.Has(&healthCheckers); has {
|
2020-04-19 05:01:09 +05:30
|
|
|
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,
|
2024-01-30 13:35:04 +05:30
|
|
|
profilesProvider ProfilesProvider,
|
2024-03-05 17:37:54 +05:30
|
|
|
texturesSigner SignerService,
|
2024-03-13 05:59:26 +05:30
|
|
|
) (*mux.Router, error) {
|
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?")
|
|
|
|
|
2024-03-13 05:59:26 +05:30
|
|
|
skinsystem, err := NewSkinsystemApi(
|
|
|
|
profilesProvider,
|
|
|
|
texturesSigner,
|
|
|
|
config.GetString("textures.extra_param_name"),
|
|
|
|
config.GetString("textures.extra_param_value"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return skinsystem.Handler(), nil
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
2020-04-16 22:12:38 +05:30
|
|
|
|
2024-03-13 05:59:26 +05:30
|
|
|
func newProfilesApiHandler(profilesManager ProfilesManager) (*mux.Router, error) {
|
|
|
|
profilesApi, err := NewProfilesApi(profilesManager)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return profilesApi.Handler(), nil
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2024-03-13 05:59:26 +05:30
|
|
|
func newSignerApiHandler(signer Signer) (*mux.Router, error) {
|
|
|
|
signerApi, err := NewSignerApi(signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return signerApi.Handler(), nil
|
2024-03-05 17:37:54 +05:30
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
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
|
|
|
|
}
|