From ea4dae7266b0d2c08ef5cbc5dac069eec834259a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Ortu=C3=B1o?= Date: Mon, 11 Oct 2021 16:54:51 +0200 Subject: [PATCH] global config refactoring (#178) Signed-off-by: ortuman --- {pkg/cmd => cmd}/jackal/app.go | 163 ++++------------ cmd/jackal/config.go | 123 ++++++++++++ {pkg/cmd => cmd}/jackal/http_server.go | 0 {pkg/cmd => cmd}/jackal/listener.go | 0 {pkg/cmd => cmd}/jackal/main.go | 0 {pkg/cmd => cmd}/jackal/modules.go | 22 +-- .../jackalctl/ctlv1/command/error.go | 0 .../jackalctl/ctlv1/command/global.go | 0 .../jackalctl/ctlv1/command/printer.go | 0 .../jackalctl/ctlv1/command/user_command.go | 0 .../ctlv1/command/version_command.go | 0 {pkg/cmd => cmd}/jackalctl/ctlv1/ctl.go | 2 +- {pkg/cmd => cmd}/jackalctl/ctlv1/help.go | 0 {pkg/cmd => cmd}/jackalctl/main.go | 2 +- config/example.config.yaml | 3 +- pkg/admin/server/server.go | 19 +- pkg/admin/server/service.go | 2 +- pkg/auth/interface.go | 2 +- pkg/auth/pepper/keys.go | 18 +- pkg/auth/pepper/keys_test.go | 30 +-- pkg/auth/scram.go | 2 +- pkg/auth/scram_test.go | 5 +- pkg/c2s/interface.go | 2 +- pkg/c2s/router.go | 2 +- pkg/c2s/socket_listener.go | 2 +- pkg/c2s/socket_listener_test.go | 4 +- pkg/cluster/etcd/dial.go | 42 +++++ pkg/cluster/etcd/etcd.go | 27 +++ pkg/cluster/{kv => }/etcd/kv.go | 19 +- pkg/cluster/{locker => }/etcd/locker.go | 31 ++- pkg/cluster/server/server.go | 16 +- pkg/cluster/server/server_test.go | 6 +- pkg/cmd/jackal/config.go | 178 ------------------ pkg/component/xep0114/socket_listener_test.go | 4 +- pkg/host/hosts.go | 39 +++- pkg/host/hosts_test.go | 8 +- pkg/module/offline/interface.go | 2 +- pkg/module/offline/offline.go | 6 +- pkg/module/roster/interface.go | 3 +- pkg/module/roster/roster.go | 2 +- pkg/module/roster/roster_test.go | 2 +- pkg/module/xep0012/interface.go | 2 +- pkg/module/xep0012/last.go | 2 +- pkg/module/xep0030/account_provider.go | 2 +- pkg/module/xep0030/disco.go | 2 +- pkg/module/xep0030/interface.go | 2 +- pkg/module/xep0049/interface.go | 2 +- pkg/module/xep0049/private.go | 2 +- pkg/module/xep0054/interface.go | 2 +- pkg/module/xep0054/vcard.go | 2 +- pkg/module/xep0092/version.go | 4 +- pkg/module/xep0115/caps.go | 2 +- pkg/module/xep0115/interface.go | 2 +- pkg/module/xep0191/blocklist.go | 2 +- pkg/module/xep0191/blocklist_test.go | 2 +- pkg/module/xep0191/interface.go | 2 +- pkg/module/xep0198/stream.go | 12 +- pkg/module/xep0199/ping.go | 15 +- pkg/module/xep0199/ping_test.go | 10 +- pkg/s2s/socket_listener_test.go | 4 +- pkg/shaper/shaper.go | 45 ++++- pkg/shaper/shaper_test.go | 16 +- .../measured/blocklist.go | 2 +- .../measured/blocklist_test.go | 0 .../measured/capabilities.go | 2 +- .../measured/capabilities_test.go | 0 .../measured/interface.go | 2 +- pkg/{repository => storage}/measured/last.go | 2 +- .../measured/last_test.go | 0 .../measured/measured.go | 2 +- .../measured/metrics.go | 0 .../measured/offline.go | 2 +- .../measured/offline_test.go | 0 .../measured/private.go | 2 +- .../measured/private_test.go | 0 .../measured/roster.go | 2 +- .../measured/roster_test.go | 0 pkg/{repository => storage}/measured/user.go | 2 +- .../measured/user_test.go | 0 pkg/{repository => storage}/measured/vcard.go | 2 +- .../measured/vcard_test.go | 0 .../pgsql/blocklist.go | 0 .../pgsql/blocklist_test.go | 0 .../pgsql/capabilities.go | 0 .../pgsql/capabilities_test.go | 0 pkg/{repository => storage}/pgsql/last.go | 0 .../pgsql/last_test.go | 0 pkg/{repository => storage}/pgsql/offline.go | 0 .../pgsql/offline_test.go | 0 pkg/{repository => storage}/pgsql/pgsql.go | 0 .../pgsql/pgsql_test.go | 0 pkg/{repository => storage}/pgsql/private.go | 0 .../pgsql/private_test.go | 0 .../pgsql/repository.go | 23 ++- pkg/{repository => storage}/pgsql/roster.go | 0 .../pgsql/roster_test.go | 0 pkg/{repository => storage}/pgsql/tx.go | 2 +- pkg/{repository => storage}/pgsql/user.go | 0 .../pgsql/user_test.go | 0 pkg/{repository => storage}/pgsql/vcard.go | 0 .../pgsql/vcard_test.go | 0 pkg/{ => storage}/repository/blocklist.go | 0 pkg/{ => storage}/repository/capabilities.go | 0 pkg/{ => storage}/repository/last.go | 0 pkg/{ => storage}/repository/offline.go | 0 pkg/{ => storage}/repository/private.go | 0 pkg/{ => storage}/repository/repository.go | 0 pkg/{ => storage}/repository/roster.go | 0 pkg/{ => storage}/repository/user.go | 0 pkg/{ => storage}/repository/vcard.go | 0 pkg/storage/storage.go | 41 ++++ scripts/compile.sh | 2 +- scripts/dockerimage.sh | 4 +- scripts/install.sh | 2 +- scripts/installctl.sh | 2 +- 115 files changed, 559 insertions(+), 459 deletions(-) rename {pkg/cmd => cmd}/jackal/app.go (73%) create mode 100644 cmd/jackal/config.go rename {pkg/cmd => cmd}/jackal/http_server.go (100%) rename {pkg/cmd => cmd}/jackal/listener.go (100%) rename {pkg/cmd => cmd}/jackal/main.go (100%) rename {pkg/cmd => cmd}/jackal/modules.go (85%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/command/error.go (100%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/command/global.go (100%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/command/printer.go (100%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/command/user_command.go (100%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/command/version_command.go (100%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/ctl.go (97%) rename {pkg/cmd => cmd}/jackalctl/ctlv1/help.go (100%) rename {pkg/cmd => cmd}/jackalctl/main.go (94%) create mode 100644 pkg/cluster/etcd/dial.go create mode 100644 pkg/cluster/etcd/etcd.go rename pkg/cluster/{kv => }/etcd/kv.go (94%) rename pkg/cluster/{locker => }/etcd/locker.go (67%) delete mode 100644 pkg/cmd/jackal/config.go rename pkg/{repository => storage}/measured/blocklist.go (97%) rename pkg/{repository => storage}/measured/blocklist_test.go (100%) rename pkg/{repository => storage}/measured/capabilities.go (96%) rename pkg/{repository => storage}/measured/capabilities_test.go (100%) rename pkg/{repository => storage}/measured/interface.go (92%) rename pkg/{repository => storage}/measured/last.go (96%) rename pkg/{repository => storage}/measured/last_test.go (100%) rename pkg/{repository => storage}/measured/measured.go (98%) rename pkg/{repository => storage}/measured/metrics.go (100%) rename pkg/{repository => storage}/measured/offline.go (97%) rename pkg/{repository => storage}/measured/offline_test.go (100%) rename pkg/{repository => storage}/measured/private.go (96%) rename pkg/{repository => storage}/measured/private_test.go (100%) rename pkg/{repository => storage}/measured/roster.go (98%) rename pkg/{repository => storage}/measured/roster_test.go (100%) rename pkg/{repository => storage}/measured/user.go (97%) rename pkg/{repository => storage}/measured/user_test.go (100%) rename pkg/{repository => storage}/measured/vcard.go (96%) rename pkg/{repository => storage}/measured/vcard_test.go (100%) rename pkg/{repository => storage}/pgsql/blocklist.go (100%) rename pkg/{repository => storage}/pgsql/blocklist_test.go (100%) rename pkg/{repository => storage}/pgsql/capabilities.go (100%) rename pkg/{repository => storage}/pgsql/capabilities_test.go (100%) rename pkg/{repository => storage}/pgsql/last.go (100%) rename pkg/{repository => storage}/pgsql/last_test.go (100%) rename pkg/{repository => storage}/pgsql/offline.go (100%) rename pkg/{repository => storage}/pgsql/offline_test.go (100%) rename pkg/{repository => storage}/pgsql/pgsql.go (100%) rename pkg/{repository => storage}/pgsql/pgsql_test.go (100%) rename pkg/{repository => storage}/pgsql/private.go (100%) rename pkg/{repository => storage}/pgsql/private_test.go (100%) rename pkg/{repository => storage}/pgsql/repository.go (81%) rename pkg/{repository => storage}/pgsql/roster.go (100%) rename pkg/{repository => storage}/pgsql/roster_test.go (100%) rename pkg/{repository => storage}/pgsql/tx.go (95%) rename pkg/{repository => storage}/pgsql/user.go (100%) rename pkg/{repository => storage}/pgsql/user_test.go (100%) rename pkg/{repository => storage}/pgsql/vcard.go (100%) rename pkg/{repository => storage}/pgsql/vcard_test.go (100%) rename pkg/{ => storage}/repository/blocklist.go (100%) rename pkg/{ => storage}/repository/capabilities.go (100%) rename pkg/{ => storage}/repository/last.go (100%) rename pkg/{ => storage}/repository/offline.go (100%) rename pkg/{ => storage}/repository/private.go (100%) rename pkg/{ => storage}/repository/repository.go (100%) rename pkg/{ => storage}/repository/roster.go (100%) rename pkg/{ => storage}/repository/user.go (100%) rename pkg/{ => storage}/repository/vcard.go (100%) create mode 100644 pkg/storage/storage.go diff --git a/pkg/cmd/jackal/app.go b/cmd/jackal/app.go similarity index 73% rename from pkg/cmd/jackal/app.go rename to cmd/jackal/app.go index 54782beb4..794ecd44b 100644 --- a/pkg/cmd/jackal/app.go +++ b/cmd/jackal/app.go @@ -26,20 +26,14 @@ import ( "syscall" "time" - "google.golang.org/grpc/keepalive" - - "google.golang.org/grpc" - - etcdv3 "github.com/coreos/etcd/clientv3" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" adminserver "github.com/ortuman/jackal/pkg/admin/server" "github.com/ortuman/jackal/pkg/auth/pepper" "github.com/ortuman/jackal/pkg/c2s" clusterconnmanager "github.com/ortuman/jackal/pkg/cluster/connmanager" + "github.com/ortuman/jackal/pkg/cluster/etcd" "github.com/ortuman/jackal/pkg/cluster/kv" - etcdkv "github.com/ortuman/jackal/pkg/cluster/kv/etcd" "github.com/ortuman/jackal/pkg/cluster/locker" - etcdlocker "github.com/ortuman/jackal/pkg/cluster/locker/etcd" "github.com/ortuman/jackal/pkg/cluster/memberlist" clusterrouter "github.com/ortuman/jackal/pkg/cluster/router" clusterserver "github.com/ortuman/jackal/pkg/cluster/server" @@ -50,15 +44,12 @@ import ( "github.com/ortuman/jackal/pkg/log" "github.com/ortuman/jackal/pkg/log/zap" "github.com/ortuman/jackal/pkg/module" - "github.com/ortuman/jackal/pkg/repository" - measuredrepository "github.com/ortuman/jackal/pkg/repository/measured" - pgsqlrepository "github.com/ortuman/jackal/pkg/repository/pgsql" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/s2s" "github.com/ortuman/jackal/pkg/shaper" + "github.com/ortuman/jackal/pkg/storage" + "github.com/ortuman/jackal/pkg/storage/repository" "github.com/ortuman/jackal/pkg/util/crashreporter" - "github.com/ortuman/jackal/pkg/util/stringmatcher" - tlsutil "github.com/ortuman/jackal/pkg/util/tls" "github.com/ortuman/jackal/pkg/version" ) @@ -108,9 +99,8 @@ type serverApp struct { peppers *pepper.Keys hk *hook.Hooks - etcdCli *etcdv3.Client - locker locker.Locker - kv kv.KV + locker locker.Locker + kv kv.KV rep repository.Repository memberList *memberlist.MemberList @@ -202,7 +192,7 @@ func run(output io.Writer, args []string) error { ) // init pepper keys - peppers, err := pepper.NewKeys(cfg.Peppers.Keys, cfg.Peppers.UseID) + peppers, err := pepper.NewKeys(cfg.Peppers) if err != nil { return err } @@ -212,11 +202,8 @@ func run(output io.Writer, args []string) error { a.hk = hook.NewHooks() // init etcd - if err := a.initEtcd(cfg.Cluster.Etcd); err != nil { - return err - } - a.initLocker() - a.initKVStore() + a.initLocker(cfg.Cluster.Etcd) + a.initKVStore(cfg.Cluster.Etcd) // init cluster connection manager a.initClusterConnManager() @@ -245,14 +232,13 @@ func run(output io.Writer, args []string) error { a.registerStartStopper(newHTTPServer(cfg.HTTPPort)) // init admin server - if !cfg.Admin.Disabled { - a.initAdminServer(cfg.Admin.BindAddr, cfg.Admin.Port) - } + a.initAdminServer(cfg.Admin) + // init cluster server - a.initClusterServer(cfg.Cluster.BindAddr, cfg.Cluster.Port) + a.initClusterServer(cfg.Cluster.Server) // init memberlist - a.initMemberList(cfg.Cluster.Port) + a.initMemberList(cfg.Cluster.Server.Port) // init C2S/S2S listeners if err := a.initListeners(cfg.Listeners); err != nil { @@ -262,53 +248,20 @@ func run(output io.Writer, args []string) error { if err := a.bootstrap(); err != nil { return err } - // ...wait for stop signal to shutdown + // ...wait for stop signal to shut down sig := a.waitForStopSignal() log.Infof("Received %s signal... shutting down...", sig.String()) return a.shutdown() } -func (a *serverApp) initEtcd(cfg etcdConfig) error { - const ( - dialKeepAliveTime = 30 * time.Second - dialKeepAliveTimeout = 10 * time.Second - dialTimeout = 20 * time.Second - - keepAlive = time.Second * 10 - timeout = time.Minute * 20 - ) - dialOptions := []grpc.DialOption{ - grpc.WithBlock(), - grpc.WithKeepaliveParams(keepalive.ClientParameters{ - Time: keepAlive, - Timeout: timeout, - PermitWithoutStream: true, - }), - grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), - grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor), - } - cli, err := etcdv3.New(etcdv3.Config{ - Endpoints: cfg.Endpoints, - DialTimeout: dialTimeout, - DialKeepAliveTime: dialKeepAliveTime, - DialKeepAliveTimeout: dialKeepAliveTimeout, - DialOptions: dialOptions, - }) - if err != nil { - return err - } - a.etcdCli = cli - return nil -} - -func (a *serverApp) initLocker() { - a.locker = etcdlocker.New(a.etcdCli) +func (a *serverApp) initLocker(cfg etcd.Config) { + a.locker = etcd.NewLocker(cfg) a.registerStartStopper(a.locker) } -func (a *serverApp) initKVStore() { - etcdKV := etcdkv.New(a.etcdCli) +func (a *serverApp) initKVStore(cfg etcd.Config) { + etcdKV := etcd.NewKV(cfg) a.kv = kv.NewMeasured(etcdKV) a.registerStartStopper(a.kv) } @@ -318,71 +271,33 @@ func (a *serverApp) initClusterConnManager() { a.registerStartStopper(a.clusterConnMng) } -func (a *serverApp) initRepository(sCfg storageConfig) error { - cfg := sCfg.PgSQL - opts := pgsqlrepository.Config{ - MaxIdleConns: cfg.MaxIdleConns, - MaxOpenConns: cfg.MaxOpenConns, - ConnMaxIdleTime: cfg.ConnMaxIdleTime, - ConnMaxLifetime: cfg.ConnMaxLifetime, +func (a *serverApp) initRepository(cfg storage.Config) error { + rep, err := storage.New(cfg) + if err != nil { + return err } - pgRep := pgsqlrepository.New( - cfg.Host, - cfg.User, - cfg.Password, - cfg.Database, - cfg.SSLMode, - opts, - ) - a.rep = measuredrepository.New(pgRep) + a.rep = rep a.registerStartStopper(a.rep) return nil } -func (a *serverApp) initHosts(configs []hostConfig) error { - const defaultDomain = "localhost" - h := host.New() - if len(configs) == 0 { - cer, err := tlsutil.LoadCertificate("", "", defaultDomain) - if err != nil { - return err - } - h.RegisterDefaultHost(defaultDomain, cer) - a.hosts = h - return nil - } - for i, config := range configs { - cer, err := tlsutil.LoadCertificate(config.TLS.PrivateKeyFile, config.TLS.CertFile, config.Domain) - if err != nil { - return err - } - if i == 0 { - h.RegisterDefaultHost(config.Domain, cer) - } else { - h.RegisterHost(config.Domain, cer) - } +func (a *serverApp) initHosts(configs []host.Config) error { + h, err := host.NewHost(configs) + if err != nil { + return err } a.hosts = h return nil } -func (a *serverApp) initShapers(cfgs []shaperConfig) error { +func (a *serverApp) initShapers(configs []shaper.Config) error { a.shapers = make(shaper.Shapers, 0) - for _, cfg := range cfgs { - var sm stringmatcher.Matcher - switch { - case len(cfg.Matching.JID.In) > 0: - sm = stringmatcher.NewStringMatcher(cfg.Matching.JID.In) - case len(cfg.Matching.JID.RegEx) > 0: - var err error - sm, err = stringmatcher.NewRegExMatcher(cfg.Matching.JID.RegEx) - if err != nil { - return err - } - default: - sm = stringmatcher.Any + for _, cfg := range configs { + shp, err := shaper.New(cfg) + if err != nil { + return err } - a.shapers = append(a.shapers, shaper.New(cfg.MaxSessions, cfg.Rate.Limit, cfg.Rate.Burst, sm)) + a.shapers = append(a.shapers, shp) log.Infow(fmt.Sprintf("Registered '%s' shaper configuration", cfg.Name), "name", cfg.Name, @@ -403,7 +318,7 @@ func (a *serverApp) initListeners(configs []listenerConfig) error { for _, cfg := range configs { lnFn, ok := lnFns[cfg.Type] if !ok { - return fmt.Errorf("main: unrecognized listener: %s", cfg.Type) + return fmt.Errorf("main: unrecognized listener type: %s", cfg.Type) } ln := lnFn(a, cfg) a.registerStartStopper(ln) @@ -472,18 +387,21 @@ func (a *serverApp) initModules(cfg modulesConfig) error { return nil } -func (a *serverApp) initAdminServer(bindAddr string, port int) { - adminSrv := adminserver.New(bindAddr, port, a.rep, a.peppers, a.hk) +func (a *serverApp) initAdminServer(cfg adminserver.Config) { + adminSrv := adminserver.New(cfg, a.rep, a.peppers, a.hk) a.registerStartStopper(adminSrv) } -func (a *serverApp) initClusterServer(bindAddr string, port int) { - clusterSrv := clusterserver.New(bindAddr, port, a.localRouter, a.comps) +func (a *serverApp) initClusterServer(cfg clusterserver.Config) { + clusterSrv := clusterserver.New(cfg, a.localRouter, a.comps) a.registerStartStopper(clusterSrv) return } func (a *serverApp) registerStartStopper(ss startStopper) { + if ss == nil { + return + } a.starters = append(a.starters, ss) a.stoppers = append([]stopper{ss}, a.stoppers...) } @@ -526,7 +444,6 @@ func (a *serverApp) shutdown() error { return } } - _ = a.etcdCli.Close() log.Close() errCh <- nil }() diff --git a/cmd/jackal/config.go b/cmd/jackal/config.go new file mode 100644 index 000000000..f1496fc4b --- /dev/null +++ b/cmd/jackal/config.go @@ -0,0 +1,123 @@ +// Copyright 2020 The jackal Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "path/filepath" + "time" + + "github.com/ortuman/jackal/pkg/module/offline" + + "github.com/ortuman/jackal/pkg/module/xep0092" + + "github.com/ortuman/jackal/pkg/module/xep0198" + + "github.com/ortuman/jackal/pkg/module/xep0199" + + "github.com/ortuman/jackal/pkg/shaper" + + "github.com/kkyr/fig" + adminserver "github.com/ortuman/jackal/pkg/admin/server" + "github.com/ortuman/jackal/pkg/auth/pepper" + "github.com/ortuman/jackal/pkg/cluster/etcd" + clusterserver "github.com/ortuman/jackal/pkg/cluster/server" + "github.com/ortuman/jackal/pkg/host" + "github.com/ortuman/jackal/pkg/storage" +) + +type listenerConfig struct { + Type string `fig:"type" default:"c2s"` + BindAddr string `fig:"bind_addr"` + Port int `fig:"port" default:"5222"` + Transport string `fig:"transport" default:"socket"` + DirectTLS bool `fig:"direct_tls"` + SASL struct { + Mechanisms []string `fig:"mechanisms" default:"[scram_sha_1, scram_sha_256, scram_sha_512, scram_sha3_512]"` + External struct { + Address string `fig:"address"` + IsSecure bool `fig:"is_secure"` + } `fig:"external"` + } `fig:"sasl"` + CompressionLevel string `fig:"compression_level" default:"default"` + ResourceConflict string `fig:"resource_conflict" default:"terminate_old"` + MaxStanzaSize int `fig:"max_stanza_size" default:"32768"` + Secret string `fig:"secret"` + ConnectTimeout time.Duration `fig:"conn_timeout" default:"3s"` + AuthenticateTimeout time.Duration `fig:"auth_timeout" default:"10s"` + KeepAliveTimeout time.Duration `fig:"keep_alive_timeout" default:"10m"` + RequestTimeout time.Duration `fig:"req_timeout" default:"15s"` +} + +type s2sOutConfig struct { + DialTimeout time.Duration `fig:"dial_timeout" default:"5s"` + DialbackSecret string `fig:"secret"` + ConnectTimeout time.Duration `fig:"conn_timeout" default:"3s"` + KeepAliveTimeout time.Duration `fig:"keep_alive_timeout" default:"120s"` + RequestTimeout time.Duration `fig:"req_timeout" default:"15s"` + MaxStanzaSize int `fig:"max_stanza_size" default:"131072"` +} + +type modulesConfig struct { + Enabled []string `fig:"enabled"` + + Offline offline.Config `fig:"offline"` + + // XEP-0092: Software Version + Version xep0092.Config `fig:"version"` + + // XEP-0198: Stream Management + Stream xep0198.Config `fig:"stream"` + + // XEP-0199: XMPP Ping + Ping xep0199.Config `fig:"ping"` +} + +type componentsConfig struct{} + +type serverConfig struct { + Logger struct { + Level string `fig:"level" default:"debug"` + OutputPath string `fig:"output_path"` + } `fig:"logger"` + + Cluster struct { + Etcd etcd.Config `fig:"etcd"` + Server clusterserver.Config `fig:"server"` + } `fig:"cluster"` + + HTTPPort int `fig:"http_port" default:"6060"` + + Peppers pepper.Config `fig:"peppers"` + Admin adminserver.Config `fig:"admin"` + Storage storage.Config `fig:"storage"` + Hosts []host.Config `fig:"hosts"` + Shapers []shaper.Config `fig:"shapers"` + Listeners []listenerConfig `fig:"listeners"` + S2SOut s2sOutConfig `fig:"s2s_out"` + Modules modulesConfig `fig:"modules"` + Components componentsConfig `fig:"components"` +} + +func loadConfig(configFile string) (*serverConfig, error) { + var cfg serverConfig + file := filepath.Base(configFile) + dir := filepath.Dir(configFile) + + err := fig.Load(&cfg, fig.File(file), fig.Dirs(dir)) + if err != nil { + return nil, err + } + return &cfg, nil +} diff --git a/pkg/cmd/jackal/http_server.go b/cmd/jackal/http_server.go similarity index 100% rename from pkg/cmd/jackal/http_server.go rename to cmd/jackal/http_server.go diff --git a/pkg/cmd/jackal/listener.go b/cmd/jackal/listener.go similarity index 100% rename from pkg/cmd/jackal/listener.go rename to cmd/jackal/listener.go diff --git a/pkg/cmd/jackal/main.go b/cmd/jackal/main.go similarity index 100% rename from pkg/cmd/jackal/main.go rename to cmd/jackal/main.go diff --git a/pkg/cmd/jackal/modules.go b/cmd/jackal/modules.go similarity index 85% rename from pkg/cmd/jackal/modules.go rename to cmd/jackal/modules.go index 04e1413b9..3e02fd46f 100644 --- a/pkg/cmd/jackal/modules.go +++ b/cmd/jackal/modules.go @@ -55,9 +55,7 @@ var modFns = map[string]func(a *serverApp, cfg modulesConfig) module.Module{ // Offline // (https://xmpp.org/extensions/xep-0160.html) offline.ModuleName: func(a *serverApp, cfg modulesConfig) module.Module { - return offline.New(a.router, a.hosts, a.resMng, a.rep, a.locker, a.hk, offline.Config{ - QueueSize: cfg.Offline.QueueSize, - }) + return offline.New(cfg.Offline, a.router, a.hosts, a.resMng, a.rep, a.locker, a.hk) }, // XEP-0012: Last Activity // (https://xmpp.org/extensions/xep-0012.html) @@ -82,9 +80,7 @@ var modFns = map[string]func(a *serverApp, cfg modulesConfig) module.Module{ // XEP-0092: Software Version // (https://xmpp.org/extensions/xep-0092.html) xep0092.ModuleName: func(a *serverApp, cfg modulesConfig) module.Module { - return xep0092.New(a.router, xep0092.Config{ - ShowOS: cfg.Version.ShowOS, - }) + return xep0092.New(cfg.Version, a.router) }, // XEP-0115: Entity Capabilities // (https://xmpp.org/extensions/xep-0115.html) @@ -99,22 +95,12 @@ var modFns = map[string]func(a *serverApp, cfg modulesConfig) module.Module{ // XEP-0198: Stream Management // (https://xmpp.org/extensions/xep-0198.html) xep0198.ModuleName: func(a *serverApp, cfg modulesConfig) module.Module { - return xep0198.New(a.router, a.hosts, a.resMng, a.hk, xep0198.Config{ - HibernateTime: cfg.Stream.HibernateTime, - RequestAckInterval: cfg.Stream.RequestAckInterval, - WaitForAckTimeout: cfg.Stream.WaitForAckTimeout, - MaxQueueSize: cfg.Stream.MaxQueueSize, - }) + return xep0198.New(cfg.Stream, a.router, a.hosts, a.resMng, a.hk) }, // XEP-0199: XMPP Ping // (https://xmpp.org/extensions/xep-0199.html) xep0199.ModuleName: func(a *serverApp, cfg modulesConfig) module.Module { - return xep0199.New(a.router, a.hk, xep0199.Config{ - AckTimeout: cfg.Ping.AckTimeout, - Interval: cfg.Ping.Interval, - SendPings: cfg.Ping.SendPings, - TimeoutAction: cfg.Ping.TimeoutAction, - }) + return xep0199.New(cfg.Ping, a.router, a.hk) }, // XEP-0202: Entity Time // (https://xmpp.org/extensions/xep-0202.html) diff --git a/pkg/cmd/jackalctl/ctlv1/command/error.go b/cmd/jackalctl/ctlv1/command/error.go similarity index 100% rename from pkg/cmd/jackalctl/ctlv1/command/error.go rename to cmd/jackalctl/ctlv1/command/error.go diff --git a/pkg/cmd/jackalctl/ctlv1/command/global.go b/cmd/jackalctl/ctlv1/command/global.go similarity index 100% rename from pkg/cmd/jackalctl/ctlv1/command/global.go rename to cmd/jackalctl/ctlv1/command/global.go diff --git a/pkg/cmd/jackalctl/ctlv1/command/printer.go b/cmd/jackalctl/ctlv1/command/printer.go similarity index 100% rename from pkg/cmd/jackalctl/ctlv1/command/printer.go rename to cmd/jackalctl/ctlv1/command/printer.go diff --git a/pkg/cmd/jackalctl/ctlv1/command/user_command.go b/cmd/jackalctl/ctlv1/command/user_command.go similarity index 100% rename from pkg/cmd/jackalctl/ctlv1/command/user_command.go rename to cmd/jackalctl/ctlv1/command/user_command.go diff --git a/pkg/cmd/jackalctl/ctlv1/command/version_command.go b/cmd/jackalctl/ctlv1/command/version_command.go similarity index 100% rename from pkg/cmd/jackalctl/ctlv1/command/version_command.go rename to cmd/jackalctl/ctlv1/command/version_command.go diff --git a/pkg/cmd/jackalctl/ctlv1/ctl.go b/cmd/jackalctl/ctlv1/ctl.go similarity index 97% rename from pkg/cmd/jackalctl/ctlv1/ctl.go rename to cmd/jackalctl/ctlv1/ctl.go index 7f1e6521c..ddd08e668 100644 --- a/pkg/cmd/jackalctl/ctlv1/ctl.go +++ b/cmd/jackalctl/ctlv1/ctl.go @@ -19,7 +19,7 @@ package ctlv1 import ( "time" - "github.com/ortuman/jackal/pkg/cmd/jackalctl/ctlv1/command" + "github.com/ortuman/jackal/cmd/jackalctl/ctlv1/command" "github.com/spf13/cobra" ) diff --git a/pkg/cmd/jackalctl/ctlv1/help.go b/cmd/jackalctl/ctlv1/help.go similarity index 100% rename from pkg/cmd/jackalctl/ctlv1/help.go rename to cmd/jackalctl/ctlv1/help.go diff --git a/pkg/cmd/jackalctl/main.go b/cmd/jackalctl/main.go similarity index 94% rename from pkg/cmd/jackalctl/main.go rename to cmd/jackalctl/main.go index a1537ada3..52603aafb 100644 --- a/pkg/cmd/jackalctl/main.go +++ b/cmd/jackalctl/main.go @@ -20,7 +20,7 @@ import ( "fmt" "os" - "github.com/ortuman/jackal/pkg/cmd/jackalctl/ctlv1" + "github.com/ortuman/jackal/cmd/jackalctl/ctlv1" ) const ( diff --git a/config/example.config.yaml b/config/example.config.yaml index c0760f7af..3d4b42e6f 100644 --- a/config/example.config.yaml +++ b/config/example.config.yaml @@ -36,7 +36,8 @@ cluster: etcd: endpoints: - http://127.0.0.1:2379 - port: 14369 + server: + port: 14369 shapers: - name: super diff --git a/pkg/admin/server/server.go b/pkg/admin/server/server.go index 3d0ed3aad..f1807d6b2 100644 --- a/pkg/admin/server/server.go +++ b/pkg/admin/server/server.go @@ -25,7 +25,7 @@ import ( "github.com/ortuman/jackal/pkg/auth/pepper" "github.com/ortuman/jackal/pkg/hook" "github.com/ortuman/jackal/pkg/log" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" "google.golang.org/grpc" ) @@ -43,17 +43,26 @@ type Server struct { hk *hook.Hooks } +// Config contains Server configuration parameters. +type Config struct { + BindAddr string `fig:"bind_addr"` + Port int `fig:"port" default:"15280"` + Disabled bool `fig:"disabled"` +} + // New returns a new initialized admin server. func New( - bindAddr string, - port int, + cfg Config, rep repository.Repository, peppers *pepper.Keys, hk *hook.Hooks, ) *Server { + if cfg.Disabled { + return nil + } return &Server{ - bindAddr: bindAddr, - port: port, + bindAddr: cfg.BindAddr, + port: cfg.Port, rep: rep, peppers: peppers, hk: hk, diff --git a/pkg/admin/server/service.go b/pkg/admin/server/service.go index 58e7ab9d3..7dc2ef0c7 100644 --- a/pkg/admin/server/service.go +++ b/pkg/admin/server/service.go @@ -30,7 +30,7 @@ import ( "github.com/ortuman/jackal/pkg/hook" "github.com/ortuman/jackal/pkg/log" usermodel "github.com/ortuman/jackal/pkg/model/user" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" "golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/sha3" "google.golang.org/grpc/codes" diff --git a/pkg/auth/interface.go b/pkg/auth/interface.go index d430b52b5..cd1de9211 100644 --- a/pkg/auth/interface.go +++ b/pkg/auth/interface.go @@ -16,7 +16,7 @@ package auth import ( authpb "github.com/ortuman/jackal/pkg/auth/pb" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" "github.com/ortuman/jackal/pkg/transport" ) diff --git a/pkg/auth/pepper/keys.go b/pkg/auth/pepper/keys.go index 3764506e4..892479051 100644 --- a/pkg/auth/pepper/keys.go +++ b/pkg/auth/pepper/keys.go @@ -27,21 +27,27 @@ type Keys struct { useID string } +// Config contains Keys configuration parameters. +type Config struct { + Keys map[string]string `fig:"keys"` + UseID string `fig:"use"` +} + // NewKeys returns an initialized set of pepper keys. -func NewKeys(keys map[string]string, useID string) (*Keys, error) { - if len(keys) == 0 { +func NewKeys(cfg Config) (*Keys, error) { + if len(cfg.Keys) == 0 { return nil, errors.New("pepper: no pepper keys defined") } - for keyID, k := range keys { + for keyID, k := range cfg.Keys { if len(k) < minKeyLength { return nil, fmt.Errorf("pepper: key %s must be at least %d characters", keyID, minKeyLength) } } - _, ok := keys[useID] + _, ok := cfg.Keys[cfg.UseID] if !ok { - return nil, fmt.Errorf("pepper: active key not found: %s", useID) + return nil, fmt.Errorf("pepper: active key not found: %s", cfg.UseID) } - return &Keys{ks: keys, useID: useID}, nil + return &Keys{ks: cfg.Keys, useID: cfg.UseID}, nil } // GetKey returns pepper associated to an identifier. diff --git a/pkg/auth/pepper/keys_test.go b/pkg/auth/pepper/keys_test.go index 4dd8fff10..b4bb05ed7 100644 --- a/pkg/auth/pepper/keys_test.go +++ b/pkg/auth/pepper/keys_test.go @@ -26,11 +26,14 @@ func init() { func TestKeys_Get(t *testing.T) { // given - ks, _ := NewKeys(map[string]string{ - "v1": "k1", - "v2": "k2", - "v3": "k3", - }, "v2") + ks, _ := NewKeys(Config{ + Keys: map[string]string{ + "v1": "k1", + "v2": "k2", + "v3": "k3", + }, + UseID: "v2", + }) // then require.Equal(t, "k2", ks.GetActiveKey()) @@ -42,13 +45,18 @@ func TestKeys_Get(t *testing.T) { func TestKeys_Error(t *testing.T) { // given - _, err1 := NewKeys(map[string]string{ - "v1": "k1", - "v2": "k2", - "v3": "k3", - }, "v4") + _, err1 := NewKeys(Config{ + Keys: map[string]string{ + "v1": "k1", + "v2": "k2", + "v3": "k3", + }, UseID: "v4", + }) - _, err2 := NewKeys(map[string]string{}, "v4") + _, err2 := NewKeys(Config{ + Keys: map[string]string{}, + UseID: "v4", + }) // then require.NotNil(t, err1) diff --git a/pkg/auth/scram.go b/pkg/auth/scram.go index e21e2ca11..2c4a909ef 100644 --- a/pkg/auth/scram.go +++ b/pkg/auth/scram.go @@ -30,7 +30,7 @@ import ( "github.com/jackal-xmpp/stravaganza/v2" "github.com/ortuman/jackal/pkg/auth/pepper" usermodel "github.com/ortuman/jackal/pkg/model/user" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" "github.com/ortuman/jackal/pkg/transport" stringsutil "github.com/ortuman/jackal/pkg/util/strings" "golang.org/x/crypto/sha3" diff --git a/pkg/auth/scram_test.go b/pkg/auth/scram_test.go index 361d2186b..734ac424b 100644 --- a/pkg/auth/scram_test.go +++ b/pkg/auth/scram_test.go @@ -413,7 +413,10 @@ func testUser() *usermodel.User { } func testPeppers() *pepper.Keys { - ks, _ := pepper.NewKeys(map[string]string{"v1": pepperKey}, "v1") + ks, _ := pepper.NewKeys(pepper.Config{ + Keys: map[string]string{"v1": pepperKey}, + UseID: "v1", + }) return ks } diff --git a/pkg/c2s/interface.go b/pkg/c2s/interface.go index b57c699e6..30c4e0f6f 100644 --- a/pkg/c2s/interface.go +++ b/pkg/c2s/interface.go @@ -25,9 +25,9 @@ import ( "github.com/ortuman/jackal/pkg/cluster/kv" c2smodel "github.com/ortuman/jackal/pkg/model/c2s" clustermodel "github.com/ortuman/jackal/pkg/model/cluster" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/router/stream" + "github.com/ortuman/jackal/pkg/storage/repository" "github.com/ortuman/jackal/pkg/transport" ) diff --git a/pkg/c2s/router.go b/pkg/c2s/router.go index 5239a2cdf..1e48f7781 100644 --- a/pkg/c2s/router.go +++ b/pkg/c2s/router.go @@ -27,9 +27,9 @@ import ( clusterrouter "github.com/ortuman/jackal/pkg/cluster/router" "github.com/ortuman/jackal/pkg/hook" "github.com/ortuman/jackal/pkg/log" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/router/stream" + "github.com/ortuman/jackal/pkg/storage/repository" ) type c2sRouter struct { diff --git a/pkg/c2s/socket_listener.go b/pkg/c2s/socket_listener.go index d3b2bbfa3..76a86c52e 100644 --- a/pkg/c2s/socket_listener.go +++ b/pkg/c2s/socket_listener.go @@ -30,9 +30,9 @@ import ( "github.com/ortuman/jackal/pkg/host" "github.com/ortuman/jackal/pkg/log" "github.com/ortuman/jackal/pkg/module" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/shaper" + "github.com/ortuman/jackal/pkg/storage/repository" "github.com/ortuman/jackal/pkg/transport" ) diff --git a/pkg/c2s/socket_listener_test.go b/pkg/c2s/socket_listener_test.go index 894f87957..491c149f6 100644 --- a/pkg/c2s/socket_listener_test.go +++ b/pkg/c2s/socket_listener_test.go @@ -29,7 +29,7 @@ func TestSocketListener_Listen(t *testing.T) { var handledConn uint32 s := &SocketListener{ - addr: ":1123", + addr: ":51124", connHandlerFn: func(_ net.Conn) { atomic.StoreUint32(&handledConn, 1) }, @@ -39,7 +39,7 @@ func TestSocketListener_Listen(t *testing.T) { err := s.Start(context.Background()) require.Nil(t, err) - _, err = net.Dial("tcp", ":1123") + _, err = net.Dial("tcp", ":51124") require.Nil(t, err) time.Sleep(time.Second) // wait to accept diff --git a/pkg/cluster/etcd/dial.go b/pkg/cluster/etcd/dial.go new file mode 100644 index 000000000..f173712af --- /dev/null +++ b/pkg/cluster/etcd/dial.go @@ -0,0 +1,42 @@ +// Copyright 2020 The jackal Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package etcd + +import ( + etcdv3 "github.com/coreos/etcd/clientv3" + grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" + "google.golang.org/grpc" + "google.golang.org/grpc/keepalive" +) + +func dial(cfg Config) (*etcdv3.Client, error) { + dialOptions := []grpc.DialOption{ + grpc.WithBlock(), + grpc.WithKeepaliveParams(keepalive.ClientParameters{ + Time: cfg.KeepAliveTime, + Timeout: cfg.Timeout, + PermitWithoutStream: true, + }), + grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), + grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor), + } + return etcdv3.New(etcdv3.Config{ + Endpoints: cfg.Endpoints, + DialTimeout: cfg.DialTimeout, + DialKeepAliveTime: cfg.DialKeepAliveTime, + DialKeepAliveTimeout: cfg.DialKeepAliveTimeout, + DialOptions: dialOptions, + }) +} diff --git a/pkg/cluster/etcd/etcd.go b/pkg/cluster/etcd/etcd.go new file mode 100644 index 000000000..1b5a463d1 --- /dev/null +++ b/pkg/cluster/etcd/etcd.go @@ -0,0 +1,27 @@ +// Copyright 2020 The jackal Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package etcd + +import "time" + +// Config contains etcd configuration parameters. +type Config struct { + Endpoints []string `fig:"endpoints" default:"[http://localhost:2379]"` + DialTimeout time.Duration `fig:"dial_timeout" default:"20s"` + DialKeepAliveTime time.Duration `fig:"dial_keep_alive_time" default:"30s"` + DialKeepAliveTimeout time.Duration `fig:"dial_keep_alive_timeout" default:"10s"` + KeepAliveTime time.Duration `fig:"keep_alive" default:"10s"` + Timeout time.Duration `fig:"keep_alive" default:"20m"` +} diff --git a/pkg/cluster/kv/etcd/kv.go b/pkg/cluster/etcd/kv.go similarity index 94% rename from pkg/cluster/kv/etcd/kv.go rename to pkg/cluster/etcd/kv.go index 526f55bb4..02ac6e007 100644 --- a/pkg/cluster/kv/etcd/kv.go +++ b/pkg/cluster/etcd/kv.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package etcdkv +package etcd import ( "context" @@ -31,6 +31,7 @@ const ( // KV represents an etcd key-value store implementation. type KV struct { + cfg Config cli *etcdv3.Client leaseID etcdv3.LeaseID ctx context.Context @@ -39,11 +40,11 @@ type KV struct { done int32 } -// New returns a new etcd key-value store instance. -func New(cli *etcdv3.Client) *KV { +// NewKV returns a new etcd key-value store instance. +func NewKV(cfg Config) *KV { ctx, cancel := context.WithCancel(context.Background()) return &KV{ - cli: cli, + cfg: cfg, ctx: ctx, cancelFn: cancel, } @@ -111,6 +112,13 @@ func (k *KV) Watch(ctx context.Context, prefix string, withPrevVal bool) <-chan // Start initializes etcd key-value store. func (k *KV) Start(ctx context.Context) error { + // perform dialing + cli, err := dial(k.cfg) + if err != nil { + return err + } + k.cli = cli + // create shared KV lease resp, err := k.cli.Grant(ctx, leaseTTLInSeconds) if err != nil { @@ -139,6 +147,9 @@ func (k *KV) Stop(ctx context.Context) error { if err != nil { return err } + if err := k.cli.Close(); err != nil { + return err + } log.Infow("Stopped etcd KV store") return nil } diff --git a/pkg/cluster/locker/etcd/locker.go b/pkg/cluster/etcd/locker.go similarity index 67% rename from pkg/cluster/locker/etcd/locker.go rename to pkg/cluster/etcd/locker.go index c5b92a500..a8027336c 100644 --- a/pkg/cluster/locker/etcd/locker.go +++ b/pkg/cluster/etcd/locker.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package etcdlocker +package etcd import ( "context" @@ -29,17 +29,20 @@ type etcdLock struct { func (m *etcdLock) Release(ctx context.Context) error { return m.mu.Unlock(ctx) } -type etcdLocker struct { +// Locker defines etcd locker.Locker implementation. +type Locker struct { + cfg Config cli *etcdv3.Client ss *concurrency.Session } -// New returns a new initialized etcd locker. -func New(cli *etcdv3.Client) locker.Locker { - return &etcdLocker{cli: cli} +// NewLocker returns a new initialized etcd locker. +func NewLocker(cfg Config) *Locker { + return &Locker{cfg: cfg} } -func (l *etcdLocker) AcquireLock(ctx context.Context, lockID string) (locker.Lock, error) { +// AcquireLock acquires and returns an etcd locker. +func (l *Locker) AcquireLock(ctx context.Context, lockID string) (locker.Lock, error) { mu := concurrency.NewMutex(l.ss, lockID) if err := mu.Lock(ctx); err != nil { return nil, err @@ -47,7 +50,15 @@ func (l *etcdLocker) AcquireLock(ctx context.Context, lockID string) (locker.Loc return &etcdLock{mu: mu}, nil } -func (l *etcdLocker) Start(_ context.Context) error { +// Start starts etcd locker. +func (l *Locker) Start(_ context.Context) error { + // perform dialing + cli, err := dial(l.cfg) + if err != nil { + return err + } + l.cli = cli + ss, err := concurrency.NewSession(l.cli) if err != nil { return err @@ -57,10 +68,14 @@ func (l *etcdLocker) Start(_ context.Context) error { return nil } -func (l *etcdLocker) Stop(_ context.Context) error { +// Stop stops etcd locker. +func (l *Locker) Stop(_ context.Context) error { if err := l.ss.Close(); err != nil { return err } + if err := l.cli.Close(); err != nil { + return err + } log.Infof("Stopped etcd locker") return nil } diff --git a/pkg/cluster/server/server.go b/pkg/cluster/server/server.go index 1e5d7c5a0..e98f1fea5 100644 --- a/pkg/cluster/server/server.go +++ b/pkg/cluster/server/server.go @@ -32,8 +32,7 @@ var netListen = net.Listen // Server represents cluster server type. type Server struct { - bindAddr string - port int + cfg Config ln net.Listener srv *grpc.Server active int32 @@ -41,11 +40,16 @@ type Server struct { comps *component.Components } +// Config contains Server configuration parameters. +type Config struct { + BindAddr string `fig:"bind_addr"` + Port int `fig:"port" default:"14369"` +} + // New returns a new initialized Server instance. -func New(bindAddr string, port int, localRouter *c2s.LocalRouter, comps *component.Components) *Server { +func New(cfg Config, localRouter *c2s.LocalRouter, comps *component.Components) *Server { return &Server{ - bindAddr: bindAddr, - port: port, + cfg: cfg, localRouter: localRouter, comps: comps, } @@ -91,5 +95,5 @@ func (s *Server) Stop(_ context.Context) error { } func (s *Server) getAddress() string { - return s.bindAddr + ":" + strconv.Itoa(s.port) + return s.cfg.BindAddr + ":" + strconv.Itoa(s.cfg.Port) } diff --git a/pkg/cluster/server/server_test.go b/pkg/cluster/server/server_test.go index 32a261ac1..0308002f0 100644 --- a/pkg/cluster/server/server_test.go +++ b/pkg/cluster/server/server_test.go @@ -27,8 +27,10 @@ import ( func TestServer_Listen(t *testing.T) { // given s := &Server{ - bindAddr: "127.0.0.1", - port: 56000, + cfg: Config{ + BindAddr: "127.0.0.1", + Port: 56000, + }, } // when diff --git a/pkg/cmd/jackal/config.go b/pkg/cmd/jackal/config.go deleted file mode 100644 index 206f9739a..000000000 --- a/pkg/cmd/jackal/config.go +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright 2020 The jackal Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "path/filepath" - "time" - - "github.com/kkyr/fig" -) - -type peppersConfig struct { - Keys map[string]string `fig:"keys"` - UseID string `fig:"use"` -} - -type loggerConfig struct { - Level string `fig:"level" default:"debug"` - OutputPath string `fig:"output_path"` -} - -type adminConfig struct { - BindAddr string `fig:"bind_addr"` - Port int `fig:"port" default:"15280"` - Disabled bool `fig:"disabled"` -} - -type etcdConfig struct { - Endpoints []string `fig:"endpoints" default:"[http://localhost:2379]"` - DialTimeout time.Duration `fig:"dial_timeout" default:"5s"` -} - -type clusterConfig struct { - Etcd etcdConfig `fig:"etcd"` - BindAddr string `fig:"bind_addr"` - Port int `fig:"port" default:"14369"` -} - -type storageConfig struct { - Type string `fig:"type" default:"pgsql"` - PgSQL struct { - Host string `fig:"host"` - User string `fig:"user"` - Password string `fig:"password"` - Database string `fig:"database"` - SSLMode string `fig:"ssl_mode" default:"disable"` - MaxOpenConns int `fig:"max_open_conns"` - MaxIdleConns int `fig:"max_idle_conns"` - ConnMaxLifetime time.Duration `fig:"conn_max_lifetime"` - ConnMaxIdleTime time.Duration `fig:"conn_max_idle_time"` - } `fig:"pgsql"` -} - -type shaperConfig struct { - Name string `fig:"name"` - MaxSessions int `fig:"max_sessions" default:"10"` - Rate struct { - Limit int `fig:"limit" default:"1000"` - Burst int `fig:"burst" default:"0"` - } `fig:"rate"` - Matching struct { - JID struct { - In []string `fig:"in"` - RegEx string `fig:"regex"` - } - } `fig:"matching"` -} - -type hostConfig struct { - Domain string `fig:"domain"` - TLS struct { - CertFile string `fig:"cert_file"` - PrivateKeyFile string `fig:"privkey_file"` - } `fig:"tls"` -} - -type listenerConfig struct { - Type string `fig:"type" default:"c2s"` - BindAddr string `fig:"bind_addr"` - Port int `fig:"port" default:"5222"` - Transport string `fig:"transport" default:"socket"` - DirectTLS bool `fig:"direct_tls"` - SASL struct { - Mechanisms []string `fig:"mechanisms" default:"[scram_sha_1, scram_sha_256, scram_sha_512, scram_sha3_512]"` - External struct { - Address string `fig:"address"` - IsSecure bool `fig:"is_secure"` - } `fig:"external"` - } `fig:"sasl"` - CompressionLevel string `fig:"compression_level" default:"default"` - ResourceConflict string `fig:"resource_conflict" default:"terminate_old"` - MaxStanzaSize int `fig:"max_stanza_size" default:"32768"` - Secret string `fig:"secret"` - ConnectTimeout time.Duration `fig:"conn_timeout" default:"3s"` - AuthenticateTimeout time.Duration `fig:"auth_timeout" default:"10s"` - KeepAliveTimeout time.Duration `fig:"keep_alive_timeout" default:"10m"` - RequestTimeout time.Duration `fig:"req_timeout" default:"15s"` -} - -type s2sOutConfig struct { - DialTimeout time.Duration `fig:"dial_timeout" default:"5s"` - DialbackSecret string `fig:"secret"` - ConnectTimeout time.Duration `fig:"conn_timeout" default:"3s"` - KeepAliveTimeout time.Duration `fig:"keep_alive_timeout" default:"120s"` - RequestTimeout time.Duration `fig:"req_timeout" default:"15s"` - MaxStanzaSize int `fig:"max_stanza_size" default:"131072"` -} - -type modulesConfig struct { - Enabled []string `fig:"enabled"` - - Offline struct { - QueueSize int `fig:"queue_size" default:"200"` - } `fig:"offline"` - - // XEP-0092: Software Version - Version struct { - ShowOS bool `fig:"show_os"` - } `fig:"version"` - - // XEP-0198: Stream Management - Stream struct { - HibernateTime time.Duration `fig:"hibernate_time" default:"3m"` - RequestAckInterval time.Duration `fig:"request_ack_interval" default:"2m"` - WaitForAckTimeout time.Duration `fig:"wait_for_ack_timeout" default:"30s"` - MaxQueueSize int `fig:"max_queue_size" default:"30"` - } - - // XEP-0199: XMPP Ping - Ping struct { - AckTimeout time.Duration `fig:"ack_timeout" default:"32s"` - Interval time.Duration `fig:"interval" default:"1m"` - SendPings bool `fig:"send_pings"` - TimeoutAction string `fig:"timeout_action" default:"none"` - } `fig:"ping"` -} - -type componentsConfig struct { -} - -type serverConfig struct { - HTTPPort int `fig:"http_port" default:"6060"` - Peppers peppersConfig `fig:"peppers"` - Logger loggerConfig `fig:"logger"` - Admin adminConfig `fig:"admin"` - Cluster clusterConfig `fig:"cluster"` - Storage storageConfig `fig:"storage"` - Hosts []hostConfig `fig:"hosts"` - Listeners []listenerConfig `fig:"listeners"` - Shapers []shaperConfig `fig:"shapers"` - S2SOut s2sOutConfig `fig:"s2s_out"` - Modules modulesConfig `fig:"modules"` - Components componentsConfig `fig:"components"` -} - -func loadConfig(configFile string) (*serverConfig, error) { - var cfg serverConfig - file := filepath.Base(configFile) - dir := filepath.Dir(configFile) - - err := fig.Load(&cfg, fig.File(file), fig.Dirs(dir)) - if err != nil { - return nil, err - } - return &cfg, nil -} diff --git a/pkg/component/xep0114/socket_listener_test.go b/pkg/component/xep0114/socket_listener_test.go index 6825c1ef7..f0e124c99 100644 --- a/pkg/component/xep0114/socket_listener_test.go +++ b/pkg/component/xep0114/socket_listener_test.go @@ -29,7 +29,7 @@ func TestSocketListener_Listen(t *testing.T) { var handledConn uint32 s := &SocketListener{ - addr: ":1123", + addr: ":51123", stmHub: newInHub(), connHandlerFn: func(_ net.Conn) { atomic.StoreUint32(&handledConn, 1) @@ -40,7 +40,7 @@ func TestSocketListener_Listen(t *testing.T) { err := s.Start(context.Background()) require.Nil(t, err) - _, err = net.Dial("tcp", ":1123") + _, err = net.Dial("tcp", ":51123") require.Nil(t, err) time.Sleep(time.Second) // wait to accept diff --git a/pkg/host/hosts.go b/pkg/host/hosts.go index d04435e6b..00ead6a39 100644 --- a/pkg/host/hosts.go +++ b/pkg/host/hosts.go @@ -18,8 +18,12 @@ import ( "crypto/tls" "sort" "sync" + + tlsutil "github.com/ortuman/jackal/pkg/util/tls" ) +const defaultDomain = "localhost" + // Hosts type represents all local domains set. type Hosts struct { mu sync.RWMutex @@ -27,11 +31,40 @@ type Hosts struct { hosts map[string]tls.Certificate } -// New creates and returns an empty Hosts instance. -func New() *Hosts { - return &Hosts{ +// Config contains host configuration parameters. +type Config struct { + Domain string `fig:"domain"` + TLS struct { + CertFile string `fig:"cert_file"` + PrivateKeyFile string `fig:"privkey_file"` + } `fig:"tls"` +} + +// NewHost creates and initializes a Hosts instance. +func NewHost(configs []Config) (*Hosts, error) { + hs := &Hosts{ hosts: make(map[string]tls.Certificate), } + if len(configs) == 0 { + cer, err := tlsutil.LoadCertificate("", "", defaultDomain) + if err != nil { + return nil, err + } + hs.RegisterDefaultHost(defaultDomain, cer) + return hs, nil + } + for i, config := range configs { + cer, err := tlsutil.LoadCertificate(config.TLS.PrivateKeyFile, config.TLS.CertFile, config.Domain) + if err != nil { + return nil, err + } + if i == 0 { + hs.RegisterDefaultHost(config.Domain, cer) + } else { + hs.RegisterHost(config.Domain, cer) + } + } + return hs, nil } // RegisterDefaultHost registers default host value along with its certificate. diff --git a/pkg/host/hosts_test.go b/pkg/host/hosts_test.go index 8200938d2..fa7ef4591 100644 --- a/pkg/host/hosts_test.go +++ b/pkg/host/hosts_test.go @@ -23,7 +23,9 @@ import ( func TestHosts_Default(t *testing.T) { // given - h := New() + h := &Hosts{ + hosts: make(map[string]tls.Certificate), + } // when cer := tls.Certificate{} @@ -36,7 +38,9 @@ func TestHosts_Default(t *testing.T) { func TestHosts_Domains(t *testing.T) { // given - h := New() + h := &Hosts{ + hosts: make(map[string]tls.Certificate), + } // when c1 := tls.Certificate{} diff --git a/pkg/module/offline/interface.go b/pkg/module/offline/interface.go index de538eed3..b692f4617 100644 --- a/pkg/module/offline/interface.go +++ b/pkg/module/offline/interface.go @@ -20,8 +20,8 @@ import ( c2smodel "github.com/ortuman/jackal/pkg/model/c2s" "github.com/ortuman/jackal/pkg/cluster/locker" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out repository.mock_test.go . offlineRepository:repositoryMock diff --git a/pkg/module/offline/offline.go b/pkg/module/offline/offline.go index e40320e2e..de28dfa6a 100644 --- a/pkg/module/offline/offline.go +++ b/pkg/module/offline/offline.go @@ -26,8 +26,8 @@ import ( "github.com/ortuman/jackal/pkg/hook" "github.com/ortuman/jackal/pkg/host" "github.com/ortuman/jackal/pkg/log" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) @@ -43,7 +43,7 @@ const ModuleName = "offline" // Config contains offline module configuration value. type Config struct { // QueueSize defines maximum offline queue size. - QueueSize int + QueueSize int `fig:"queue_size" default:"200"` } // Offline represents offline module type. @@ -59,13 +59,13 @@ type Offline struct { // New creates and initializes a new Offline instance. func New( + cfg Config, router router.Router, hosts *host.Hosts, resMng *c2s.ResourceManager, rep repository.Offline, locker locker.Locker, hk *hook.Hooks, - cfg Config, ) *Offline { return &Offline{ cfg: cfg, diff --git a/pkg/module/roster/interface.go b/pkg/module/roster/interface.go index 9260f9049..77f063d81 100644 --- a/pkg/module/roster/interface.go +++ b/pkg/module/roster/interface.go @@ -18,10 +18,9 @@ import ( "context" c2smodel "github.com/ortuman/jackal/pkg/model/c2s" - - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/router/stream" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out repository.mock_test.go . globalRepository:repositoryMock diff --git a/pkg/module/roster/roster.go b/pkg/module/roster/roster.go index 954c68aaa..f374bc6ad 100644 --- a/pkg/module/roster/roster.go +++ b/pkg/module/roster/roster.go @@ -31,8 +31,8 @@ import ( "github.com/ortuman/jackal/pkg/host" "github.com/ortuman/jackal/pkg/log" rostermodel "github.com/ortuman/jackal/pkg/model/roster" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) diff --git a/pkg/module/roster/roster_test.go b/pkg/module/roster/roster_test.go index 4194a1aa0..d32bf455f 100644 --- a/pkg/module/roster/roster_test.go +++ b/pkg/module/roster/roster_test.go @@ -25,9 +25,9 @@ import ( "github.com/jackal-xmpp/stravaganza/v2/jid" "github.com/ortuman/jackal/pkg/hook" rostermodel "github.com/ortuman/jackal/pkg/model/roster" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/router/stream" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" "github.com/stretchr/testify/require" ) diff --git a/pkg/module/xep0012/interface.go b/pkg/module/xep0012/interface.go index 2bb73fe3d..470f0ea04 100644 --- a/pkg/module/xep0012/interface.go +++ b/pkg/module/xep0012/interface.go @@ -19,8 +19,8 @@ import ( c2smodel "github.com/ortuman/jackal/pkg/model/c2s" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out repository.mock_test.go . globalRepository:repositoryMock diff --git a/pkg/module/xep0012/last.go b/pkg/module/xep0012/last.go index d3c80d208..b55da9de2 100644 --- a/pkg/module/xep0012/last.go +++ b/pkg/module/xep0012/last.go @@ -28,8 +28,8 @@ import ( "github.com/ortuman/jackal/pkg/log" lastmodel "github.com/ortuman/jackal/pkg/model/last" rostermodel "github.com/ortuman/jackal/pkg/model/roster" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) diff --git a/pkg/module/xep0030/account_provider.go b/pkg/module/xep0030/account_provider.go index c338bb08d..1f73ef820 100644 --- a/pkg/module/xep0030/account_provider.go +++ b/pkg/module/xep0030/account_provider.go @@ -24,7 +24,7 @@ import ( rostermodel "github.com/ortuman/jackal/pkg/model/roster" "github.com/ortuman/jackal/pkg/module" "github.com/ortuman/jackal/pkg/module/xep0004" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type accountProvider struct { diff --git a/pkg/module/xep0030/disco.go b/pkg/module/xep0030/disco.go index 2bd83afd2..4ddba5e27 100644 --- a/pkg/module/xep0030/disco.go +++ b/pkg/module/xep0030/disco.go @@ -28,8 +28,8 @@ import ( "github.com/ortuman/jackal/pkg/log" discomodel "github.com/ortuman/jackal/pkg/model/disco" "github.com/ortuman/jackal/pkg/module/xep0004" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) diff --git a/pkg/module/xep0030/interface.go b/pkg/module/xep0030/interface.go index 935a9f992..755df9f71 100644 --- a/pkg/module/xep0030/interface.go +++ b/pkg/module/xep0030/interface.go @@ -21,8 +21,8 @@ import ( "github.com/ortuman/jackal/pkg/component" "github.com/ortuman/jackal/pkg/module" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out roster_repository.mock_test.go . rosterRepository diff --git a/pkg/module/xep0049/interface.go b/pkg/module/xep0049/interface.go index 2bcd6f7a4..66e89e607 100644 --- a/pkg/module/xep0049/interface.go +++ b/pkg/module/xep0049/interface.go @@ -15,8 +15,8 @@ package xep0049 import ( - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out repository.mock_test.go . privateRepository:repositoryMock diff --git a/pkg/module/xep0049/private.go b/pkg/module/xep0049/private.go index d9eba0eb6..d793a5bef 100644 --- a/pkg/module/xep0049/private.go +++ b/pkg/module/xep0049/private.go @@ -22,8 +22,8 @@ import ( stanzaerror "github.com/jackal-xmpp/stravaganza/v2/errors/stanza" "github.com/ortuman/jackal/pkg/hook" "github.com/ortuman/jackal/pkg/log" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) diff --git a/pkg/module/xep0054/interface.go b/pkg/module/xep0054/interface.go index 0a7a57b9d..69672988c 100644 --- a/pkg/module/xep0054/interface.go +++ b/pkg/module/xep0054/interface.go @@ -15,8 +15,8 @@ package xep0054 import ( - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out repository.mock_test.go . vCardRepository:repositoryMock diff --git a/pkg/module/xep0054/vcard.go b/pkg/module/xep0054/vcard.go index 3667d1abc..2d3d00caf 100644 --- a/pkg/module/xep0054/vcard.go +++ b/pkg/module/xep0054/vcard.go @@ -21,8 +21,8 @@ import ( stanzaerror "github.com/jackal-xmpp/stravaganza/v2/errors/stanza" "github.com/ortuman/jackal/pkg/hook" "github.com/ortuman/jackal/pkg/log" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) diff --git a/pkg/module/xep0092/version.go b/pkg/module/xep0092/version.go index 13b686d08..c89a39691 100644 --- a/pkg/module/xep0092/version.go +++ b/pkg/module/xep0092/version.go @@ -45,7 +45,7 @@ const ( // Config contains version module configuration options. type Config struct { // ShowOS tells whether OS info should be revealed or not. - ShowOS bool + ShowOS bool `fig:"show_os"` } // Version represents a version (XEP-0092) module type. @@ -56,7 +56,7 @@ type Version struct { } // New returns a new initialized version instance. -func New(router router.Router, cfg Config) *Version { +func New(cfg Config, router router.Router) *Version { return &Version{ router: router, cfg: cfg, diff --git a/pkg/module/xep0115/caps.go b/pkg/module/xep0115/caps.go index 47af1525d..2b7df97a0 100644 --- a/pkg/module/xep0115/caps.go +++ b/pkg/module/xep0115/caps.go @@ -37,8 +37,8 @@ import ( discomodel "github.com/ortuman/jackal/pkg/model/disco" "github.com/ortuman/jackal/pkg/module/xep0004" "github.com/ortuman/jackal/pkg/module/xep0030" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) const ( diff --git a/pkg/module/xep0115/interface.go b/pkg/module/xep0115/interface.go index 81bc822ad..dcf15ef26 100644 --- a/pkg/module/xep0115/interface.go +++ b/pkg/module/xep0115/interface.go @@ -15,8 +15,8 @@ package xep0115 import ( - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out repository.mock_test.go . capsRepository:repositoryMock diff --git a/pkg/module/xep0191/blocklist.go b/pkg/module/xep0191/blocklist.go index 924737e55..b1f95c369 100644 --- a/pkg/module/xep0191/blocklist.go +++ b/pkg/module/xep0191/blocklist.go @@ -30,8 +30,8 @@ import ( "github.com/ortuman/jackal/pkg/log" blocklistmodel "github.com/ortuman/jackal/pkg/model/blocklist" rostermodel "github.com/ortuman/jackal/pkg/model/roster" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" ) diff --git a/pkg/module/xep0191/blocklist_test.go b/pkg/module/xep0191/blocklist_test.go index 49448da50..f03549856 100644 --- a/pkg/module/xep0191/blocklist_test.go +++ b/pkg/module/xep0191/blocklist_test.go @@ -25,9 +25,9 @@ import ( blocklistmodel "github.com/ortuman/jackal/pkg/model/blocklist" c2smodel "github.com/ortuman/jackal/pkg/model/c2s" rostermodel "github.com/ortuman/jackal/pkg/model/roster" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/router/stream" + "github.com/ortuman/jackal/pkg/storage/repository" xmpputil "github.com/ortuman/jackal/pkg/util/xmpp" "github.com/stretchr/testify/require" ) diff --git a/pkg/module/xep0191/interface.go b/pkg/module/xep0191/interface.go index 329c2f359..7f30bb633 100644 --- a/pkg/module/xep0191/interface.go +++ b/pkg/module/xep0191/interface.go @@ -19,9 +19,9 @@ import ( c2smodel "github.com/ortuman/jackal/pkg/model/c2s" - "github.com/ortuman/jackal/pkg/repository" "github.com/ortuman/jackal/pkg/router" "github.com/ortuman/jackal/pkg/router/stream" + "github.com/ortuman/jackal/pkg/storage/repository" ) //go:generate moq -out router.mock_test.go . globalRouter:routerMock diff --git a/pkg/module/xep0198/stream.go b/pkg/module/xep0198/stream.go index 4a7e6129e..352dabcf2 100644 --- a/pkg/module/xep0198/stream.go +++ b/pkg/module/xep0198/stream.go @@ -63,20 +63,20 @@ const ( // Config contains stream management module configuration options. type Config struct { - // HibernateTime defines defines the amount of time a stream + // HibernateTime defines the amount of time a stream // can stay in disconnected state before being terminated. - HibernateTime time.Duration + HibernateTime time.Duration `fig:"hibernate_time" default:"3m"` // RequestAckInterval defines the period of stream inactivity // that should be waited before requesting acknowledgement. - RequestAckInterval time.Duration + RequestAckInterval time.Duration `fig:"request_ack_interval" default:"2m"` // WaitForAckTimeout defines stanza acknowledgement timeout. - WaitForAckTimeout time.Duration + WaitForAckTimeout time.Duration `fig:"wait_for_ack_timeout" default:"30s"` // MaxQueueSize defines maximum number of unacknowledged stanzas. // When the limit is reached the c2s stream is terminated. - MaxQueueSize int + MaxQueueSize int `fig:"max_queue_size" default:"30"` } // Stream represents a stream (XEP-0198) module type. @@ -94,11 +94,11 @@ type Stream struct { // New returns a new initialized Stream instance. func New( + cfg Config, router router.Router, hosts *host.Hosts, resMng *c2s.ResourceManager, hk *hook.Hooks, - cfg Config, ) *Stream { return &Stream{ cfg: cfg, diff --git a/pkg/module/xep0199/ping.go b/pkg/module/xep0199/ping.go index 9ed4ab338..513bc787a 100644 --- a/pkg/module/xep0199/ping.go +++ b/pkg/module/xep0199/ping.go @@ -50,16 +50,13 @@ const ( // Config contains ping module configuration options. type Config struct { // AckTimeout tells how long should we wait until considering a client to be disconnected. - AckTimeout time.Duration - + AckTimeout time.Duration `fig:"ack_timeout" default:"32s"` // Interval tells how often pings should be sent to clients. - Interval time.Duration - - // SendPings tells whether or not server pings should be sent. - SendPings bool - + Interval time.Duration `fig:"interval" default:"1m"` + // SendPings tells whether server pings should be sent. + SendPings bool `fig:"send_pings"` // TimeoutAction specifies the action to be taken when a client is considered as disconnected. - TimeoutAction string + TimeoutAction string `fig:"timeout_action" default:"none"` } // Ping represents ping (XEP-0199) module type. @@ -74,7 +71,7 @@ type Ping struct { } // New returns a new initialized ping instance. -func New(router router.Router, hk *hook.Hooks, cfg Config) *Ping { +func New(cfg Config, router router.Router, hk *hook.Hooks) *Ping { return &Ping{ cfg: cfg, router: router, diff --git a/pkg/module/xep0199/ping_test.go b/pkg/module/xep0199/ping_test.go index 23b3a356f..1c7f93714 100644 --- a/pkg/module/xep0199/ping_test.go +++ b/pkg/module/xep0199/ping_test.go @@ -39,7 +39,7 @@ func TestPing_Pong(t *testing.T) { _ = stanza.ToXML(outBuf, true) return nil, nil } - p := New(routerMock, &hook.Hooks{}, Config{}) + p := New(Config{}, routerMock, &hook.Hooks{}) // when iq, _ := stravaganza.NewIQBuilder(). @@ -72,10 +72,10 @@ func TestPing_SendPing(t *testing.T) { return nil, nil } hk := hook.NewHooks() - p := New(routerMock, hk, Config{ + p := New(Config{ Interval: time.Millisecond * 500, SendPings: true, - }) + }, routerMock, hk) jd, _ := jid.NewWithString("ortuman@jackal.im/yard", true) // when @@ -116,12 +116,12 @@ func TestPing_Timeout(t *testing.T) { } hk := hook.NewHooks() - p := New(routerMock, hk, Config{ + p := New(Config{ Interval: time.Millisecond * 500, AckTimeout: time.Millisecond * 250, SendPings: true, TimeoutAction: killAction, - }) + }, routerMock, hk) jd, _ := jid.NewWithString("ortuman@jackal.im/yard", true) // when diff --git a/pkg/s2s/socket_listener_test.go b/pkg/s2s/socket_listener_test.go index 4bf12249d..a07e8eb4f 100644 --- a/pkg/s2s/socket_listener_test.go +++ b/pkg/s2s/socket_listener_test.go @@ -29,7 +29,7 @@ func TestSocketListener_Listen(t *testing.T) { var handledConn uint32 s := &SocketListener{ - addr: ":1123", + addr: ":51125", connHandlerFn: func(_ net.Conn) { atomic.StoreUint32(&handledConn, 1) }, @@ -39,7 +39,7 @@ func TestSocketListener_Listen(t *testing.T) { err := s.Start(context.Background()) require.Nil(t, err) - _, err = net.Dial("tcp", ":1123") + _, err = net.Dial("tcp", ":51125") require.Nil(t, err) time.Sleep(time.Second) // wait to accept diff --git a/pkg/shaper/shaper.go b/pkg/shaper/shaper.go index a5df4815c..eafb109dd 100644 --- a/pkg/shaper/shaper.go +++ b/pkg/shaper/shaper.go @@ -59,21 +59,54 @@ func (ss Shapers) DefaultS2S() *Shaper { // Shaper represents a connection traffic constraint set. type Shaper struct { - // MaxSessions represens maximum sessions count. + // Name is the shaper name. + Name string + + // MaxSessions represents maximum sessions count. MaxSessions int rateLimit, burst int jidMatcher stringmatcher.Matcher } +// Config contains Shaper configuration parameters. +type Config struct { + Name string `fig:"name"` + MaxSessions int `fig:"max_sessions" default:"10"` + Rate struct { + Limit int `fig:"limit" default:"1000"` + Burst int `fig:"burst" default:"0"` + } `fig:"rate"` + Matching struct { + JID struct { + In []string `fig:"in"` + RegEx string `fig:"regex"` + } + } `fig:"matching"` +} + // New returns a new Shaper given a configuration. -func New(maxSessions int, rateLimit int, burst int, jidMatcher stringmatcher.Matcher) Shaper { +func New(cfg Config) (Shaper, error) { + var jidMatcher stringmatcher.Matcher + switch { + case len(cfg.Matching.JID.In) > 0: + jidMatcher = stringmatcher.NewStringMatcher(cfg.Matching.JID.In) + case len(cfg.Matching.JID.RegEx) > 0: + var err error + jidMatcher, err = stringmatcher.NewRegExMatcher(cfg.Matching.JID.RegEx) + if err != nil { + return Shaper{}, err + } + default: + jidMatcher = stringmatcher.Any + } return Shaper{ - MaxSessions: maxSessions, - rateLimit: rateLimit, - burst: burst, + Name: cfg.Name, + MaxSessions: cfg.MaxSessions, + rateLimit: cfg.Rate.Limit, + burst: cfg.Rate.Burst, jidMatcher: jidMatcher, - } + }, nil } // RateLimiter returns a new rate limiter configured with shaper parameters. diff --git a/pkg/shaper/shaper_test.go b/pkg/shaper/shaper_test.go index bdab42fad..8414347e2 100644 --- a/pkg/shaper/shaper_test.go +++ b/pkg/shaper/shaper_test.go @@ -25,7 +25,13 @@ import ( func TestShaper_RateLimiter(t *testing.T) { // given - s := New(5, 2000, 1000, stringmatcher.Any) + s := Shaper{ + Name: "foo", + MaxSessions: 5, + rateLimit: 2000, + burst: 1000, + jidMatcher: stringmatcher.Any, + } // when rLim := s.RateLimiter() @@ -38,7 +44,13 @@ func TestShaper_RateLimiter(t *testing.T) { func TestShapers_MatchingJID(t *testing.T) { // given var ss Shapers - ss = append(ss, New(5, 2000, 1000, stringmatcher.Any)) + ss = append(ss, Shaper{ + Name: "foo", + MaxSessions: 5, + rateLimit: 2000, + burst: 1000, + jidMatcher: stringmatcher.Any, + }) j, _ := jid.NewWithString("ortuman@gmail.com", true) diff --git a/pkg/repository/measured/blocklist.go b/pkg/storage/measured/blocklist.go similarity index 97% rename from pkg/repository/measured/blocklist.go rename to pkg/storage/measured/blocklist.go index 19d661610..b7e684723 100644 --- a/pkg/repository/measured/blocklist.go +++ b/pkg/storage/measured/blocklist.go @@ -19,7 +19,7 @@ import ( "time" blocklistmodel "github.com/ortuman/jackal/pkg/model/blocklist" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredBlockListRep struct { diff --git a/pkg/repository/measured/blocklist_test.go b/pkg/storage/measured/blocklist_test.go similarity index 100% rename from pkg/repository/measured/blocklist_test.go rename to pkg/storage/measured/blocklist_test.go diff --git a/pkg/repository/measured/capabilities.go b/pkg/storage/measured/capabilities.go similarity index 96% rename from pkg/repository/measured/capabilities.go rename to pkg/storage/measured/capabilities.go index 87ffe1c6a..7889bbab9 100644 --- a/pkg/repository/measured/capabilities.go +++ b/pkg/storage/measured/capabilities.go @@ -19,7 +19,7 @@ import ( "time" capsmodel "github.com/ortuman/jackal/pkg/model/caps" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredCapabilitiesRep struct { diff --git a/pkg/repository/measured/capabilities_test.go b/pkg/storage/measured/capabilities_test.go similarity index 100% rename from pkg/repository/measured/capabilities_test.go rename to pkg/storage/measured/capabilities_test.go diff --git a/pkg/repository/measured/interface.go b/pkg/storage/measured/interface.go similarity index 92% rename from pkg/repository/measured/interface.go rename to pkg/storage/measured/interface.go index 8caf251b5..fce5f68ec 100644 --- a/pkg/repository/measured/interface.go +++ b/pkg/storage/measured/interface.go @@ -14,7 +14,7 @@ package measuredrepository -import "github.com/ortuman/jackal/pkg/repository" +import "github.com/ortuman/jackal/pkg/storage/repository" //go:generate moq -out repository.mock_test.go . c2sRepository:repositoryMock type c2sRepository interface { diff --git a/pkg/repository/measured/last.go b/pkg/storage/measured/last.go similarity index 96% rename from pkg/repository/measured/last.go rename to pkg/storage/measured/last.go index 0b934b867..1629c8426 100644 --- a/pkg/repository/measured/last.go +++ b/pkg/storage/measured/last.go @@ -19,7 +19,7 @@ import ( "time" lastmodel "github.com/ortuman/jackal/pkg/model/last" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredLastRep struct { diff --git a/pkg/repository/measured/last_test.go b/pkg/storage/measured/last_test.go similarity index 100% rename from pkg/repository/measured/last_test.go rename to pkg/storage/measured/last_test.go diff --git a/pkg/repository/measured/measured.go b/pkg/storage/measured/measured.go similarity index 98% rename from pkg/repository/measured/measured.go rename to pkg/storage/measured/measured.go index f6bcce9e8..9504f0e27 100644 --- a/pkg/repository/measured/measured.go +++ b/pkg/storage/measured/measured.go @@ -19,7 +19,7 @@ import ( "strconv" "github.com/ortuman/jackal/pkg/cluster/instance" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" "github.com/prometheus/client_golang/prometheus" ) diff --git a/pkg/repository/measured/metrics.go b/pkg/storage/measured/metrics.go similarity index 100% rename from pkg/repository/measured/metrics.go rename to pkg/storage/measured/metrics.go diff --git a/pkg/repository/measured/offline.go b/pkg/storage/measured/offline.go similarity index 97% rename from pkg/repository/measured/offline.go rename to pkg/storage/measured/offline.go index d18f10340..fff690f00 100644 --- a/pkg/repository/measured/offline.go +++ b/pkg/storage/measured/offline.go @@ -19,7 +19,7 @@ import ( "time" "github.com/jackal-xmpp/stravaganza/v2" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredOfflineRep struct { diff --git a/pkg/repository/measured/offline_test.go b/pkg/storage/measured/offline_test.go similarity index 100% rename from pkg/repository/measured/offline_test.go rename to pkg/storage/measured/offline_test.go diff --git a/pkg/repository/measured/private.go b/pkg/storage/measured/private.go similarity index 96% rename from pkg/repository/measured/private.go rename to pkg/storage/measured/private.go index 2f4975a19..15a56d441 100644 --- a/pkg/repository/measured/private.go +++ b/pkg/storage/measured/private.go @@ -19,7 +19,7 @@ import ( "time" "github.com/jackal-xmpp/stravaganza/v2" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredPrivateRep struct { diff --git a/pkg/repository/measured/private_test.go b/pkg/storage/measured/private_test.go similarity index 100% rename from pkg/repository/measured/private_test.go rename to pkg/storage/measured/private_test.go diff --git a/pkg/repository/measured/roster.go b/pkg/storage/measured/roster.go similarity index 98% rename from pkg/repository/measured/roster.go rename to pkg/storage/measured/roster.go index 921f7a2b0..418eb7246 100644 --- a/pkg/repository/measured/roster.go +++ b/pkg/storage/measured/roster.go @@ -19,7 +19,7 @@ import ( "time" rostermodel "github.com/ortuman/jackal/pkg/model/roster" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredRosterRep struct { diff --git a/pkg/repository/measured/roster_test.go b/pkg/storage/measured/roster_test.go similarity index 100% rename from pkg/repository/measured/roster_test.go rename to pkg/storage/measured/roster_test.go diff --git a/pkg/repository/measured/user.go b/pkg/storage/measured/user.go similarity index 97% rename from pkg/repository/measured/user.go rename to pkg/storage/measured/user.go index dcaccd52a..9be0959a2 100644 --- a/pkg/repository/measured/user.go +++ b/pkg/storage/measured/user.go @@ -20,7 +20,7 @@ import ( usermodel "github.com/ortuman/jackal/pkg/model/user" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredUserRep struct { diff --git a/pkg/repository/measured/user_test.go b/pkg/storage/measured/user_test.go similarity index 100% rename from pkg/repository/measured/user_test.go rename to pkg/storage/measured/user_test.go diff --git a/pkg/repository/measured/vcard.go b/pkg/storage/measured/vcard.go similarity index 96% rename from pkg/repository/measured/vcard.go rename to pkg/storage/measured/vcard.go index a6ac265cb..e0824d153 100644 --- a/pkg/repository/measured/vcard.go +++ b/pkg/storage/measured/vcard.go @@ -19,7 +19,7 @@ import ( "time" "github.com/jackal-xmpp/stravaganza/v2" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type measuredVCardRep struct { diff --git a/pkg/repository/measured/vcard_test.go b/pkg/storage/measured/vcard_test.go similarity index 100% rename from pkg/repository/measured/vcard_test.go rename to pkg/storage/measured/vcard_test.go diff --git a/pkg/repository/pgsql/blocklist.go b/pkg/storage/pgsql/blocklist.go similarity index 100% rename from pkg/repository/pgsql/blocklist.go rename to pkg/storage/pgsql/blocklist.go diff --git a/pkg/repository/pgsql/blocklist_test.go b/pkg/storage/pgsql/blocklist_test.go similarity index 100% rename from pkg/repository/pgsql/blocklist_test.go rename to pkg/storage/pgsql/blocklist_test.go diff --git a/pkg/repository/pgsql/capabilities.go b/pkg/storage/pgsql/capabilities.go similarity index 100% rename from pkg/repository/pgsql/capabilities.go rename to pkg/storage/pgsql/capabilities.go diff --git a/pkg/repository/pgsql/capabilities_test.go b/pkg/storage/pgsql/capabilities_test.go similarity index 100% rename from pkg/repository/pgsql/capabilities_test.go rename to pkg/storage/pgsql/capabilities_test.go diff --git a/pkg/repository/pgsql/last.go b/pkg/storage/pgsql/last.go similarity index 100% rename from pkg/repository/pgsql/last.go rename to pkg/storage/pgsql/last.go diff --git a/pkg/repository/pgsql/last_test.go b/pkg/storage/pgsql/last_test.go similarity index 100% rename from pkg/repository/pgsql/last_test.go rename to pkg/storage/pgsql/last_test.go diff --git a/pkg/repository/pgsql/offline.go b/pkg/storage/pgsql/offline.go similarity index 100% rename from pkg/repository/pgsql/offline.go rename to pkg/storage/pgsql/offline.go diff --git a/pkg/repository/pgsql/offline_test.go b/pkg/storage/pgsql/offline_test.go similarity index 100% rename from pkg/repository/pgsql/offline_test.go rename to pkg/storage/pgsql/offline_test.go diff --git a/pkg/repository/pgsql/pgsql.go b/pkg/storage/pgsql/pgsql.go similarity index 100% rename from pkg/repository/pgsql/pgsql.go rename to pkg/storage/pgsql/pgsql.go diff --git a/pkg/repository/pgsql/pgsql_test.go b/pkg/storage/pgsql/pgsql_test.go similarity index 100% rename from pkg/repository/pgsql/pgsql_test.go rename to pkg/storage/pgsql/pgsql_test.go diff --git a/pkg/repository/pgsql/private.go b/pkg/storage/pgsql/private.go similarity index 100% rename from pkg/repository/pgsql/private.go rename to pkg/storage/pgsql/private.go diff --git a/pkg/repository/pgsql/private_test.go b/pkg/storage/pgsql/private_test.go similarity index 100% rename from pkg/repository/pgsql/private_test.go rename to pkg/storage/pgsql/private_test.go diff --git a/pkg/repository/pgsql/repository.go b/pkg/storage/pgsql/repository.go similarity index 81% rename from pkg/repository/pgsql/repository.go rename to pkg/storage/pgsql/repository.go index a5f7ab0ca..0dd75bc54 100644 --- a/pkg/repository/pgsql/repository.go +++ b/pkg/storage/pgsql/repository.go @@ -23,19 +23,24 @@ import ( sq "github.com/Masterminds/squirrel" _ "github.com/lib/pq" // PostgreSQL driver "github.com/ortuman/jackal/pkg/log" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) func init() { sq.StatementBuilder = sq.StatementBuilder.PlaceholderFormat(sq.Dollar) } -// Config contais PgSQL configuration value. +// Config contains PgSQL configuration value. type Config struct { - MaxIdleConns int - MaxOpenConns int - ConnMaxIdleTime time.Duration - ConnMaxLifetime time.Duration + Host string `fig:"host"` + User string `fig:"user"` + Password string `fig:"password"` + Database string `fig:"database"` + SSLMode string `fig:"ssl_mode" default:"disable"` + MaxOpenConns int `fig:"max_open_conns"` + MaxIdleConns int `fig:"max_idle_conns"` + ConnMaxLifetime time.Duration `fig:"conn_max_lifetime"` + ConnMaxIdleTime time.Duration `fig:"conn_max_idle_time"` } // Repository represents a PgSQL repository implementation. @@ -57,10 +62,10 @@ type Repository struct { } // New creates and returns an initialized PgSQL Repository instance. -func New(host, username, password, database, sslMode string, cfg Config) *Repository { - dsn := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s", username, password, host, database, sslMode) +func New(cfg Config) *Repository { + dsn := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s", cfg.User, cfg.Password, cfg.Host, cfg.Database, cfg.SSLMode) return &Repository{ - host: host, + host: cfg.Host, dsn: dsn, cfg: cfg, } diff --git a/pkg/repository/pgsql/roster.go b/pkg/storage/pgsql/roster.go similarity index 100% rename from pkg/repository/pgsql/roster.go rename to pkg/storage/pgsql/roster.go diff --git a/pkg/repository/pgsql/roster_test.go b/pkg/storage/pgsql/roster_test.go similarity index 100% rename from pkg/repository/pgsql/roster_test.go rename to pkg/storage/pgsql/roster_test.go diff --git a/pkg/repository/pgsql/tx.go b/pkg/storage/pgsql/tx.go similarity index 95% rename from pkg/repository/pgsql/tx.go rename to pkg/storage/pgsql/tx.go index 61d457293..cc14cd84e 100644 --- a/pkg/repository/pgsql/tx.go +++ b/pkg/storage/pgsql/tx.go @@ -17,7 +17,7 @@ package pgsqlrepository import ( "database/sql" - "github.com/ortuman/jackal/pkg/repository" + "github.com/ortuman/jackal/pkg/storage/repository" ) type repTx struct { diff --git a/pkg/repository/pgsql/user.go b/pkg/storage/pgsql/user.go similarity index 100% rename from pkg/repository/pgsql/user.go rename to pkg/storage/pgsql/user.go diff --git a/pkg/repository/pgsql/user_test.go b/pkg/storage/pgsql/user_test.go similarity index 100% rename from pkg/repository/pgsql/user_test.go rename to pkg/storage/pgsql/user_test.go diff --git a/pkg/repository/pgsql/vcard.go b/pkg/storage/pgsql/vcard.go similarity index 100% rename from pkg/repository/pgsql/vcard.go rename to pkg/storage/pgsql/vcard.go diff --git a/pkg/repository/pgsql/vcard_test.go b/pkg/storage/pgsql/vcard_test.go similarity index 100% rename from pkg/repository/pgsql/vcard_test.go rename to pkg/storage/pgsql/vcard_test.go diff --git a/pkg/repository/blocklist.go b/pkg/storage/repository/blocklist.go similarity index 100% rename from pkg/repository/blocklist.go rename to pkg/storage/repository/blocklist.go diff --git a/pkg/repository/capabilities.go b/pkg/storage/repository/capabilities.go similarity index 100% rename from pkg/repository/capabilities.go rename to pkg/storage/repository/capabilities.go diff --git a/pkg/repository/last.go b/pkg/storage/repository/last.go similarity index 100% rename from pkg/repository/last.go rename to pkg/storage/repository/last.go diff --git a/pkg/repository/offline.go b/pkg/storage/repository/offline.go similarity index 100% rename from pkg/repository/offline.go rename to pkg/storage/repository/offline.go diff --git a/pkg/repository/private.go b/pkg/storage/repository/private.go similarity index 100% rename from pkg/repository/private.go rename to pkg/storage/repository/private.go diff --git a/pkg/repository/repository.go b/pkg/storage/repository/repository.go similarity index 100% rename from pkg/repository/repository.go rename to pkg/storage/repository/repository.go diff --git a/pkg/repository/roster.go b/pkg/storage/repository/roster.go similarity index 100% rename from pkg/repository/roster.go rename to pkg/storage/repository/roster.go diff --git a/pkg/repository/user.go b/pkg/storage/repository/user.go similarity index 100% rename from pkg/repository/user.go rename to pkg/storage/repository/user.go diff --git a/pkg/repository/vcard.go b/pkg/storage/repository/vcard.go similarity index 100% rename from pkg/repository/vcard.go rename to pkg/storage/repository/vcard.go diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go new file mode 100644 index 000000000..87a0a4e5a --- /dev/null +++ b/pkg/storage/storage.go @@ -0,0 +1,41 @@ +// Copyright 2021 The jackal Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import ( + "fmt" + + measuredrepository "github.com/ortuman/jackal/pkg/storage/measured" + pgsqlrepository "github.com/ortuman/jackal/pkg/storage/pgsql" + + "github.com/ortuman/jackal/pkg/storage/repository" +) + +const pgSQLRepositoryType = "pgsql" + +// Config contains generic storage configuration. +type Config struct { + Type string `fig:"type" default:"pgsql"` + PgSQL pgsqlrepository.Config `fig:"pgsql"` +} + +// New returns an initialized repository.Repository derived from cfg configuration. +func New(cfg Config) (repository.Repository, error) { + if cfg.Type != pgSQLRepositoryType { + return nil, fmt.Errorf("storage: unrecognized repository type: %s", cfg.Type) + } + rep := pgsqlrepository.New(cfg.PgSQL) + return measuredrepository.New(rep), nil +} diff --git a/scripts/compile.sh b/scripts/compile.sh index 236a81029..5a4ce5614 100644 --- a/scripts/compile.sh +++ b/scripts/compile.sh @@ -4,4 +4,4 @@ set -eufo pipefail command -v go >/dev/null 2>&1 || { echo 'Please install go or use image that has it'; exit 1; } CGO_ENABLED=0 go build -a -tags netgo \ - -ldflags "-s -w" -o "jackal" "github.com/ortuman/jackal/pkg/cmd/jackal" + -ldflags "-s -w" -o "jackal" "github.com/ortuman/jackal/cmd/jackal" diff --git a/scripts/dockerimage.sh b/scripts/dockerimage.sh index 247c5b368..2dfe6f4f6 100644 --- a/scripts/dockerimage.sh +++ b/scripts/dockerimage.sh @@ -8,7 +8,7 @@ rm -rf build/ mkdir build CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo \ - -ldflags "-s -w" -o "build/jackal" "github.com/ortuman/jackal/pkg/cmd/jackal" + -ldflags "-s -w" -o "build/jackal" "github.com/ortuman/jackal/cmd/jackal" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo \ - -ldflags "-s -w" -o "build/jackalctl" "github.com/ortuman/jackal/pkg/cmd/jackalctl" + -ldflags "-s -w" -o "build/jackalctl" "github.com/ortuman/jackal/cmd/jackalctl" docker build -f dockerfiles/Dockerfile -t ortuman/jackal . diff --git a/scripts/install.sh b/scripts/install.sh index 23da0282d..8c289b29e 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -3,4 +3,4 @@ set -eufo pipefail command -v go >/dev/null 2>&1 || { echo 'Please install go or use image that has it'; exit 1; } -CGO_ENABLED=0 go install -ldflags "-s -w" "github.com/ortuman/jackal/pkg/cmd/jackal" +CGO_ENABLED=0 go install -ldflags "-s -w" "github.com/ortuman/jackal/cmd/jackal" diff --git a/scripts/installctl.sh b/scripts/installctl.sh index d06991d14..fa8770ffb 100644 --- a/scripts/installctl.sh +++ b/scripts/installctl.sh @@ -3,4 +3,4 @@ set -eufo pipefail command -v go >/dev/null 2>&1 || { echo 'Please install go or use image that has it'; exit 1; } -CGO_ENABLED=0 go install -ldflags "-s -w" "github.com/ortuman/jackal/pkg/cmd/jackalctl" +CGO_ENABLED=0 go install -ldflags "-s -w" "github.com/ortuman/jackal/cmd/jackalctl"