aboutsummaryrefslogtreecommitdiff
path: root/src/serverConfig.ts
blob: 5d2575a83b0878f4ebd56055a062ace0336a614f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { default as types } from './matcher/types';

type Value = string | number | boolean;
type ValueString = 'string' | 'number' | 'integer' | 'boolean';

const serverConfig: Property[] = [];

/**
 * A way to display user-configurable properties.
 */
interface PropertyDisplay {
	/**
	 * The category the property belongs to.
	 */
	category: string;

	/**
	 * The group the property belongs to.
	 */
	group: string;

	/**
	 * The name for the property to display to a user.
	 */
	displayName: string;

	/**
	 * The description of the property.
	 */
	description: string;
}

/**
 * A server property that infuences the running of the server.
 * Loaded from configs at launch, and possibly editable during run-time.
 */
export class Property {
	/**
	 * The property's unique ID.
	 */
	public id: string;

	/**
	 * How to display the property to the user.
	 */
	public display?: PropertyDisplay;

	/**
	 * Is the property read-only, or can it be edited at run-time?
	 */
	public readOnly: boolean;

	/**
	 * The variable type of the property.
	 *
	 * These strings represent Javascript types directly,
	 * except `"integer"`, which is a `number` in logic.
	 */
	public type: ValueString;

	/**
	 * The value of the property.
	 */
	private valueInternal!: Value;

	public constructor(
		id: string,
		type: ValueString,
		value: Value,
		readOnly: boolean = true,
		display?: PropertyDisplay
	) {
		this.id = id;
		this.display = display;
		this.readOnly = readOnly;
		this.type = type;
		this.valueInternal = value;
	}

	public get value(): Value {
		return this.valueInternal;
	}

	public set value(value: Value) {
		if (this.readOnly) {
			throw new Error('this property is read-only');
		} else if (!types[this.type](value)) {
			throw new Error(`value ${value} is not of type ${this.type}`);
		}
		this.valueInternal = value;
	}
}

// Logging
let propertyDisplay: PropertyDisplay = {
	category: 'logging',
	group: 'BE4',
	displayName: 'Is logging enabled?',
	description:
		'This property defines if logging is enabled on this backend server.'
};

serverConfig.push(new Property(
	'Server_Log',
	'boolean',
	false,
	false,
	propertyDisplay
));

// Caching Settings (global)
serverConfig.push(new Property(
	'Global_Cache_Limit',
	'integer',
	1,
	true
));

// Caching Settings (project level)
propertyDisplay = {
	category: 'Caching',
	group: 'BE4',
	displayName: 'Caching Type',
	description:
		'This property defines the current caching style for this project.'
};

serverConfig.push(new Property(
	'project_example',
	'integer',
	1,
	false,
	propertyDisplay
));

export default serverConfig;