Skip to content

Commit

Permalink
Merge pull request #512 from akutz/feature/path-refactor
Browse files Browse the repository at this point in the history
Paths Refactor
  • Loading branch information
akutz authored Apr 23, 2017
2 parents 111ef6f + 6cfd1f4 commit 8570256
Show file tree
Hide file tree
Showing 37 changed files with 1,263 additions and 731 deletions.
34 changes: 33 additions & 1 deletion api/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type lsc struct {
req *http.Request
right context.Context
logger *log.Logger
pathConfig *types.PathConfig
loggerInherited bool
}

Expand Down Expand Up @@ -48,6 +49,17 @@ func newContext(
if logger == nil {
logger = log.StandardLogger()
}
if types.Debug {
logger.Level = log.DebugLevel
}

// forward the pathConfig reference
var pathConfig *types.PathConfig
if key == PathConfigKey {
pathConfig = val.(*types.PathConfig)
} else if ctx, ok := parent.(*lsc); ok {
pathConfig = ctx.pathConfig
}

return &lsc{
Context: parent,
Expand All @@ -56,6 +68,7 @@ func newContext(
req: req,
right: right,
logger: logger,
pathConfig: pathConfig,
loggerInherited: true,
}
}
Expand Down Expand Up @@ -166,10 +179,14 @@ func Value(ctx context.Context, key interface{}) interface{} {

func (ctx *lsc) Value(key interface{}) interface{} {

if key == LoggerKey {
if key == LoggerKey && ctx.logger != nil {
return ctx.logger
}

if key == PathConfigKey && ctx.pathConfig != nil {
return ctx.pathConfig
}

if customKeyID, ok := isCustomKey(key); ok {
key = customKeyID
}
Expand Down Expand Up @@ -293,6 +310,21 @@ func MustSession(ctx context.Context) interface{} {
return v
}

// PathConfig returns the context's path config. This value is valid on
// both the client and the server.
func PathConfig(ctx context.Context) (*types.PathConfig, bool) {
if v, ok := ctx.Value(PathConfigKey).(*types.PathConfig); ok {
return v, ok
}
return nil, false
}

// MustPathConfig returns the context's path config and panics if it does
// not exist and/or cannot be type cast.
func MustPathConfig(ctx context.Context) *types.PathConfig {
return ctx.Value(PathConfigKey).(*types.PathConfig)
}

// AuthToken returns the context's security token. This value is valid on
// both the client and the server.
func AuthToken(ctx context.Context) (*types.AuthToken, bool) {
Expand Down
3 changes: 3 additions & 0 deletions api/context/context_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type Key int
const (
_ Key = -1 - iota

// PathConfigKey is a context key.
PathConfigKey

// LoggerKey is a context key.
LoggerKey

Expand Down
33 changes: 33 additions & 0 deletions api/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"sync"

gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"

"github.com/codedellemc/libstorage/api/types"
Expand All @@ -27,10 +28,25 @@ var (
intDriverCtors = map[string]types.NewIntegrationDriver{}
intDriverCtorsRWL = &sync.RWMutex{}

cfgRegs = []*cregW{}
cfgRegsRWL = &sync.RWMutex{}

routers = []types.Router{}
routersRWL = &sync.RWMutex{}
)

type cregW struct {
name string
creg types.NewConfigReg
}

// RegisterConfigReg registers a new configuration registration request.
func RegisterConfigReg(name string, f types.NewConfigReg) {
cfgRegsRWL.Lock()
defer cfgRegsRWL.Unlock()
cfgRegs = append(cfgRegs, &cregW{name, f})
}

// RegisterRouter registers a Router.
func RegisterRouter(router types.Router) {
routersRWL.Lock()
Expand Down Expand Up @@ -180,6 +196,23 @@ func NewIntegrationDriver(name string) (types.IntegrationDriver, error) {
return NewIntegrationDriverManager(ctor()), nil
}

// ConfigRegs returns a channel on which all registered configuration
// registrations are returned.
func ConfigRegs(ctx types.Context) <-chan gofig.ConfigRegistration {
c := make(chan gofig.ConfigRegistration)
go func() {
cfgRegsRWL.RLock()
defer cfgRegsRWL.RUnlock()
for _, cr := range cfgRegs {
r := NewConfigReg(cr.name)
cr.creg(ctx, r)
c <- r
}
close(c)
}()
return c
}

// StorageExecutors returns a channel on which new instances of all registered
// storage executors can be received.
func StorageExecutors() <-chan types.StorageExecutor {
Expand Down
3 changes: 3 additions & 0 deletions api/registry/registry_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ import (

// NewConfig is a function that returns a new Config object.
var NewConfig func() gofig.Config

// NewConfigReg is a function that returns a new ConfigRegistration object.
var NewConfigReg func(string) gofig.ConfigRegistration
9 changes: 9 additions & 0 deletions api/registry/registry_config_gofig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ package registry

import (
"github.com/akutz/gofig"
"github.com/codedellemc/libstorage/api/types"
)

func init() {
NewConfig = gofig.New
NewConfigReg = gofig.NewRegistration
}

// ProcessRegisteredConfigs processes the registered configuration requests.
func ProcessRegisteredConfigs(ctx types.Context) {
for r := range ConfigRegs(ctx) {
gofig.Register(r)
}
}
10 changes: 10 additions & 0 deletions api/registry/registry_config_nogofig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// +build !gofig

package registry

import "github.com/codedellemc/libstorage/api/types"

// ProcessRegisteredConfigs processes the registered configuration requests.
func ProcessRegisteredConfigs(ctx types.Context) {
// noop
}
17 changes: 15 additions & 2 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
glogrus "github.com/codedellemc/gournal/logrus"

"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/registry"
"github.com/codedellemc/libstorage/api/server/services"
"github.com/codedellemc/libstorage/api/types"
"github.com/codedellemc/libstorage/api/utils"
Expand Down Expand Up @@ -110,7 +111,7 @@ func newServer(goCtx gocontext.Context, config gofig.Config) (*server, error) {

if config == nil {
var err error
if config, err = apicnfg.NewConfig(); err != nil {
if config, err = apicnfg.NewConfig(ctx); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -198,7 +199,19 @@ func Serve(
goCtx gocontext.Context,
config gofig.Config) (types.Server, <-chan error, error) {

s, err := newServer(goCtx, config)
if goCtx == nil {
goCtx = context.Background()
}

ctx := context.New(goCtx)

if _, ok := context.PathConfig(ctx); !ok {
pathConfig := utils.NewPathConfig(ctx, "", "")
ctx = ctx.WithValue(context.PathConfigKey, pathConfig)
registry.ProcessRegisteredConfigs(ctx)
}

s, err := newServer(ctx, config)
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/server/server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *server) initEndpoints(ctx types.Context) error {

case types.UnixEndpoint:

laddr = fmt.Sprintf("unix://%s", utils.GetTempSockFile())
laddr = fmt.Sprintf("unix://%s", utils.GetTempSockFile(ctx))
s.ctx.WithField("endpoint", endpoint).Info(
"initializing auto unix endpoint")

Expand Down
35 changes: 23 additions & 12 deletions api/server/server_startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

"github.com/codedellemc/libstorage/api"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/server/services"
"github.com/codedellemc/libstorage/api/types"
)

var (
Expand Down Expand Up @@ -44,15 +44,20 @@ func (s *server) PrintServerStartupHeader(w io.Writer) {
}

var (
n int
v = api.Version
b = &bytes.Buffer{}
bar = strings.Repeat("#", 80)
barl = fmt.Sprintf("##%s##", strings.Repeat(" ", 76))
now = time.Now().UTC().Format(dateFormat)
vts = v.BuildTimestamp.Format(time.RFC1123)
n int
v = api.Version
b = &bytes.Buffer{}
bar = strings.Repeat("#", 80)
barl = fmt.Sprintf("##%s##", strings.Repeat(" ", 76))
now = time.Now().UTC().Format(dateFormat)
vts = v.BuildTimestamp.Format(time.RFC1123)
pathConfig = context.MustPathConfig(s.ctx)
)

if pathConfig == nil {
panic("pathConfig is nil")
}

fmt.Fprint(b, serverStartupLogo)
fmt.Fprintln(b, bar)
fmt.Fprintln(b, barl)
Expand Down Expand Up @@ -89,16 +94,22 @@ func (s *server) PrintServerStartupHeader(w io.Writer) {

fmt.Fprintln(b, barl)

n, _ = fmt.Fprintf(b, "## etc: %s", types.Etc)
n, _ = fmt.Fprintf(b, "## etc: %s", pathConfig.Etc)
fmt.Fprint(b, strings.Repeat(" ", trunc80(n)))
fmt.Fprintln(b, "##")
n, _ = fmt.Fprintf(b, "## tls: %s", pathConfig.TLS)
fmt.Fprint(b, strings.Repeat(" ", trunc80(n)))
fmt.Fprintln(b, "##")
n, _ = fmt.Fprintf(b, "## lib: %s", pathConfig.Lib)
fmt.Fprint(b, strings.Repeat(" ", trunc80(n)))
fmt.Fprintln(b, "##")
n, _ = fmt.Fprintf(b, "## lib: %s", types.Lib)
n, _ = fmt.Fprintf(b, "## log: %s", pathConfig.Log)
fmt.Fprint(b, strings.Repeat(" ", trunc80(n)))
fmt.Fprintln(b, "##")
n, _ = fmt.Fprintf(b, "## log: %s", types.Log)
n, _ = fmt.Fprintf(b, "## run: %s", pathConfig.Run)
fmt.Fprint(b, strings.Repeat(" ", trunc80(n)))
fmt.Fprintln(b, "##")
n, _ = fmt.Fprintf(b, "## run: %s", types.Run)
n, _ = fmt.Fprintf(b, "## lsx: %s", pathConfig.LSX)
fmt.Fprint(b, strings.Repeat(" ", trunc80(n)))
fmt.Fprintln(b, "##")

Expand Down
Loading

0 comments on commit 8570256

Please sign in to comment.