Files

136 lines
4.4 KiB
Go
Raw Permalink Normal View History

package http
import (
"fmt"
"path"
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"
"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"
"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"
"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"
)
var (
// _customAppsEndpoint path is used to make app artifacts available by the web service.
_customAppsEndpoint = "/assets/apps"
)
// Server initializes the http service and server.
func Server(opts ...Option) (http.Service, error) {
options := newOptions(opts...)
service, err := http.NewService(
http.TLSConfig(options.Config.HTTP.TLS),
http.Logger(options.Logger),
2019-12-18 12:51:09 +01:00
http.Namespace(options.Namespace),
http.Name(options.Config.Service.Name),
2022-06-13 10:41:14 +02:00
http.Version(version.GetString()),
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),
)
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)
}
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
}
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)),
)
}
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(
svc.Logger(options.Logger),
svc.CoreFS(coreFS.IOFS()),
svc.AppFS(appsFS.IOFS()),
svc.ThemeFS(themeFS),
svc.AppsHTTPEndpoint(_customAppsEndpoint),
svc.Config(options.Config),
svc.GatewaySelector(gatewaySelector),
svc.Middleware(
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,
middleware.Version(
options.Config.Service.Name,
2022-06-13 10:41:14 +02:00
version.GetString(),
),
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),
),
),
svc.TraceProvider(options.TraceProvider),
)
if err != nil {
return http.Service{}, err
}
{
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
}
return service, nil
}