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, ¤tActorConfig) 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 ¶
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 ¶
var ErrConflict = errors.New(http.StatusConflict, "There was a conflict between versions of the object being saved")
var ErrNotInConfigContext = errors.New(http.StatusInternalServerError, "That context was not run through the ori/config middleware")
Functions ¶
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 ¶
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 (*Config) UnmarshalJSON ¶
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.