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 (
|
2021-08-24 17:48:37 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
2022-04-13 10:35:04 +02:00
|
|
|
"github.com/owncloud/ocis/extensions/settings/pkg/assets"
|
|
|
|
|
svc "github.com/owncloud/ocis/extensions/settings/pkg/service/v0"
|
2020-09-18 14:51:07 +02:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/account"
|
2021-10-21 15:46:48 +02:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/cors"
|
2020-09-18 14:51:07 +02:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/middleware"
|
|
|
|
|
"github.com/owncloud/ocis/ocis-pkg/service/http"
|
2021-10-05 12:39:31 +02:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/version"
|
2022-01-31 12:17:56 +01:00
|
|
|
settingssvc "github.com/owncloud/ocis/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.
|
|
|
|
|
func Server(opts ...Option) http.Service {
|
|
|
|
|
options := newOptions(opts...)
|
|
|
|
|
|
|
|
|
|
service := http.NewService(
|
|
|
|
|
http.Logger(options.Logger),
|
|
|
|
|
http.Name(options.Name),
|
2022-01-03 16:24:39 +01:00
|
|
|
http.Version(version.String),
|
2020-04-21 22:43:22 +02:00
|
|
|
http.Address(options.Config.HTTP.Addr),
|
|
|
|
|
http.Namespace(options.Config.HTTP.Namespace),
|
|
|
|
|
http.Context(options.Context),
|
|
|
|
|
http.Flags(options.Flags...),
|
|
|
|
|
)
|
|
|
|
|
|
2020-08-19 09:53:03 +02:00
|
|
|
handle := svc.NewService(options.Config, options.Logger)
|
2020-04-21 22:43:22 +02:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
handle = svc.NewInstrument(handle, options.Metrics)
|
|
|
|
|
handle = svc.NewLogging(handle, options.Logger)
|
|
|
|
|
handle = svc.NewTracing(handle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mux := chi.NewMux()
|
|
|
|
|
|
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-04-21 22:43:22 +02:00
|
|
|
mux.Use(middleware.Secure)
|
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,
|
2020-04-21 22:43:22 +02:00
|
|
|
version.String,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
mux.Use(middleware.Logger(
|
|
|
|
|
options.Logger,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
mux.Use(middleware.Static(
|
|
|
|
|
options.Config.HTTP.Root,
|
|
|
|
|
assets.New(
|
|
|
|
|
assets.Logger(options.Logger),
|
|
|
|
|
assets.Config(options.Config),
|
|
|
|
|
),
|
2020-11-16 23:22:56 +01:00
|
|
|
options.Config.HTTP.CacheTTL,
|
2020-04-21 22:43:22 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
2021-02-17 11:45:22 +01:00
|
|
|
micro.RegisterHandler(service.Server(), mux)
|
2020-04-21 22:43:22 +02:00
|
|
|
|
|
|
|
|
return service
|
|
|
|
|
}
|