-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jkoberg <[email protected]>
- Loading branch information
Showing
12 changed files
with
557 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
|
||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" | ||
"github.com/cs3org/reva/v2/pkg/events" | ||
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/log" | ||
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0" | ||
"github.com/owncloud/ocis/v2/services/activitylog/pkg/config" | ||
"github.com/owncloud/ocis/v2/services/activitylog/pkg/metrics" | ||
"github.com/urfave/cli/v2" | ||
"go-micro.dev/v4/store" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// Option defines a single option function. | ||
type Option func(o *Options) | ||
|
||
// Options defines the available options for this package. | ||
type Options struct { | ||
Logger log.Logger | ||
Context context.Context | ||
Config *config.Config | ||
Metrics *metrics.Metrics | ||
Flags []cli.Flag | ||
Namespace string | ||
Store store.Store | ||
Stream events.Stream | ||
GatewaySelector pool.Selectable[gateway.GatewayAPIClient] | ||
TraceProvider trace.TracerProvider | ||
HistoryClient ehsvc.EventHistoryService | ||
RegisteredEvents []events.Unmarshaller | ||
} | ||
|
||
// newOptions initializes the available default options. | ||
func newOptions(opts ...Option) Options { | ||
opt := Options{} | ||
|
||
for _, o := range opts { | ||
o(&opt) | ||
} | ||
|
||
return opt | ||
} | ||
|
||
// Logger provides a function to set the logger option. | ||
func Logger(val log.Logger) Option { | ||
return func(o *Options) { | ||
o.Logger = val | ||
} | ||
} | ||
|
||
// Context provides a function to set the context option. | ||
func Context(val context.Context) Option { | ||
return func(o *Options) { | ||
o.Context = val | ||
} | ||
} | ||
|
||
// Config provides a function to set the config option. | ||
func Config(val *config.Config) Option { | ||
return func(o *Options) { | ||
o.Config = val | ||
} | ||
} | ||
|
||
// Metrics provides a function to set the metrics option. | ||
func Metrics(val *metrics.Metrics) Option { | ||
return func(o *Options) { | ||
o.Metrics = val | ||
} | ||
} | ||
|
||
// Flags provides a function to set the flags option. | ||
func Flags(val []cli.Flag) Option { | ||
return func(o *Options) { | ||
o.Flags = append(o.Flags, val...) | ||
} | ||
} | ||
|
||
// Namespace provides a function to set the Namespace option. | ||
func Namespace(val string) Option { | ||
return func(o *Options) { | ||
o.Namespace = val | ||
} | ||
} | ||
|
||
// Store provides a function to configure the store | ||
func Store(store store.Store) Option { | ||
return func(o *Options) { | ||
o.Store = store | ||
} | ||
} | ||
|
||
// Stream provides a function to configure the stream | ||
func Stream(stream events.Stream) Option { | ||
return func(o *Options) { | ||
o.Stream = stream | ||
} | ||
} | ||
|
||
// GatewaySelector provides a function to configure the gateway client selector | ||
func GatewaySelector(gatewaySelector pool.Selectable[gateway.GatewayAPIClient]) Option { | ||
return func(o *Options) { | ||
o.GatewaySelector = gatewaySelector | ||
} | ||
} | ||
|
||
// History provides a function to configure the event history client | ||
func History(h ehsvc.EventHistoryService) Option { | ||
return func(o *Options) { | ||
o.HistoryClient = h | ||
} | ||
} | ||
|
||
// RegisteredEvents provides a function to register events | ||
func RegisteredEvents(evs []events.Unmarshaller) Option { | ||
return func(o *Options) { | ||
o.RegisteredEvents = evs | ||
} | ||
} | ||
|
||
// TraceProvider provides a function to set the TracerProvider option | ||
func TraceProvider(val trace.TracerProvider) Option { | ||
return func(o *Options) { | ||
o.TraceProvider = val | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
|
||
stdhttp "net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
chimiddleware "github.com/go-chi/chi/v5/middleware" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/account" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/cors" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/middleware" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/service/http" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/tracing" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/version" | ||
svc "github.com/owncloud/ocis/v2/services/activitylog/pkg/service" | ||
"github.com/riandyrn/otelchi" | ||
"go-micro.dev/v4" | ||
) | ||
|
||
// Service is the service interface | ||
type Service interface{} | ||
|
||
// 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), | ||
http.Namespace(options.Config.HTTP.Namespace), | ||
http.Name(options.Config.Service.Name), | ||
http.Version(version.GetString()), | ||
http.Address(options.Config.HTTP.Addr), | ||
http.Context(options.Context), | ||
http.Flags(options.Flags...), | ||
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) | ||
} | ||
|
||
middlewares := []func(stdhttp.Handler) stdhttp.Handler{ | ||
chimiddleware.RequestID, | ||
middleware.Version( | ||
options.Config.Service.Name, | ||
version.GetString(), | ||
), | ||
middleware.Logger( | ||
options.Logger, | ||
), | ||
middleware.ExtractAccountUUID( | ||
account.Logger(options.Logger), | ||
account.JWTSecret(options.Config.TokenManager.JWTSecret), | ||
), | ||
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), | ||
), | ||
} | ||
|
||
mux := chi.NewMux() | ||
mux.Use(middlewares...) | ||
|
||
mux.Use( | ||
otelchi.Middleware( | ||
"actitivylog", | ||
otelchi.WithChiRoutes(mux), | ||
otelchi.WithTracerProvider(options.TraceProvider), | ||
otelchi.WithPropagators(tracing.GetPropagator()), | ||
), | ||
) | ||
|
||
handle, err := svc.New( | ||
svc.Logger(options.Logger), | ||
svc.Stream(options.Stream), | ||
svc.Mux(mux), | ||
svc.Store(options.Store), | ||
svc.Config(options.Config), | ||
svc.GatewaySelector(options.GatewaySelector), | ||
svc.TraceProvider(options.TraceProvider), | ||
svc.HistoryClient(options.HistoryClient), | ||
svc.RegisteredEvents(options.RegisteredEvents), | ||
) | ||
if err != nil { | ||
return http.Service{}, err | ||
} | ||
|
||
if err := micro.RegisterHandler(service.Server(), handle); err != nil { | ||
return http.Service{}, err | ||
} | ||
|
||
return service, nil | ||
} |
Oops, something went wrong.