2018-10-08 23:15:54 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package statdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
)
|
|
|
|
|
2018-11-21 22:01:09 +00:00
|
|
|
//CtxKey Used as statdb key
|
|
|
|
type CtxKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxKeyStats CtxKey = iota
|
|
|
|
)
|
|
|
|
|
2018-10-08 23:15:54 +01:00
|
|
|
// Config is a configuration struct that is everything you need to start a
|
|
|
|
// StatDB responsibility
|
|
|
|
type Config struct {
|
|
|
|
DatabaseURL string `help:"the database connection string to use" default:"$CONFDIR/stats.db"`
|
|
|
|
DatabaseDriver string `help:"the database driver to use" default:"sqlite3"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the provider.Responsibility interface
|
|
|
|
func (c Config) Run(ctx context.Context, server *provider.Provider) error {
|
2018-12-04 18:47:58 +00:00
|
|
|
ns, err := NewStatDB(c.DatabaseDriver, c.DatabaseURL, zap.L())
|
2018-10-08 23:15:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:01:09 +00:00
|
|
|
return server.Run(context.WithValue(ctx, ctxKeyStats, ns))
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromContext loads an existing StatDB from the Provider context
|
|
|
|
// stack if one exists.
|
2018-12-04 18:47:58 +00:00
|
|
|
func LoadFromContext(ctx context.Context) *StatDB {
|
|
|
|
if v, ok := ctx.Value(ctxKeyStats).(*StatDB); ok {
|
2018-11-21 22:01:09 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-08 23:15:54 +01:00
|
|
|
}
|