-
Notifications
You must be signed in to change notification settings - Fork 16
/
config_service.go
63 lines (50 loc) · 1.58 KB
/
config_service.go
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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package config
import (
"github.com/mitchellh/mapstructure"
"github.com/pb33f/ranch/bus"
"github.com/pb33f/ranch/model"
"github.com/pb33f/ranch/service"
"github.com/pb33f/wiretap/controls"
"github.com/pb33f/wiretap/shared"
)
const (
ConfigurationServiceChan = "configuration"
GetConfigurationRequest = "config-request"
)
type ConfigurationService struct {
configStore bus.BusStore
}
type RequestConfiguration struct {
}
type Configuration struct {
Configuration *shared.WiretapConfiguration `json:"configuration,omitempty"`
}
func NewConfigurationService() *ConfigurationService {
storeManager := bus.GetBus().GetStoreManager()
controlsStore := storeManager.CreateStore(controls.ControlServiceChan)
return &ConfigurationService{
configStore: controlsStore,
}
}
func (cs *ConfigurationService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore) {
switch request.RequestCommand {
case GetConfigurationRequest:
cs.returnConfig(request, core)
default:
core.HandleUnknownRequest(request)
}
}
func (cs *ConfigurationService) returnConfig(request *model.Request, core service.FabricServiceCore) {
if dl, ok := request.Payload.(map[string]interface{}); ok {
// decode the object into a request
var r RequestConfiguration
_ = mapstructure.Decode(dl, &r)
// extract state from store.
storeData, _ := cs.configStore.Get(shared.ConfigKey)
core.SendResponse(request, storeData)
} else {
core.SendErrorResponse(request, 400, "Invalid config request")
}
}