2019-12-06 15:03:59 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
2022-11-01 15:50:32 +01:00
|
|
|
"fmt"
|
2024-03-05 14:11:18 +01:00
|
|
|
"path"
|
2022-11-01 15:50:32 +01:00
|
|
|
|
2021-08-24 17:48:37 +02:00
|
|
|
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
2025-03-04 16:02:36 +01:00
|
|
|
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
|
2024-03-05 14:11:18 +01:00
|
|
|
"go-micro.dev/v4"
|
|
|
|
|
|
2023-05-19 16:10:36 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/cors"
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
|
2023-06-08 12:41:04 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/service/http"
|
|
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/version"
|
2024-03-05 14:11:18 +01:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/x/io/fsx"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/web"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/web/pkg/apps"
|
2022-06-20 16:49:06 +02:00
|
|
|
svc "github.com/owncloud/ocis/v2/services/web/pkg/service/v0"
|
2024-03-05 14:11:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// _customAppsEndpoint path is used to make app artifacts available by the web service.
|
|
|
|
|
_customAppsEndpoint = "/assets/apps"
|
2019-12-06 15:03:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Server initializes the http service and server.
|
|
|
|
|
func Server(opts ...Option) (http.Service, error) {
|
|
|
|
|
options := newOptions(opts...)
|
|
|
|
|
|
2022-11-01 15:50:32 +01:00
|
|
|
service, err := http.NewService(
|
|
|
|
|
http.TLSConfig(options.Config.HTTP.TLS),
|
2019-12-06 15:03:59 +01:00
|
|
|
http.Logger(options.Logger),
|
2019-12-18 12:51:09 +01:00
|
|
|
http.Namespace(options.Namespace),
|
2025-08-05 10:00:24 +02:00
|
|
|
http.Name(options.Config.Service.Name),
|
2022-06-13 10:41:14 +02:00
|
|
|
http.Version(version.GetString()),
|
2019-12-06 15:03:59 +01:00
|
|
|
http.Address(options.Config.HTTP.Addr),
|
|
|
|
|
http.Context(options.Context),
|
2019-12-12 11:09:45 +01:00
|
|
|
http.Flags(options.Flags...),
|
2023-08-03 10:38:13 +02:00
|
|
|
http.TraceProvider(options.TraceProvider),
|
2019-12-06 15:03:59 +01:00
|
|
|
)
|
2022-11-01 15:50:32 +01:00
|
|
|
if err != nil {
|
|
|
|
|
options.Logger.Error().
|
|
|
|
|
Err(err).
|
|
|
|
|
Msg("Error initializing http service")
|
|
|
|
|
return http.Service{}, fmt.Errorf("could not initialize http service: %w", err)
|
|
|
|
|
}
|
2019-12-06 15:03:59 +01:00
|
|
|
|
2023-08-23 15:17:17 +02:00
|
|
|
gatewaySelector, err := pool.GatewaySelector(
|
|
|
|
|
options.Config.GatewayAddress,
|
|
|
|
|
pool.WithRegistry(registry.GetRegistry()),
|
|
|
|
|
pool.WithTracerProvider(options.TraceProvider),
|
|
|
|
|
)
|
2023-02-09 16:18:44 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return http.Service{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 14:11:18 +01:00
|
|
|
appsFS := fsx.NewFallbackFS(
|
|
|
|
|
fsx.NewReadOnlyFs(fsx.NewBasePathFs(fsx.NewOsFs(), options.Config.Asset.AppsPath)),
|
|
|
|
|
fsx.NewBasePathFs(fsx.FromIOFS(web.Assets), "assets/apps"),
|
|
|
|
|
)
|
|
|
|
|
// build and inject the list of applications into the config
|
|
|
|
|
for _, application := range apps.List(options.Logger, options.Config.Apps, appsFS.Secondary().IOFS(), appsFS.Primary().IOFS()) {
|
|
|
|
|
options.Config.Web.Config.ExternalApps = append(
|
|
|
|
|
options.Config.Web.Config.ExternalApps,
|
|
|
|
|
application.ToExternal(path.Join(options.Config.HTTP.Root, _customAppsEndpoint)),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 15:48:49 +02:00
|
|
|
coreFS := fsx.NewFallbackFS(
|
|
|
|
|
fsx.NewBasePathFs(fsx.NewOsFs(), options.Config.Asset.CorePath),
|
|
|
|
|
fsx.NewBasePathFs(fsx.FromIOFS(web.Assets), "assets/core"),
|
|
|
|
|
)
|
|
|
|
|
themeFS := fsx.NewFallbackFS(
|
|
|
|
|
fsx.NewBasePathFs(fsx.NewOsFs(), options.Config.Asset.ThemesPath),
|
|
|
|
|
fsx.NewBasePathFs(fsx.FromIOFS(web.Assets), "assets/themes"),
|
|
|
|
|
)
|
|
|
|
|
// oCis is Apache licensed, and the ownCloud branding is AGPLv3.
|
|
|
|
|
// we are not allowed to have the ownCloud branding as part of the oCIS repository,
|
|
|
|
|
// as workaround we layer the embedded core fs on top of the theme fs to provide the ownCloud branding.
|
|
|
|
|
// each asset that is part of the embedded core fs (coreFS secondary fs)
|
|
|
|
|
// is downloaded at build time from the ownCloud web repository,
|
|
|
|
|
// web is licensed under AGPLv3 too, and is allowed to contain the ownCloud branding.
|
|
|
|
|
// themeFS = themeFS.Primary (rw) < themeFS.Secondary (ro) < coreFS.Secondary (ro)
|
|
|
|
|
themeFS = fsx.NewFallbackFS(
|
|
|
|
|
themeFS,
|
|
|
|
|
fsx.NewBasePathFs(coreFS.Secondary(), "themes"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
handle, err := svc.NewService(
|
2019-12-06 15:03:59 +01:00
|
|
|
svc.Logger(options.Logger),
|
2024-05-29 15:48:49 +02:00
|
|
|
svc.CoreFS(coreFS.IOFS()),
|
2024-03-05 14:11:18 +01:00
|
|
|
svc.AppFS(appsFS.IOFS()),
|
2024-05-29 15:48:49 +02:00
|
|
|
svc.ThemeFS(themeFS),
|
2024-03-05 14:11:18 +01:00
|
|
|
svc.AppsHTTPEndpoint(_customAppsEndpoint),
|
2019-12-06 15:03:59 +01:00
|
|
|
svc.Config(options.Config),
|
2023-06-08 12:41:04 +02:00
|
|
|
svc.GatewaySelector(gatewaySelector),
|
2019-12-06 15:03:59 +01:00
|
|
|
svc.Middleware(
|
2025-08-05 10:00:24 +02:00
|
|
|
middleware.GetOtelhttpMiddleware(options.Config.Service.Name, options.TraceProvider),
|
2021-08-12 17:14:41 +02:00
|
|
|
chimiddleware.RealIP,
|
|
|
|
|
chimiddleware.RequestID,
|
2024-05-31 12:34:12 +02:00
|
|
|
chimiddleware.Compress(5),
|
2020-11-16 22:13:26 +01:00
|
|
|
middleware.NoCache,
|
2019-12-06 15:03:59 +01:00
|
|
|
middleware.Version(
|
2024-05-13 17:04:01 +02:00
|
|
|
options.Config.Service.Name,
|
2022-06-13 10:41:14 +02:00
|
|
|
version.GetString(),
|
2019-12-06 15:03:59 +01:00
|
|
|
),
|
|
|
|
|
middleware.Logger(
|
|
|
|
|
options.Logger,
|
|
|
|
|
),
|
2023-05-19 16:10:36 +02:00
|
|
|
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),
|
|
|
|
|
),
|
2019-12-06 15:03:59 +01:00
|
|
|
),
|
2023-08-23 15:17:17 +02:00
|
|
|
svc.TraceProvider(options.TraceProvider),
|
2019-12-06 15:03:59 +01:00
|
|
|
)
|
|
|
|
|
|
2024-05-29 15:48:49 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return http.Service{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 15:03:59 +01:00
|
|
|
{
|
|
|
|
|
handle = svc.NewInstrument(handle, options.Metrics)
|
|
|
|
|
handle = svc.NewLogging(handle, options.Logger)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 14:45:19 +01:00
|
|
|
if err := micro.RegisterHandler(service.Server(), handle); err != nil {
|
|
|
|
|
return http.Service{}, err
|
|
|
|
|
}
|
2019-12-06 15:03:59 +01:00
|
|
|
|
|
|
|
|
return service, nil
|
|
|
|
|
}
|