2020-04-21 12:00:34 +02:00
|
|
|
package http
|
2020-04-07 15:06:08 +02:00
|
|
|
|
2020-04-21 22:43:22 +02:00
|
|
|
import (
|
2022-11-01 15:50:32 +01:00
|
|
|
"fmt"
|
2023-01-23 12:30:34 +01:00
|
|
|
"net/http"
|
2022-11-01 15:50:32 +01:00
|
|
|
|
2021-08-24 17:48:37 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/account"
|
|
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/cors"
|
|
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
|
2023-01-23 12:30:34 +01:00
|
|
|
ohttp "github.com/owncloud/ocis/v2/ocis-pkg/service/http"
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/version"
|
|
|
|
|
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
|
2021-10-12 17:29:33 +02:00
|
|
|
"go-micro.dev/v4"
|
2020-04-21 22:43:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Server initializes the http service and server.
|
2023-01-23 12:30:34 +01:00
|
|
|
func Server(opts ...Option) (ohttp.Service, error) {
|
2020-04-21 22:43:22 +02:00
|
|
|
options := newOptions(opts...)
|
|
|
|
|
|
2023-01-23 12:30:34 +01:00
|
|
|
service, err := ohttp.NewService(
|
|
|
|
|
ohttp.TLSConfig(options.Config.HTTP.TLS),
|
|
|
|
|
ohttp.Logger(options.Logger),
|
|
|
|
|
ohttp.Name(options.Name),
|
|
|
|
|
ohttp.Version(version.GetString()),
|
|
|
|
|
ohttp.Address(options.Config.HTTP.Addr),
|
|
|
|
|
ohttp.Namespace(options.Config.HTTP.Namespace),
|
|
|
|
|
ohttp.Context(options.Context),
|
|
|
|
|
ohttp.Flags(options.Flags...),
|
2020-04-21 22:43:22 +02:00
|
|
|
)
|
2022-11-01 15:50:32 +01:00
|
|
|
if err != nil {
|
|
|
|
|
options.Logger.Error().
|
|
|
|
|
Err(err).
|
|
|
|
|
Msg("Error initializing http service")
|
2023-01-23 12:30:34 +01:00
|
|
|
return ohttp.Service{}, fmt.Errorf("could not initialize http service: %w", err)
|
2022-11-01 15:50:32 +01:00
|
|
|
}
|
2020-04-21 22:43:22 +02:00
|
|
|
|
2023-05-09 14:30:35 +02:00
|
|
|
handle := options.ServiceHandler
|
2020-04-21 22:43:22 +02:00
|
|
|
|
|
|
|
|
mux := chi.NewMux()
|
|
|
|
|
|
2025-07-29 15:40:41 +02:00
|
|
|
mux.Use(middleware.GetOtelhttpMiddleware(options.Name, options.TraceProvider))
|
2021-08-12 17:14:41 +02:00
|
|
|
mux.Use(chimiddleware.RealIP)
|
|
|
|
|
mux.Use(chimiddleware.RequestID)
|
2020-11-16 22:13:26 +01:00
|
|
|
mux.Use(middleware.NoCache)
|
2021-10-21 15:46:48 +02:00
|
|
|
mux.Use(middleware.Cors(
|
|
|
|
|
cors.Logger(options.Logger),
|
|
|
|
|
cors.AllowedOrigins(options.Config.HTTP.CORS.AllowedOrigins),
|
|
|
|
|
cors.AllowedMethods(options.Config.HTTP.CORS.AllowedMethods),
|
|
|
|
|
cors.AllowedHeaders(options.Config.HTTP.CORS.AllowedHeaders),
|
|
|
|
|
cors.AllowCredentials(options.Config.HTTP.CORS.AllowCredentials),
|
|
|
|
|
))
|
2020-05-27 15:37:47 +02:00
|
|
|
mux.Use(middleware.ExtractAccountUUID(
|
|
|
|
|
account.Logger(options.Logger),
|
|
|
|
|
account.JWTSecret(options.Config.TokenManager.JWTSecret)),
|
|
|
|
|
)
|
2020-04-21 22:43:22 +02:00
|
|
|
|
|
|
|
|
mux.Use(middleware.Version(
|
2020-07-02 08:13:24 +02:00
|
|
|
options.Name,
|
2022-06-13 10:41:14 +02:00
|
|
|
version.GetString(),
|
2020-04-21 22:43:22 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
mux.Use(middleware.Logger(
|
|
|
|
|
options.Logger,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
mux.Route(options.Config.HTTP.Root, func(r chi.Router) {
|
2022-01-12 15:10:15 +01:00
|
|
|
settingssvc.RegisterBundleServiceWeb(r, handle)
|
|
|
|
|
settingssvc.RegisterValueServiceWeb(r, handle)
|
|
|
|
|
settingssvc.RegisterRoleServiceWeb(r, handle)
|
|
|
|
|
settingssvc.RegisterPermissionServiceWeb(r, handle)
|
2020-04-21 22:43:22 +02:00
|
|
|
})
|
|
|
|
|
|
2023-01-23 12:30:34 +01:00
|
|
|
_ = chi.Walk(mux, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
|
|
|
|
options.Logger.Debug().Str("method", method).Str("route", route).Int("middlewares", len(middlewares)).Msg("serving endpoint")
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
2021-02-17 11:45:22 +01:00
|
|
|
micro.RegisterHandler(service.Server(), mux)
|
2020-04-21 22:43:22 +02:00
|
|
|
|
2022-11-01 15:50:32 +01:00
|
|
|
return service, nil
|
2020-04-21 22:43:22 +02:00
|
|
|
}
|