@@ -3,20 +3,17 @@ package app
33import (
44 "context"
55 "errors"
6- "flag"
76 "fmt"
87 syslog "log"
98 "os"
109 "os/signal"
1110 "os/user"
12- "path/filepath"
1311 "reflect"
1412 "regexp"
1513 "syscall"
1614 "time"
1715
1816 "github.com/bhmj/goblocks/appstatus"
19- "github.com/bhmj/goblocks/conftool"
2017 "github.com/bhmj/goblocks/gorillarouter"
2118 "github.com/bhmj/goblocks/httpserver"
2219 "github.com/bhmj/goblocks/log"
@@ -25,7 +22,6 @@ import (
2522 "github.com/bhmj/goblocks/statserver"
2623 "go.uber.org/automaxprocs/maxprocs"
2724 "golang.org/x/sync/errgroup"
28- "gopkg.in/yaml.v3"
2925)
3026
3127var (
@@ -163,97 +159,6 @@ func (a *application) Run(config any) {
163159 a .logger .Sync () //nolint:errcheck
164160}
165161
166- func (a * application ) readConfigStruct (config any ) {
167- pwd , err := os .Getwd ()
168- if err != nil {
169- syslog .Fatalf ("get current dir: %s" , err )
170- }
171- a .cfgPath = pwd
172-
173- err = a .applyConfigStruct (config )
174- if err != nil {
175- syslog .Fatalf ("read config data: %s" , err )
176- }
177- }
178-
179- func (a * application ) readConfigFile () {
180- pstr := flag .String ("config-file" , "" , "" )
181- flag .Parse ()
182- if pstr == nil || * pstr == "" {
183- syslog .Fatalf ("Usage: %s --config-file=/path/to/config.yaml" , os .Args [0 ])
184- }
185- a .cfgPath = filepath .Dir (* pstr )
186-
187- fullname , err := filepath .Abs (* pstr )
188- if err != nil {
189- syslog .Fatalf ("filepath: %s" , err )
190- }
191- raw , err := os .ReadFile (fullname )
192- if err != nil {
193- syslog .Fatalf ("read config: %s" , err )
194- }
195-
196- data := conftool .ParseEnvVars (raw )
197- err = a .readConfigData (data )
198- if err != nil {
199- syslog .Fatalf ("read config data: %s" , err )
200- }
201- }
202-
203- func (a * application ) readConfigData (data []byte ) error {
204- var root yaml.Node
205-
206- cfg := make (map [string ]any )
207-
208- for name , reg := range a .serviceDefs {
209- cfg [name ] = reg .Config
210- }
211-
212- if err := yaml .Unmarshal (data , & root ); err != nil {
213- return err
214- }
215-
216- if len (root .Content ) == 0 {
217- return errEmptyConfig
218- }
219-
220- mapping := root .Content [0 ] // top-level mapping
221- rootNodes := make (map [string ]* yaml.Node )
222- for i := 0 ; i < len (mapping .Content ); i += 2 {
223- key := mapping .Content [i ].Value
224- val := mapping .Content [i + 1 ]
225- rootNodes [key ] = val
226- }
227-
228- // decode app config
229- if node , ok := rootNodes ["app" ]; ok {
230- if err := node .Decode (a .cfg ); err != nil {
231- return fmt .Errorf ("app config: %w" , err )
232- }
233- }
234- if err := conftool .DefaultsAndRequired (a .cfg ); err != nil {
235- return fmt .Errorf ("app config: missing required value: %w" , err )
236- }
237-
238- // decode configs for all registered services
239- for name , service := range a .serviceDefs {
240- node , ok := rootNodes [name ]
241- if ! ok {
242- continue
243- }
244- if err := node .Decode (service .Config ); err != nil {
245- return fmt .Errorf ("decode %s: %w" , name , err )
246- }
247- }
248- for name , service := range a .serviceDefs {
249- if err := conftool .DefaultsAndRequired (service .Config ); err != nil {
250- return fmt .Errorf ("%s config: missing required value: %w" , name , err )
251- }
252- }
253-
254- return nil
255- }
256-
257162// Run starts the application
258163func (a * application ) runEverything (appReporter appstatus.ServiceStatusReporter ) {
259164 ctx , cancel := context .WithCancel (context .Background ())
0 commit comments