mirror of
https://github.com/zr-hebo/sniffer-agent.git
synced 2025-08-11 20:34:46 +08:00
add communicator api
This commit is contained in:
@@ -2,13 +2,13 @@ package communicator
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
hu "github.com/zr-hebo/util-http"
|
||||
_ "net/http/pprof"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
THROW_PACKET_RATE = "throw_packet_rate"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -16,34 +16,15 @@ var (
|
||||
router = mux.NewRouter()
|
||||
)
|
||||
|
||||
var (
|
||||
configMapLock sync.RWMutex
|
||||
configMap map[string]configItem
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.IntVar(&communicatePort, "communicate_port", 8088, "http server port. Default is 8088")
|
||||
|
||||
router.Path("/get_status").Methods("GET").HandlerFunc(getStatus)
|
||||
router.Path("/set_config").Methods("POST").HandlerFunc(setConfig)
|
||||
configMap = make(map[string]configItem)
|
||||
tprc := newThrowPacketRateConfig()
|
||||
configMap[tprc.name] = tprc
|
||||
}
|
||||
|
||||
func Server() {
|
||||
server := &http.Server{
|
||||
Addr: "0.0.0.0:" + strconv.Itoa(communicatePort),
|
||||
IdleTimeout: time.Second * 5,
|
||||
}
|
||||
|
||||
http.Handle("/", router)
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getStatus(resp http.ResponseWriter, req *http.Request) {
|
||||
mp := hu.NewMouthpiece(resp)
|
||||
defer mp.Convey()
|
||||
mp.Data = "OK"
|
||||
}
|
||||
|
||||
func setConfig(resp http.ResponseWriter, req *http.Request) {
|
||||
mp := hu.NewMouthpiece(resp)
|
||||
defer mp.Convey()
|
||||
mp.Data = "OK"
|
||||
}
|
||||
|
||||
|
83
communicator/controller.go
Normal file
83
communicator/controller.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package communicator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
hu "github.com/zr-hebo/util-http"
|
||||
"github.com/zr-hebo/validator"
|
||||
)
|
||||
|
||||
func Server() {
|
||||
server := &http.Server{
|
||||
Addr: "0.0.0.0:" + strconv.Itoa(communicatePort),
|
||||
IdleTimeout: time.Second * 5,
|
||||
}
|
||||
|
||||
http.Handle("/", router)
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func outletCheckAlive(resp http.ResponseWriter, req *http.Request) {
|
||||
mp := hu.NewMouthpiece(resp)
|
||||
defer func() {
|
||||
_ = mp.Convey()
|
||||
}()
|
||||
|
||||
mp.Data = "OK"
|
||||
}
|
||||
|
||||
func outletGetConfig(resp http.ResponseWriter, req *http.Request) {
|
||||
mp := hu.NewMouthpiece(resp)
|
||||
defer func() {
|
||||
_ = mp.Convey()
|
||||
}()
|
||||
|
||||
ep := &struct {
|
||||
ConfigName string `validate:"nonzero" json:"config_name"`
|
||||
}{}
|
||||
up := hu.NewUnpacker(req, ep, nil)
|
||||
if err := up.Unpack(); err != nil {
|
||||
mp.Err = err
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(*ep); err != nil {
|
||||
mp.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
_, ok := configMap[ep.ConfigName]
|
||||
if !ok {
|
||||
mp.Err = fmt.Errorf("no config %s found", ep.ConfigName)
|
||||
return
|
||||
}
|
||||
|
||||
mp.Data = GetConfig(ep.ConfigName)
|
||||
}
|
||||
|
||||
func outletSetConfig(resp http.ResponseWriter, req *http.Request) {
|
||||
mp := hu.NewMouthpiece(resp)
|
||||
defer func() {
|
||||
_ = mp.Convey()
|
||||
}()
|
||||
|
||||
ep := &struct {
|
||||
ConfigName string `validate:"nonzero" json:"config_name"`
|
||||
Value interface{} `json:"value"`
|
||||
}{}
|
||||
up := hu.NewUnpacker(req, ep, nil)
|
||||
if err := up.Unpack(); err != nil {
|
||||
mp.Err = err
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(*ep); err != nil {
|
||||
mp.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
mp.Err = SetConfig(ep.ConfigName, ep.Value)
|
||||
}
|
27
communicator/major_function.go
Normal file
27
communicator/major_function.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package communicator
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SetConfig set config by config key(name) and value
|
||||
func SetConfig(key string, val interface{}) (err error) {
|
||||
configMapLock.Lock()
|
||||
defer configMapLock.Unlock()
|
||||
|
||||
config, ok := configMap[key]
|
||||
if !ok {
|
||||
err = fmt.Errorf("no config %s exist", key)
|
||||
return
|
||||
}
|
||||
|
||||
err = config.setVal(val)
|
||||
return
|
||||
}
|
||||
|
||||
// GetConfig get config value by config key(name)
|
||||
func GetConfig(key string) (val interface{}) {
|
||||
configMapLock.RLock()
|
||||
defer configMapLock.RUnlock()
|
||||
|
||||
config := configMap[key]
|
||||
return config.getVal()
|
||||
}
|
36
communicator/model.go
Normal file
36
communicator/model.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package communicator
|
||||
|
||||
import "fmt"
|
||||
|
||||
type configItem interface {
|
||||
setVal (interface{}) error
|
||||
getVal () interface{}
|
||||
}
|
||||
|
||||
type throwPacketRateConfig struct {
|
||||
name string
|
||||
value float64
|
||||
}
|
||||
|
||||
func newThrowPacketRateConfig() (tpr *throwPacketRateConfig) {
|
||||
tpr = &throwPacketRateConfig{
|
||||
name: THROW_PACKET_RATE,
|
||||
value: 0.0,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tc *throwPacketRateConfig) setVal (val interface{}) (err error){
|
||||
realVal, ok := val.(float64)
|
||||
if !ok {
|
||||
err = fmt.Errorf("cannot reansform val: %v to float64", val)
|
||||
return
|
||||
}
|
||||
|
||||
tc.value = realVal
|
||||
return
|
||||
}
|
||||
|
||||
func (tc *throwPacketRateConfig) getVal () (val interface{}){
|
||||
return tc.value
|
||||
}
|
7
communicator/url.go
Normal file
7
communicator/url.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package communicator
|
||||
|
||||
func init() {
|
||||
router.Path("/check_alive").Methods("GET").HandlerFunc(outletCheckAlive)
|
||||
router.Path("/get_config").Methods("GET").HandlerFunc(outletGetConfig)
|
||||
router.Path("/set_config").Methods("POST").HandlerFunc(outletSetConfig)
|
||||
}
|
Reference in New Issue
Block a user