Move service handler init to the server command

We kept two instances of the service handler in parallel
This commit is contained in:
Benedikt Kulmann
2020-09-11 12:47:43 +02:00
parent 177d41211b
commit c661312f5d
7 changed files with 43 additions and 76 deletions
+7
View File
@@ -43,6 +43,11 @@ func Server(cfg *config.Config) *cli.Command {
defer cancel()
handler, err := svc.New(svc.Logger(logger), svc.Config(cfg))
if err != nil {
logger.Fatal().Err(err).Msg("could not initialize service handler")
}
{
server := http.Server(
http.Logger(logger),
@@ -52,6 +57,7 @@ func Server(cfg *config.Config) *cli.Command {
http.Metrics(mtrcs),
http.Flags(flagset.RootWithConfig(cfg)),
http.Flags(flagset.ServerWithConfig(cfg)),
http.Handler(handler),
)
gr.Add(server.Run, func(_ error) {
@@ -70,6 +76,7 @@ func Server(cfg *config.Config) *cli.Command {
grpc.Context(ctx),
grpc.Config(cfg),
grpc.Metrics(mtrcs),
grpc.Handler(handler),
)
gr.Add(func() error {
+9
View File
@@ -6,6 +6,7 @@ import (
"github.com/micro/cli/v2"
"github.com/owncloud/ocis-accounts/pkg/config"
"github.com/owncloud/ocis-accounts/pkg/metrics"
svc "github.com/owncloud/ocis-accounts/pkg/service/v0"
"github.com/owncloud/ocis-pkg/v2/log"
)
@@ -20,6 +21,7 @@ type Options struct {
Config *config.Config
Metrics *metrics.Metrics
Flags []cli.Flag
Handler *svc.Service
}
// newOptions initializes the available default options.
@@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option {
o.Flags = append(o.Flags, val...)
}
}
// Handler provides a function to set the handler option.
func Handler(val *svc.Service) Option {
return func(o *Options) {
o.Handler = val
}
}
+3 -29
View File
@@ -1,19 +1,14 @@
package grpc
import (
"time"
mclient "github.com/micro/go-micro/v2/client"
"github.com/owncloud/ocis-accounts/pkg/proto/v0"
svc "github.com/owncloud/ocis-accounts/pkg/service/v0"
"github.com/owncloud/ocis-pkg/v2/roles"
"github.com/owncloud/ocis-pkg/v2/service/grpc"
settings "github.com/owncloud/ocis-settings/pkg/proto/v0"
)
// Server initializes a new go-micro service ready to run
func Server(opts ...Option) grpc.Service {
options := newOptions(opts...)
handler := options.Handler
service := grpc.NewService(
grpc.Name(options.Config.Server.Name),
@@ -24,31 +19,10 @@ func Server(opts ...Option) grpc.Service {
grpc.Flags(options.Flags...),
)
var hdlr *svc.Service
var err error
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-proxy/issues/38
rs := settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient)
roleManager := roles.NewManager(
roles.CacheSize(1024),
roles.CacheTTL(time.Hour*24*7),
roles.Logger(options.Logger),
roles.RoleService(rs),
)
if hdlr, err = svc.New(
svc.Logger(options.Logger),
svc.Config(options.Config),
svc.RoleManager(&roleManager),
svc.RoleService(rs),
); err != nil {
options.Logger.Fatal().Err(err).Msg("could not initialize service handler")
}
if err = proto.RegisterAccountsServiceHandler(service.Server(), hdlr); err != nil {
if err := proto.RegisterAccountsServiceHandler(service.Server(), handler); err != nil {
options.Logger.Fatal().Err(err).Msg("could not register service handler")
}
if err = proto.RegisterGroupsServiceHandler(service.Server(), hdlr); err != nil {
if err := proto.RegisterGroupsServiceHandler(service.Server(), handler); err != nil {
options.Logger.Fatal().Err(err).Msg("could not register groups handler")
}
+9
View File
@@ -6,6 +6,7 @@ import (
"github.com/micro/cli/v2"
"github.com/owncloud/ocis-accounts/pkg/config"
"github.com/owncloud/ocis-accounts/pkg/metrics"
svc "github.com/owncloud/ocis-accounts/pkg/service/v0"
"github.com/owncloud/ocis-pkg/v2/log"
)
@@ -20,6 +21,7 @@ type Options struct {
Config *config.Config
Metrics *metrics.Metrics
Flags []cli.Flag
Handler *svc.Service
}
// newOptions initializes the available default options.
@@ -74,3 +76,10 @@ func Flags(val []cli.Flag) Option {
o.Flags = append(o.Flags, val...)
}
}
// Handler provides a function to set the handler option.
func Handler(val *svc.Service) Option {
return func(o *Options) {
o.Handler = val
}
}
+1 -25
View File
@@ -1,24 +1,19 @@
package http
import (
"time"
"github.com/go-chi/chi"
mclient "github.com/micro/go-micro/v2/client"
"github.com/owncloud/ocis-accounts/pkg/assets"
"github.com/owncloud/ocis-accounts/pkg/proto/v0"
svc "github.com/owncloud/ocis-accounts/pkg/service/v0"
"github.com/owncloud/ocis-accounts/pkg/version"
"github.com/owncloud/ocis-pkg/v2/account"
"github.com/owncloud/ocis-pkg/v2/middleware"
"github.com/owncloud/ocis-pkg/v2/roles"
"github.com/owncloud/ocis-pkg/v2/service/http"
settings "github.com/owncloud/ocis-settings/pkg/proto/v0"
)
// Server initializes the http service and server.
func Server(opts ...Option) http.Service {
options := newOptions(opts...)
handler := options.Handler
service := http.NewService(
http.Logger(options.Logger),
@@ -30,25 +25,6 @@ func Server(opts ...Option) http.Service {
http.Flags(options.Flags...),
)
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-proxy/issues/38
rs := settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient)
roleManager := roles.NewManager(
roles.CacheSize(1024),
roles.CacheTTL(time.Hour*24*7),
roles.Logger(options.Logger),
roles.RoleService(rs),
)
handler, err := svc.New(
svc.Logger(options.Logger),
svc.Config(options.Config),
svc.RoleManager(&roleManager),
svc.RoleService(rs),
)
if err != nil {
options.Logger.Fatal().Err(err).Msg("could not initialize service handler")
}
mux := chi.NewMux()
mux.Use(middleware.RealIP)
-18
View File
@@ -3,8 +3,6 @@ package service
import (
"github.com/owncloud/ocis-accounts/pkg/config"
"github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-pkg/v2/roles"
settings "github.com/owncloud/ocis-settings/pkg/proto/v0"
)
// Option defines a single option function.
@@ -14,8 +12,6 @@ type Option func(o *Options)
type Options struct {
Logger log.Logger
Config *config.Config
RoleService settings.RoleService
RoleManager *roles.Manager
}
func newOptions(opts ...Option) Options {
@@ -41,17 +37,3 @@ func Config(val *config.Config) Option {
o.Config = val
}
}
// RoleService provides a function to set the role service option.
func RoleService(val settings.RoleService) Option {
return func(o *Options) {
o.RoleService = val
}
}
// RoleManager provides a function to set the roles manager option.
func RoleManager(val *roles.Manager) Option {
return func(o *Options) {
o.RoleManager = val
}
}
+14 -4
View File
@@ -5,10 +5,12 @@ import (
"encoding/json"
"errors"
"fmt"
mclient "github.com/micro/go-micro/v2/client"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/analysis/analyzer/custom"
@@ -30,10 +32,18 @@ func New(opts ...Option) (s *Service, err error) {
options := newOptions(opts...)
logger := options.Logger
cfg := options.Config
roleService := options.RoleService
roleManager := options.RoleManager
// read all user and group records
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-proxy/issues/38
roleService := settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient)
roleManager := roles.NewManager(
roles.CacheSize(1024),
roles.CacheTTL(time.Hour*24*7),
roles.Logger(options.Logger),
roles.RoleService(roleService),
)
// read all user and group records
accountsDir := filepath.Join(cfg.Server.AccountsDataPath, "accounts")
{
// check if accounts exist
@@ -326,7 +336,7 @@ func New(opts ...Option) (s *Service, err error) {
log: logger,
Config: cfg,
RoleService: roleService,
RoleManager: roleManager,
RoleManager: &roleManager,
}
indexDir := filepath.Join(cfg.Server.AccountsDataPath, "index.bleve")