Difference between revisions of "Configuration"
(Created page with "Every service inside a SocialStack API can have its own automatically managed configuration. Assuming you have created a service (either an entity or just a basic...") |
|||
| Line 66: | Line 66: | ||
= Using configuration from javascript = | = Using configuration from javascript = | ||
If you mark config with the [Frontend] attribute, you'll be able to use it from your js. As always, be careful what you expose. Note that to essentially "register" that the config even exists, you do need to call GetConfig at least once. Typically a small service is made which has the job of loading/ providing your frontend config - it has the added side effect of other services in the API being able to access your config if they need it as well anyway. | If you mark config with the [Frontend] attribute, you'll be able to use it from your js. As always, be careful what you expose. Note that to essentially "register" that the config even exists, you do need to call GetConfig (inside a service, see above) at least once. Typically a small service is made which has the job of loading/ providing your frontend config - it has the added side effect of other services in the API being able to access your config if they need it as well anyway. | ||
Once your config is setup and ready, the useConfig js function is able to access it: | Once your config is setup and ready, the useConfig js function is able to access it: | ||
Latest revision as of 14:50, 13 September 2022
Every service inside a SocialStack API can have its own automatically managed configuration. Assuming you have created a service (either an entity or just a basic service) then you'll be able to start using configuration.
Defining a configuration class
Configuration classes must just inherit from Config and use properties which can be JSON serialised like so:
using Api.Configuration;
public class OAuthConfig : Config
{
public string Key {get; set;}
[Frontend]
public string PublicKey {get; set;}
}
The name of the class doesn't matter, but typically it is named after the service that uses it like UserService may have "UserConfig.cs".
The frontend attribute
Optionally add [Frontend] to individual properties or your whole class to note that the configured value should be sent to the frontend javascript. It can be used via useConfig (see below).
Setting config values
By calling GetConfig at least once (see below), Socialstack will first check if config exists in your database, specifically the configuration table. It will look for a config entry with a key that matches the name of your configuration class minus the word "Config" or "Configuration" if it ended with either:
- OAuthConfig -> will search for a key of "OAuth" in your configuration database table
- UserConfig -> will search for a key of "User" in your configuration database table
If it does not exist in the database, it will check if it exists in the appsettings file(s) by searching for the same key. If that doesn't exist either, a new database row will be created automatically.
The easiest way to edit your configuration is via the administration panel in the configuration section.
Using configuration from a service
Inside your service, you can obtain a config instance like so:
var myConfig = GetConfig<MyConfigClass>();
// myConfig is an instance of MyConfigClass. It is never null.
var key = myConfig.Key;
It's recommended to do this in your constructor and then keep a reference to the object as a private variable. The configuration system makes a guarantee that the object it gave you will always be kept up to date if an admin changes the config values. When this occurs, OnChange will be triggered on the object.
// var myConfig = GetConfig<OAuthConfig>();
myConfig.OnChange += () => {
// Calculate some complex/ derived configuration value here. Optionally can mark it async and await too.
// If not using async, just return a valuetask:
return new ValueTask();
};
Using configuration from javascript
If you mark config with the [Frontend] attribute, you'll be able to use it from your js. As always, be careful what you expose. Note that to essentially "register" that the config even exists, you do need to call GetConfig (inside a service, see above) at least once. Typically a small service is made which has the job of loading/ providing your frontend config - it has the added side effect of other services in the API being able to access your config if they need it as well anyway.
Once your config is setup and ready, the useConfig js function is able to access it:
import {useConfig} from 'UI/Session';
export default function MyComponent(props)
{
var frontendBitsOfTheConfig = useConfig("OAuth");
// frontendBitsOfTheConfig.publicKey is a thing, frontendBitsOfTheConfig.key is not
}