Files
opencloud/services/userlog/pkg/server/http/option.go
T

113 lines
2.7 KiB
Go

package http
import (
"context"
"net/http"
"github.com/opencloud-eu/opencloud/pkg/log"
settingssvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/settings/v0"
"github.com/opencloud-eu/opencloud/services/userlog/pkg/config"
"github.com/opencloud-eu/opencloud/services/userlog/pkg/metrics"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel/trace"
)
// UserlogService is the http service interface the server needs
type UserlogService interface {
HandleGetEvents(w http.ResponseWriter, r *http.Request)
HandleDeleteEvents(w http.ResponseWriter, r *http.Request)
HandlePostGlobalEvent(w http.ResponseWriter, r *http.Request)
HandleDeleteGlobalEvent(w http.ResponseWriter, r *http.Request)
}
// 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 []pflag.Flag
Namespace string
RoleClient settingssvc.RoleService
UserlogService UserlogService
TracerProvider trace.TracerProvider
}
// 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(flags ...pflag.Flag) Option {
return func(o *Options) {
o.Flags = append(o.Flags, flags...)
}
}
// Namespace provides a function to set the Namespace option.
func Namespace(val string) Option {
return func(o *Options) {
o.Namespace = val
}
}
// Service provides a function to set the userlog http service
func Service(s UserlogService) Option {
return func(o *Options) {
o.UserlogService = s
}
}
// Roles provides a function to configure the roles service client
func Role(rs settingssvc.RoleService) Option {
return func(o *Options) {
o.RoleClient = rs
}
}
// TracerProvider provides a function to set the TracerProvider option
func TracerProvider(val trace.TracerProvider) Option {
return func(o *Options) {
o.TracerProvider = val
}
}