Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Sep 16, 2021
1 parent a690e3a commit e26337f
Show file tree
Hide file tree
Showing 8 changed files with 841 additions and 841 deletions.
605 changes: 605 additions & 0 deletions api.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type broker struct {
termMessage chan *termMessage
userMessage chan *usrMessage
cmdMessage chan []byte
webMessage chan *webResp
httpMessage chan *httpResp
devCertPool *x509.CertPool
}

Expand All @@ -48,7 +48,7 @@ func newBroker(cfg *config.Config) *broker {
termMessage: make(chan *termMessage, 1000),
userMessage: make(chan *usrMessage, 1000),
cmdMessage: make(chan []byte, 1000),
webMessage: make(chan *webResp, 1000),
httpMessage: make(chan *httpResp, 1000),
}
}

Expand Down Expand Up @@ -208,8 +208,8 @@ func (br *broker) run() {
case data := <-br.cmdMessage:
handleCmdResp(data)

case resp := <-br.webMessage:
handleWebResp(resp)
case resp := <-br.httpMessage:
handleHttpProxyResp(resp)
}
}
}
48 changes: 24 additions & 24 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (

// Config struct
type Config struct {
AddrDev string
AddrUser string
AddrWeb string
WebRedirURL string
WebPort int
SslCert string
SslKey string
SslCacert string // mTLS for device
Token string
WhiteList map[string]bool
DB string
LocalAuth bool
AddrDev string
AddrUser string
AddrHttpProxy string
HttpProxyRedirURL string
HttpProxyPort int
SslCert string
SslKey string
SslCacert string // mTLS for device
Token string
WhiteList map[string]bool
DB string
LocalAuth bool
}

func getConfigOpt(yamlCfg *yaml.File, name string, opt interface{}) {
Expand All @@ -43,16 +43,16 @@ func getConfigOpt(yamlCfg *yaml.File, name string, opt interface{}) {
// Parse config
func Parse(c *cli.Context) *Config {
cfg := &Config{
AddrDev: c.String("addr-dev"),
AddrUser: c.String("addr-user"),
AddrWeb: c.String("addr-web"),
WebRedirURL: c.String("web-redir-url"),
SslCert: c.String("ssl-cert"),
SslKey: c.String("ssl-key"),
SslCacert: c.String("ssl-cacert"),
Token: c.String("token"),
DB: c.String("db"),
LocalAuth: c.Bool("local-auth"),
AddrDev: c.String("addr-dev"),
AddrUser: c.String("addr-user"),
AddrHttpProxy: c.String("addr-http-proxy"),
HttpProxyRedirURL: c.String("http-proxy-redir-url"),
SslCert: c.String("ssl-cert"),
SslKey: c.String("ssl-key"),
SslCacert: c.String("ssl-cacert"),
Token: c.String("token"),
DB: c.String("db"),
LocalAuth: c.Bool("local-auth"),
}

cfg.WhiteList = make(map[string]bool)
Expand All @@ -71,8 +71,8 @@ func Parse(c *cli.Context) *Config {
if err == nil {
getConfigOpt(yamlCfg, "addr-dev", &cfg.AddrDev)
getConfigOpt(yamlCfg, "addr-user", &cfg.AddrUser)
getConfigOpt(yamlCfg, "addr-web", &cfg.AddrWeb)
getConfigOpt(yamlCfg, "web-redir-url", &cfg.WebRedirURL)
getConfigOpt(yamlCfg, "addr-http-proxy", &cfg.AddrHttpProxy)
getConfigOpt(yamlCfg, "http-proxy-redir-url", &cfg.HttpProxyRedirURL)
getConfigOpt(yamlCfg, "ssl-cert", &cfg.SslCert)
getConfigOpt(yamlCfg, "ssl-key", &cfg.SslKey)
getConfigOpt(yamlCfg, "ssl-cacert", &cfg.SslCacert)
Expand Down
10 changes: 5 additions & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const (
msgTypeCmd
msgTypeHeartbeat
msgTypeFile
msgTypeWeb
msgTypeMax = msgTypeWeb
msgTypeHttp
msgTypeMax = msgTypeHttp
)

const rttyProto uint8 = 3
Expand Down Expand Up @@ -268,13 +268,13 @@ func (dev *device) readLoop() {

dev.br.cmdMessage <- b

case msgTypeWeb:
case msgTypeHttp:
if msgLen < 18 {
log.Error().Msg("msgTypeWeb: invalid")
log.Error().Msg("msgTypeHttp: invalid")
return
}

dev.br.webMessage <- &webResp{b, dev}
dev.br.httpMessage <- &httpResp{b, dev}

case msgTypeHeartbeat:
parseHeartbeat(dev, b)
Expand Down
Loading

0 comments on commit e26337f

Please sign in to comment.