refactoring

This commit is contained in:
crusader 2017-09-28 19:09:33 +09:00
parent 6e88452b1b
commit 0ece619ffb
13 changed files with 120 additions and 120 deletions

View File

@ -26,53 +26,47 @@ type AuthHandler interface {
} }
type authHandlers struct { type authHandlers struct {
c client.Client c client.Client
entryURL string entryURL string
configDir string
noAuthConfigPath string noAuthConfigPath string
noAuthConfig config.NoAuthProbeConfig noAuthConfig config.NoAuthProbeConfig
probeConfigPath string
probeConfig config.ProbeConfig
shutdownChan chan bool shutdownChan chan bool
acceptedChan chan bool acceptedChan chan bool
deniedChan chan error deniedChan chan error
} }
func New(configDir string) (AuthHandler, error) { func New() (AuthHandler, error) {
var err error var err error
h := &authHandlers{ h := &authHandlers{
configDir: configDir,
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
acceptedChan: make(chan bool), acceptedChan: make(chan bool),
deniedChan: make(chan error), deniedChan: make(chan error),
} }
if h.entryURL, err = opuu.Join(config.Config.Central.URL, noAuthEntryPoint); nil != err { if h.entryURL, err = opuu.Join(*config.CFG.Central.URL, noAuthEntryPoint); nil != err {
return nil, err return nil, err
} }
h.noAuthConfigPath = path.Join(configDir, config.NoAuthProbeConfigFileName) h.noAuthConfigPath = path.Join(*config.ConfigDir, config.NoAuthProbeConfigFileName)
h.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
conf := lfcc.New() conf := lfcc.New()
if lfcc.Exists(h.noAuthConfigPath) { if lfcc.Exists(h.noAuthConfigPath) {
if err = conf.Load(&h.noAuthConfig, h.noAuthConfigPath); nil != err { if err = conf.Load(&h.noAuthConfig, h.noAuthConfigPath); nil != err {
return nil, fmt.Errorf("Auth: Loading of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err) return nil, fmt.Errorf("Auth: Loading of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)
} }
} else {
if err = lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
return nil, fmt.Errorf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)
}
} }
return h, nil return h, nil
} }
func (h *authHandlers) Serve() error { func (h *authHandlers) Serve() error {
if nil != config.CFG.Probe.Key || "" != *config.CFG.Probe.Key {
return nil
}
if nil != h.noAuthConfig.DenyDate { if nil != h.noAuthConfig.DenyDate {
return fmt.Errorf("Cannot start because this probe have been denied from overFlow[%s]", h.noAuthConfig.DenyDate.String()) return fmt.Errorf("Cannot start because this probe have been denied from overFlow[%s]", h.noAuthConfig.DenyDate.String())
} }

View File

@ -7,6 +7,7 @@ import (
lfcc "git.loafle.net/commons_go/config" lfcc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/central/api/module" "git.loafle.net/overflow/overflow_probes/central/api/module"
"git.loafle.net/overflow/overflow_probes/config"
) )
func (h *authHandlers) onNotify(method string, params []string) { func (h *authHandlers) onNotify(method string, params []string) {
@ -23,17 +24,20 @@ func (h *authHandlers) onNotify(method string, params []string) {
func (h *authHandlers) onNoAuthProbeAccept(params []string) { func (h *authHandlers) onNoAuthProbeAccept(params []string) {
var err error var err error
probeID := params[0] probeKey := params[0]
if lfcc.Exists(h.probeConfigPath) { // if lfcc.Exists(h.probeConfigPath) {
if err = lfcc.Load(&h.probeConfig, h.probeConfigPath); nil != err { // if err = lfcc.Load(&h.probeConfig, h.probeConfigPath); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Loading of Probe config file[%s] failed error[%v]", h.probeConfigPath, err)) // logging.Logger.Error(fmt.Sprintf("Auth: Loading of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
} // }
} // }
h.probeConfig.ID = &probeID config.CFG.Probe.Key = &probeKey
if err = lfcc.Save(h.probeConfig, h.probeConfigPath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of Probe config file[%s] failed error[%v]", h.probeConfigPath, err)) if err = lfcc.Save(*config.CFG, *config.ConfigFilePath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of config file[%s] failed error[%v]", *config.ConfigFilePath, err))
h.shutdownChan <- true
return
} }
h.acceptedChan <- true h.acceptedChan <- true
@ -44,6 +48,8 @@ func (h *authHandlers) onNoAuthProbeDeny(params []string) {
h.noAuthConfig.DenyDate = &n h.noAuthConfig.DenyDate = &n
if err := lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err { if err := lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)) logging.Logger.Error(fmt.Sprintf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err))
h.shutdownChan <- true
return
} }
h.deniedChan <- fmt.Errorf("This probe have been denied from overFlow") h.deniedChan <- fmt.Errorf("This probe have been denied from overFlow")

View File

