-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
40 lines (33 loc) · 1.3 KB
/
Copy pathmain.go
File metadata and controls
40 lines (33 loc) · 1.3 KB
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
// Example config-loading shows building a luart.Config from external sources via
// the luartconfig subpackage: a base JSON string overlaid by environment
// variables, using the precedence resolver (env > base > defaults). The file
// loaders (Load/LoadJSON/LoadYAML) work the same way against a path.
package main
import (
"fmt"
"log"
"os"
luart "github.com/htcom-code/go-lua-perf"
"github.com/htcom-code/go-lua-perf/luartconfig"
)
// resolveDemo loads a base JSON config, overrides one field via an env var, and
// returns the resolved Config. Precedence is env > json > defaults: the env var
// overrides only the field it sets, leaving the rest to the JSON base.
func resolveDemo() (luart.Config, error) {
const baseJSON = `{"maxStates": 8, "idleTTL": "5m"}`
os.Setenv("LUART_MAX_STATES", "16") // simulate a deployment override
defer os.Unsetenv("LUART_MAX_STATES")
return luartconfig.ResolveJSONString(baseJSON, "LUART_")
}
func main() {
cfg, err := resolveDemo()
if err != nil {
log.Fatal(err)
}
fmt.Printf("MaxStates (env override): %d\n", cfg.MaxStates) // 16
fmt.Printf("IdleTTL (from JSON): %s\n", cfg.IdleTTL) // 5m0s
// The resolved Config plugs straight into luart.New.
rt := luart.New(luart.NewMapLoader(), cfg)
defer rt.Close()
fmt.Println("config applied to a new Runtime")
}