config

package
v0.0.0-...-b405234 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2016 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

package config provides support for storing application-wide configuration parameters in the App Engine Datastore.

To use config, install its middleware in your Kami routes. Then you can retrieve configuration using config.Get.

You can think of config as an application-wide key-value store. You can store and retrieve any kind of struct in it that Datastore can serialize. Beware, though, that names will collide across structs. Consider the following code:

type AccountConfig struct {
	DefaultRoles []string
}

type ActorConfig struct {
	DefaultRoles []string
}

actorCfg := &ActorConfig{
	DefaultRoles: []string{"Edward I", "Macbeth"},
}

acctCfg := &AccountConfig{
	DefaultRoles: []string{"user", "viewer"},
}

config.Save(ctx, actorCfg)
config.Save(ctx, acctCfg)

In a later request, if somebody does the following,

type Actor struct {
	Name string
	Role []string
}

var currentActorConfig ActorConfig
config.Get(ctx, &currentActorConfig)

shakespeare := Actor{
	Name: "William Shakespeare",
	Roles: currentActorConfig.DefaultRoles,
}

They might be very suprised to find that Bill is set to play "user" and "viewer" rather than "Edward I" and "Macbeth."

Index

Constants

View Source
const Entity = "Config"

Entity is the string name for the Entity used to store the configuration in the App Engine Datastore. Think of it like a table name.

Variables

View Source
var ErrConflict = errors.New(http.StatusConflict, "There was a conflict between versions of the object being saved")
View Source
var ErrNotInConfigContext = errors.New(http.StatusInternalServerError, "That context was not run through the ori/config middleware")

Functions

func Get

func Get(ctx context.Context, conf interface{}) error

Get stores the application configuration in the variable pointed to by conf.

func Middleware

Middleware is a Kami middleware that retrieves application configuration and makes it available to be obtained by config.Get(ctx) in handlers. The simplest way to use it is just to include it at the top of your routes, like so:

kami.Use("/", config.Middleware)

func Save

func Save(ctx context.Context, conf interface{}) error

Save changes the application configuration to the values in conf. All HTTP requests subsequent to this one are guaranteed to use the new values in their configuration.

Note that subsequent calls to Get with the same request context will continue to retrieve the old version of the configuration.

As a special case, calling Save with a *config.Config will replace the entire contents of the configuration with the contents of Config.

Types

type Config

type Config datastore.PropertyList

Config is a type that can represent the full state of the application at any time. It's quite slow because it has to rely on reflection. Use config.Get to pull config into your own struct instead.

func (*Config) MarshalJSON

func (conf *Config) MarshalJSON() ([]byte, error)

func (*Config) UnmarshalJSON

func (conf *Config) UnmarshalJSON(data []byte) error

type Global

type Global struct {
	// AuthSecret is the secret key by which all JWTs are signed using a SHA-256 HMAC.
	AuthSecret string `json:",omitempty"`
	// ValidOriginSuffix is the suffix for which CORS requests are valid for this app.
	ValidOriginSuffix string `json:",omitempty"`
}

Global describes some configuration parameters that are required for the API to function.

Jump to

Keyboard shortcuts

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