@ -16,7 +16,7 @@ import (
func getRegistHeader() (string, error) { func getRegistHeader() (string, error) {
var err error var err error
nap := module.NoAuthProbe{ nap := module.NoAuthProbe{
APIKey: config.Config.Domain.APIKey, APIKey: *config.CFG.Central.APIKey,
} }
var nad *module.NoAuthProbeDescription var nad *module.NoAuthProbeDescription
if nad, err = getDescription(); nil != err { if nad, err = getDescription(); nil != err {

View File

@ -1,8 +1,10 @@
{ {
"domain": { "central": {
"url": "ws://127.0.0.1:19190",
"apikey": "52abd6fd57e511e7ac52080027658d13" "apikey": "52abd6fd57e511e7ac52080027658d13"
}, },
"central": {
"url": "ws://127.0.0.1:19190" "probe": {
"key": ""
} }
} }

View File

@ -1,6 +0,0 @@
package config
type CentralConfig struct {
URL string `json:"url" yaml:"url" toml:"url"`
EntryPoints map[string]string `json:"entryPoints" yaml:"entryPoints" toml:"entryPoints"`
}

View File

@ -4,9 +4,20 @@ const (
ConfigFileName = "config.json" ConfigFileName = "config.json"
) )
var Config AllConfig var ConfigDir *string
var ConfigFilePath *string
var CFG *Config
type AllConfig struct { type Config struct {
Domain DomainConfig `json:"domain" yaml:"domain" toml:"domain"`
Central CentralConfig `json:"central" yaml:"central" toml:"central"` Central CentralConfig `json:"central" yaml:"central" toml:"central"`
Probe ProbeConfig `json:"probe" yaml:"probe" toml:"probe"`
}
type CentralConfig struct {
URL *string `json:"url" yaml:"url" toml:"url"`
APIKey *string `json:"apiKey" yaml:"apiKey" toml:"apiKey"`
}
type ProbeConfig struct {
Key *string `json:"key,omitempty" yaml:"key" toml:"key"`
} }

View File

@ -1,5 +0,0 @@
package config
type DomainConfig struct {
APIKey string `json:"apiKey" yaml:"apiKey" toml:"apiKey"`
}

View File

@ -1,9 +0,0 @@
package config
const (
ProbeConfigFileName = "probe.json"
)
type ProbeConfig struct {
ID *string `json:"id,omitempty" yaml:"id" toml:"id"`
}

81
main.go
View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"path"
"syscall" "syscall"
"time" "time"
@ -18,20 +19,17 @@ import (
"github.com/takama/daemon" "github.com/takama/daemon"
) )
const ( /*
version = "1.0.0" /bin/
website = "https://www.overflow.cloud" probe.exe
banner = ` /config/
/sensor/
noauthprobe.json
central.json
probe.json
logging.json
*/
`
)
const ( const (
// name of the service // name of the service
@ -113,28 +111,33 @@ func main() {
var status string var status string
var handler commons.Handler var handler commons.Handler
isRunning := true isRunning := true
var confDir string
defer logging.Logger.Sync() defer logging.Logger.Sync()
fmt.Println(banner) printBanner()
fmt.Printf("Version: %s\n", version)
fmt.Printf("URL: %s\n", website)
fmt.Println()
if dir, err := lfcc.ABSPathify(*configDir); nil != err { if dir, err := lfcc.ABSPathify(*configDir); nil != err {
logging.Logger.Panic(fmt.Sprintf("Probe: config path[%s] is not valid", *configDir)) logging.Logger.Panic(fmt.Sprintf("Probe: config path[%s] is not valid", *configDir))
} else { } else {
logging.Logger.Debug(fmt.Sprintf("Probe: config path[%s]", dir)) logging.Logger.Debug(fmt.Sprintf("Probe: config path[%s]", dir))
confDir = dir config.ConfigDir = &dir
} }
cfp := path.Join(*config.ConfigDir, config.ConfigFileName)
config.ConfigFilePath = &cfp
conf := lfcc.New() conf := lfcc.New()
conf.SetConfigPath(*configDir) config.CFG = &config.Config{}
if err := conf.Load(&config.Config, "config.json"); nil != err { if err := conf.Load(config.CFG, *config.ConfigFilePath); nil != err {
logging.Logger.Panic(fmt.Sprintf("Probe: config is not valid error[%v]", err)) logging.Logger.Panic(fmt.Sprintf("Probe: config is not valid error[%v]", err))
} }
if nil == config.CFG.Central.APIKey {
logging.Logger.Panic("Probe: APIKey is required")
}
if nil == config.CFG.Central.URL {
logging.Logger.Panic("Probe: URL of overFlow Central is required")
}
if srv, err = daemon.New(serviceName, serviceDescription); nil != err { if srv, err = daemon.New(serviceName, serviceDescription); nil != err {
logging.Logger.Panic(fmt.Sprintf("Probe: %v", err)) logging.Logger.Panic(fmt.Sprintf("Probe: %v", err))
} }
@ -150,18 +153,17 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if handler, err = auth.New(confDir); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: error: %v", err))
os.Exit(1)
}
go func() { go func() {
if handler, err = auth.New(); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: error: %v", err))
os.Exit(1)
}
if err := handler.Serve(); err != nil { if err := handler.Serve(); err != nil {
logging.Logger.Error(fmt.Sprintf("Probe: Authenticator error: %v", err)) logging.Logger.Error(fmt.Sprintf("Auth: Stopped[%v]", err))
return return
} }
if handler, err = probe.New(confDir); nil != err { if handler, err = probe.New(); nil != err {
logging.Logger.Error(fmt.Sprintf("Probe: error: %v", err)) logging.Logger.Error(fmt.Sprintf("Probe: error: %v", err))
return return
} }
@ -213,3 +215,26 @@ func main() {
// } // }
} }
const (
version = "1.0.0"
website = "https://www.overflow.cloud"
banner = `
`
)
func printBanner() {
fmt.Println(banner)
fmt.Printf("Version: %s\n", version)
fmt.Printf("URL: %s\n", website)
fmt.Println()
}

Binary file not shown.

View File

@ -1,3 +0,0 @@
{
"id": ""
}

15
probe/on_notify.go Normal file
View File

@ -0,0 +1,15 @@
package probe
import "git.loafle.net/overflow/overflow_probes/central/api/module"
func (p *probe) onNotify(method string, params []string) {
switch method {
case module.NoAuthProbeService_AcceptNoAuthProbe:
break
case module.NoAuthProbeService_DenyNoauthProbe:
break
}
}

View File

@ -5,9 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"path"
lfcc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/central/api/module" "git.loafle.net/overflow/overflow_probes/central/api/module"
"git.loafle.net/overflow/overflow_probes/central/client" "git.loafle.net/overflow/overflow_probes/central/client"
@ -19,6 +17,7 @@ import (
const ( const (
probeEntryPoint = "/probe" probeEntryPoint = "/probe"
metricsEntryPoint = "/metrics" metricsEntryPoint = "/metrics"
fileEntryPoint = "/file"
) )
type Probe interface { type Probe interface {
@ -26,10 +25,6 @@ type Probe interface {
} }
type probe struct { type probe struct {
configDir string
probeConfigPath string
probeConfig config.ProbeConfig
probeEntryURL string probeEntryURL string
metricsEntryURL string metricsEntryURL string
@ -39,31 +34,19 @@ type probe struct {
shutdownChan chan bool shutdownChan chan bool
} }
func New(configDir string) (Probe, error) { func New() (Probe, error) {
p := &probe{ p := &probe{
configDir: configDir,
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
} }
var err error var err error
if p.probeEntryURL, err = opuu.Join(config.Config.Central.URL, probeEntryPoint); nil != err { if p.probeEntryURL, err = opuu.Join(*config.CFG.Central.URL, probeEntryPoint); nil != err {
return nil, err return nil, err
} }
if p.metricsEntryURL, err = opuu.Join(config.Config.Central.URL, metricsEntryPoint); nil != err { if p.metricsEntryURL, err = opuu.Join(*config.CFG.Central.URL, metricsEntryPoint); nil != err {
return nil, err return nil, err
} }
p.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
conf := lfcc.New()
if !lfcc.Exists(p.probeConfigPath) {
return nil, fmt.Errorf("Probe: Config file[%s] is not exist", p.probeConfigPath)
}
if err = conf.Load(&p.probeConfig, p.probeConfigPath); nil != err {
return nil, fmt.Errorf("Probe: Loading of Probe config file[%s] failed error[%v]", p.probeConfigPath, err)
}
return p, nil return p, nil
} }
@ -74,16 +57,15 @@ func (p *probe) Serve() error {
return err return err
} }
ListenLoop: // ListenLoop:
for { for {
select { select {
case <-p.shutdownChan: case <-p.shutdownChan:
err = errors.New("Shutting down") return errors.New("Shutting down")
break ListenLoop
} }
} }
return err return nil
} }
func (p *probe) Shutdown(ctx context.Context) error { func (p *probe) Shutdown(ctx context.Context) error {
@ -93,7 +75,7 @@ func (p *probe) Shutdown(ctx context.Context) error {
func (p *probe) connectToCentral() error { func (p *probe) connectToCentral() error {
header := http.Header{} header := http.Header{}
header[module.ProbeHeader_ProbeKey] = []string{*p.probeConfig.ID} header[module.ProbeHeader_ProbeKey] = []string{*config.CFG.Probe.Key}
var res *http.Response var res *http.Response
var err error var err error
@ -112,15 +94,3 @@ func (p *probe) connectToCentral() error {
return nil return nil
} }
func (p *probe) onNotify(method string, params []string) {
switch method {
case module.NoAuthProbeService_AcceptNoAuthProbe:
break
case module.NoAuthProbeService_DenyNoauthProbe:
break
}
}