Implement config singleton (#1268)
* Implement config singleton * Pass token in grpc request headers * Refactor BearerScheme * Fix typo
This commit is contained in:
		| @@ -18,6 +18,7 @@ import ( | |||||||
| 	"github.com/micro/go-micro/v2/errors" | 	"github.com/micro/go-micro/v2/errors" | ||||||
| 	"github.com/micro/go-micro/v2/metadata" | 	"github.com/micro/go-micro/v2/metadata" | ||||||
| 	"github.com/micro/go-micro/v2/registry" | 	"github.com/micro/go-micro/v2/registry" | ||||||
|  | 	"github.com/micro/go-micro/v2/util/config" | ||||||
|  |  | ||||||
| 	"google.golang.org/grpc" | 	"google.golang.org/grpc" | ||||||
| 	"google.golang.org/grpc/credentials" | 	"google.golang.org/grpc/credentials" | ||||||
| @@ -25,6 +26,10 @@ import ( | |||||||
| 	gmetadata "google.golang.org/grpc/metadata" | 	gmetadata "google.golang.org/grpc/metadata" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	BearerScheme = "Bearer " | ||||||
|  | ) | ||||||
|  |  | ||||||
| type grpcClient struct { | type grpcClient struct { | ||||||
| 	opts client.Options | 	opts client.Options | ||||||
| 	pool *pool | 	pool *pool | ||||||
| @@ -128,6 +133,10 @@ func (g *grpcClient) call(ctx context.Context, node *registry.Node, req client.R | |||||||
| 	header["timeout"] = fmt.Sprintf("%d", opts.RequestTimeout) | 	header["timeout"] = fmt.Sprintf("%d", opts.RequestTimeout) | ||||||
| 	// set the content type for the request | 	// set the content type for the request | ||||||
| 	header["x-content-type"] = req.ContentType() | 	header["x-content-type"] = req.ContentType() | ||||||
|  | 	// set the authorization token if one is saved locally | ||||||
|  | 	if token, err := config.Get("token"); err == nil && len(token) > 0 { | ||||||
|  | 		header["authorization"] = BearerScheme + token | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	md := gmetadata.New(header) | 	md := gmetadata.New(header) | ||||||
| 	ctx = gmetadata.NewOutgoingContext(ctx, md) | 	ctx = gmetadata.NewOutgoingContext(ctx, md) | ||||||
|   | |||||||
| @@ -9,39 +9,20 @@ import ( | |||||||
|  |  | ||||||
| 	conf "github.com/micro/go-micro/v2/config" | 	conf "github.com/micro/go-micro/v2/config" | ||||||
| 	"github.com/micro/go-micro/v2/config/source/file" | 	"github.com/micro/go-micro/v2/config/source/file" | ||||||
|  | 	"github.com/micro/go-micro/v2/util/log" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // FileName for global micro config | // FileName for global micro config | ||||||
| const FileName = ".micro" | const FileName = ".micro" | ||||||
|  |  | ||||||
|  | // config is a singleton which is required to ensure | ||||||
|  | // each function call doesn't load the .micro file | ||||||
|  | // from disk | ||||||
|  | var config = newConfig() | ||||||
|  |  | ||||||
| // Get a value from the .micro file | // Get a value from the .micro file | ||||||
| func Get(key string) (string, error) { | func Get(key string) (string, error) { | ||||||
| 	// get the filepath | 	tk := config.Get(key).String("") | ||||||
| 	fp, err := filePath() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// create a new config |  | ||||||
| 	c, err := conf.NewConfig( |  | ||||||
| 		conf.WithSource( |  | ||||||
| 			file.NewSource( |  | ||||||
| 				file.WithPath(fp), |  | ||||||
| 			), |  | ||||||
| 		), |  | ||||||
| 	) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// load the config |  | ||||||
| 	if err := c.Load(); err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// set a value |  | ||||||
| 	tk := c.Get(key).String("") |  | ||||||
|  |  | ||||||
| 	return strings.TrimSpace(tk), nil | 	return strings.TrimSpace(tk), nil | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -53,11 +34,36 @@ func Set(key, value string) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// set the value | ||||||
|  | 	config.Set(value, key) | ||||||
|  |  | ||||||
|  | 	// write to the file | ||||||
|  | 	return ioutil.WriteFile(fp, config.Bytes(), 0644) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func filePath() (string, error) { | ||||||
|  | 	usr, err := user.Current() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 	return filepath.Join(usr.HomeDir, FileName), nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // newConfig returns a loaded config | ||||||
|  | func newConfig() conf.Config { | ||||||
|  | 	// get the filepath | ||||||
|  | 	fp, err := filePath() | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Error(err) | ||||||
|  | 		return conf.DefaultConfig | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// write the file if it does not exist | 	// write the file if it does not exist | ||||||
| 	if _, err := os.Stat(fp); os.IsNotExist(err) { | 	if _, err := os.Stat(fp); os.IsNotExist(err) { | ||||||
| 		ioutil.WriteFile(fp, []byte{}, 0644) | 		ioutil.WriteFile(fp, []byte{}, 0644) | ||||||
| 	} else if err != nil { | 	} else if err != nil { | ||||||
| 		return err | 		log.Error(err) | ||||||
|  | 		return conf.DefaultConfig | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// create a new config | 	// create a new config | ||||||
| @@ -69,25 +75,16 @@ func Set(key, value string) error { | |||||||
| 		), | 		), | ||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		log.Error(err) | ||||||
|  | 		return conf.DefaultConfig | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// load the config | 	// load the config | ||||||
| 	if err := c.Load(); err != nil { | 	if err := c.Load(); err != nil { | ||||||
| 		return err | 		log.Error(err) | ||||||
|  | 		return conf.DefaultConfig | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// set a value | 	// return the conf | ||||||
| 	c.Set(value, key) | 	return c | ||||||
|  |  | ||||||
| 	// write the file |  | ||||||
| 	return ioutil.WriteFile(fp, c.Bytes(), 0644) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func filePath() (string, error) { |  | ||||||
| 	usr, err := user.Current() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
| 	return filepath.Join(usr.HomeDir, FileName), nil |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user