forked from cwtch.im/cwtch
remove config sample; now generate config and populate defaults; add config to control logging
This commit is contained in:
parent
8a1fb3ccc2
commit
addd5a7b00
|
@ -2,28 +2,14 @@ package main
|
|||
|
||||
import (
|
||||
cwtchserver "cwtch.im/cwtch/server"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
serverConfigFile = "serverConfig.json"
|
||||
)
|
||||
|
||||
func confirmOrCopySampleConfig() {
|
||||
// if no config file, attempt to copy sample
|
||||
if _, err := os.Stat(serverConfigFile); os.IsNotExist(err) {
|
||||
raw, err := ioutil.ReadFile(serverConfigFile + ".sample")
|
||||
if err != nil {
|
||||
log.Fatal("Could not read sample config to copy: ", err)
|
||||
}
|
||||
ioutil.WriteFile(serverConfigFile, raw, 0600)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
confirmOrCopySampleConfig()
|
||||
serverConfig := cwtchserver.LoadConfig(serverConfigFile)
|
||||
|
||||
server := new(cwtchserver.Server)
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"maxBufferLines": 100000,
|
||||
"serverReporting": {
|
||||
"reportingGroupId": "",
|
||||
"reportingServerAddr": ""
|
||||
}
|
||||
}
|
|
@ -23,10 +23,12 @@ type Monitors struct {
|
|||
ClientConns MonitorHistory
|
||||
starttime time.Time
|
||||
breakChannel chan bool
|
||||
log bool
|
||||
}
|
||||
|
||||
// Start initializes a Monitors's monitors
|
||||
func (mp *Monitors) Start(ra *application.RicochetApplication) {
|
||||
func (mp *Monitors) Start(ra *application.RicochetApplication, log bool) {
|
||||
mp.log = log
|
||||
mp.starttime = time.Now()
|
||||
mp.breakChannel = make(chan bool)
|
||||
mp.MessageCounter = NewCounter()
|
||||
|
@ -35,19 +37,15 @@ func (mp *Monitors) Start(ra *application.RicochetApplication) {
|
|||
mp.Memory = NewMonitorHistory(MegaBytes, func() float64 { sysInfo, _ := pidusage.GetStat(os.Getpid()); return float64(sysInfo.Memory) })
|
||||
mp.ClientConns = NewMonitorHistory(Count, func() float64 { return float64(ra.ConnectionCount()) })
|
||||
|
||||
// Todo: replace with proper reporting
|
||||
go mp.log()
|
||||
if mp.log {
|
||||
go mp.run()
|
||||
}
|
||||
}
|
||||
|
||||
func (mp *Monitors) log() {
|
||||
func (mp *Monitors) run() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(time.Minute):
|
||||
messageMinutes := mp.Messages.Minutes()
|
||||
cpuMinutes := mp.CPU.Minutes()
|
||||
memoryMinutes := mp.Memory.Minutes()
|
||||
listenConnsMinutes := mp.ClientConns.Minutes()
|
||||
log.Printf("METRICS: Messages: %.0f ClientConns: %.0f CPU: %.2f Memory: %.0fMBs\n", messageMinutes[0], listenConnsMinutes[0], cpuMinutes[0], memoryMinutes[0]/1024/1024)
|
||||
mp.report()
|
||||
case <-mp.breakChannel:
|
||||
return
|
||||
|
@ -80,12 +78,13 @@ func (mp *Monitors) report() {
|
|||
mp.Memory.Report(w)
|
||||
|
||||
w.Flush()
|
||||
|
||||
}
|
||||
|
||||
// Stop stops all the monitors in a Monitors
|
||||
func (mp *Monitors) Stop() {
|
||||
mp.breakChannel <- true
|
||||
if mp.log {
|
||||
mp.breakChannel <- true
|
||||
}
|
||||
mp.Messages.Stop()
|
||||
mp.CPU.Stop()
|
||||
mp.Memory.Stop()
|
||||
|
|
|
@ -23,7 +23,7 @@ type Server struct {
|
|||
func (s *Server) Run(serverConfig *Config) {
|
||||
s.config = serverConfig
|
||||
cwtchserver := new(application.RicochetApplication)
|
||||
s.metricsPack.Start(cwtchserver)
|
||||
s.metricsPack.Start(cwtchserver, s.config.ServerReporting.LogMetricsToFile)
|
||||
|
||||
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", s.config.PrivateKey(), 9878)
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
// Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
|
||||
type Reporting struct {
|
||||
LogMetricsToFile bool `json:"logMetricsToFile"`
|
||||
PeerPrivateKey string `json:"privateKey"`
|
||||
ReportingGroupID string `json:"reportingGroupId"`
|
||||
ReportingServerAddr string `json:"reportingServerAddr"`
|
||||
|
@ -41,32 +42,45 @@ func (config *Config) Save(filename string) {
|
|||
ioutil.WriteFile(filename, bytes, 0600)
|
||||
}
|
||||
|
||||
// LoadConfig loads a Config from a json file specified by filename
|
||||
func LoadConfig(filename string) *Config {
|
||||
raw, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Fatal("Could not read config: ", err)
|
||||
}
|
||||
|
||||
// newConfig generates a simple config with defaults. Unmarshal will return them if they aren't specified
|
||||
func newConfig() *Config {
|
||||
config := Config{}
|
||||
err = json.Unmarshal(raw, &config)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Error reading config: ", err)
|
||||
}
|
||||
|
||||
if config.PrivateKeyBytes == "" {
|
||||
config.generatePrivateKey()
|
||||
config.Save(filename)
|
||||
}
|
||||
if config.ServerReporting.PeerPrivateKey == "" {
|
||||
config.generatePeerPrivateKey()
|
||||
config.Save(filename)
|
||||
}
|
||||
config.MaxBufferLines = 100000
|
||||
config.ServerReporting.LogMetricsToFile = false
|
||||
|
||||
return &config
|
||||
}
|
||||
|
||||
// LoadConfig loads a Config from a json file specified by filename
|
||||
func LoadConfig(filename string) *Config {
|
||||
config := newConfig()
|
||||
|
||||
raw, err := ioutil.ReadFile(filename)
|
||||
if err == nil {
|
||||
err = json.Unmarshal(raw, &config)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Error reading config: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
configAutoPopulate(config)
|
||||
// Always save (first time generation, new version with new variables populated)
|
||||
config.Save(filename)
|
||||
return config
|
||||
}
|
||||
|
||||
// Auto populate required values if missing and save
|
||||
func configAutoPopulate(config *Config) {
|
||||
if config.PrivateKeyBytes == "" {
|
||||
config.generatePrivateKey()
|
||||
}
|
||||
|
||||
if config.ServerReporting.PeerPrivateKey == "" {
|
||||
config.generatePeerPrivateKey()
|
||||
}
|
||||
}
|
||||
|
||||
func (config *Config) generatePrivateKey() {
|
||||
pk, err := utils.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue