services

package
v0.0.0-...-ef1d3fc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2024 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const Biller = "biller"
View Source
const Captcha = "captcha"
View Source
const Config = "config"
View Source
const Data = "data"
View Source
const I18n = "i18n"
View Source
const IPStrategy = "ipStrategy"
View Source
const Limiter = "limiter"
View Source
const Logger = "logger"
View Source
const Messenger = "messenger"
View Source
const Redis = "redis"
View Source
const Resolver = "resolver"
View Source
const Scheduler = "scheduler"
View Source
const Ses = "ses"

Variables

View Source
var BillerDef = dingo.Def{
	Name:  Biller,
	Scope: di.App,
	Build: func(cfg *config.Config, log *slog.Logger, data *data.Client) (billing.Biller, error) {
		return billing.NewBiller(
			cfg,
			log,
			data,
		), nil
	},
}
View Source
var CaptchaDef = dingo.Def{
	Name:  Captcha,
	Scope: di.App,
	Build: func(cfg *config.Config) (auth.Captcha, error) {
		return auth.NewCaptchaHandler(cfg.HcaptchaSecret), nil
	},
}
View Source
var ConfigDef = dingo.Def{
	Name: Config,
	Build: func() (*config.Config, error) {
		cfg := &config.Config{}

		if err := env.Parse(cfg); err != nil {
			return nil, err
		}

		return cfg, nil
	},
}
View Source
var DataDef = dingo.Def{
	Name: Data,
	Build: func(cfg *config.Config) (*data.Client, error) {
		query := url.Values{
			"sslmode": {cfg.CockroachDBTLSMode},
		}

		if cfg.CockroachDBTLSMode != "disable" {
			query.Add("sslrootcert", cfg.CockroachDBTLSCA)
		}

		url := url.URL{
			Scheme: "postgresql",
			User: url.UserPassword(
				cfg.CockroachDBUser,
				cfg.CockroachDBPassword,
			),
			Host: fmt.Sprintf("%s:%s",
				cfg.CockroachDBHost,
				cfg.CockroachDBPort,
			),
			Path:     cfg.CockroachDBDatabase,
			RawQuery: query.Encode(),
		}

		client, err := data.Open(dialect.Postgres, url.String())

		if err != nil {
			return nil, err
		}

		return client, nil
	},
	Close: func(d *data.Client) error {
		return d.Close()
	},
}
View Source
var I18nDef = dingo.Def{
	Name:  I18n,
	Scope: di.App,
	Build: func() (*i.Bundle, error) {
		bundle := i.NewBundle(language.Catalan)
		bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)

		files, err := i18n.FS.ReadDir(".")

		if err != nil {
			return nil, err
		}

		for _, file := range files {
			bundle.LoadMessageFileFS(i18n.FS, file.Name())
		}

		return bundle, nil
	},
}
View Source
var IPStrategyDef = dingo.Def{
	Name: IPStrategy,
	Build: func(cfg *config.Config) (realclientip.Strategy, error) {
		// Parse trusted ranges from configuration
		var ranges []net.IPNet

		for _, str := range cfg.HttpTrustedProxies {
			_, addr, err := net.ParseCIDR(str)

			if err != nil {
				return nil, err
			}

			ranges = append(ranges, *addr)
		}

		strategy, err := realclientip.NewRightmostTrustedRangeStrategy("x-forwarded-for", ranges)

		if err != nil {
			return nil, err
		}

		return strategy, nil
	},
}
View Source
var LimiterDef = dingo.Def{
	Name:  Limiter,
	Scope: di.App,
	Build: func(cfg *config.Config, redis *libredis.Client) (*redis_rate.Limiter, error) {
		limiter := redis_rate.NewLimiter(redis)

		return limiter, nil
	},
}
View Source
var LoggerDef = dingo.Def{
	Name: Logger,
	Build: func(cfg *config.Config) (*slog.Logger, error) {
		level := slog.LevelInfo

		if cfg.Debug {
			level = slog.LevelDebug
		}

		log := slog.New(
			slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
				Level: level,
			}),
		)

		return log, nil
	},
}
View Source
var MessengerDef = dingo.Def{
	Name:  Messenger,
	Scope: di.App,
	Build: func(cfg *config.Config, ses *ses.SES, i18n *i.Bundle) (messaging.Messenger, error) {
		return messaging.NewMessenger(
			cfg,
			ses,
			i18n,
		), nil
	},
}
View Source
var RedisDef = dingo.Def{
	Name:  Redis,
	Scope: di.App,
	Build: func(cfg *config.Config) (*redis.Client, error) {
		return redis.NewClient(&redis.Options{
			Addr:     cfg.RedisAddress,
			Password: cfg.RedisPassword,
			DB:       cfg.RedisDatabase,
		}), nil
	},
}
View Source
var ResolverDef = dingo.Def{
	Name:  Resolver,
	Scope: di.App,
	Build: func(
		biller billing.Biller,
		captcha auth.Captcha,
		cfg *config.Config,
		data *data.Client,
		limiter *redis_rate.Limiter,
		messenger messaging.Messenger,
	) (*resolvers.Resolver, error) {
		return resolvers.NewResolver(
			biller,
			captcha,
			cfg,
			data,
			limiter,
			messenger,
		), nil
	},
}
View Source
var SchedulerDef = dingo.Def{
	Name:  Scheduler,
	Scope: di.App,
	Build: func() (*tasks.Scheduler, error) {
		return tasks.New(), nil
	},
	Close: func(s *tasks.Scheduler) error {
		s.Stop()
		return nil
	},
}
View Source
var SesDef = dingo.Def{
	Name:  Ses,
	Scope: di.App,
	Build: func(cfg *config.Config) (*ses.SES, error) {
		session, err := session.NewSession(&aws.Config{
			Region: aws.String(cfg.AwsRegion),
			Credentials: credentials.NewStaticCredentials(
				cfg.AwsKeyId,
				cfg.AwsKeySecret,
				"",
			),
		})

		if err != nil {
			return nil, err
		}

		return ses.New(session), nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL