提交 ce016860 编写于 作者: J Jason

移植部分代码

上级 1ae3004b
package base
import (
"errors"
"net"
)
type PackConn struct {
net.PacketConn
addr net.Addr
}
func NewPackConn(packConn net.PacketConn, addr net.Addr) *PackConn {
return &PackConn{
PacketConn: packConn,
addr: addr,
}
}
func (c *PackConn) Read(b []byte) (n int, err error) {
return 0, errors.New("此接口不支持读")
}
func (c *PackConn) Write(b []byte) (n int, err error) {
return c.WriteTo(b, c.addr)
}
func (c *PackConn) RemoteAddr() net.Addr {
return c.addr
}
func (c *PackConn) Close() error {
//c.PacketConn = nil
return nil
}
package base
import (
"bytes"
"math/rand"
"time"
)
const _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func RandomString(size int) string {
rand.NewSource(time.Now().UnixNano()) // 产生随机种子
var s bytes.Buffer
for i := 0; i < size; i ++ {
s.WriteByte(_chars[rand.Int63() % int64(len(_chars))])
}
return s.String()
}
package conf
import (
"git.zgwit.com/zgwit/iot-admin/cool/base"
"git.zgwit.com/zgwit/iot-admin/flag"
"gopkg.in/yaml.v2"
"log"
"os"
)
type _database struct {
Desc string `json:"desc" yaml:"desc"`
Type string `json:"type" yaml:"type"`
Url string `json:"url" yaml:"url"`
ShowSQL bool `json:"showSQL" yaml:"showSQL"`
}
type _web struct {
Desc string `yaml:"desc"`
Addr string `yaml:"addr"`
//Cors bool `yaml:"cors"`
Debug bool `yaml:"debug"`
}
type _users map[string]string
type _baseAuth struct {
Desc string `yaml:"desc"`
Enable bool `yaml:"enable"`
Users _users `yaml:"users"`
}
type _sysAdmin struct {
Desc string `yaml:"desc"`
Enable bool `yaml:"enable"`
Addr string `yaml:"addr"`
AppKey string `yaml:"appKey"`
Secret string `yaml:"secret"`
}
type _dbus struct {
Desc string `yaml:"desc"`
Addr string `yaml:"addr"`
}
type _config struct {
Database _database `yaml:"database"`
Web _web `yaml:"web"`
BaseAuth _baseAuth `yaml:"basicAuth"`
SysAdmin _sysAdmin `yaml:"sysAdmin"`
DBus _dbus `yaml:"dbus"`
}
var Config = _config{
Database: _database{
Desc: "数据库配置",
Type: "mysql",
Url: "root:root@tcp(127.0.0.1:3306)/iot?charset=utf8",
ShowSQL: false,
},
Web: _web{
Desc: "Web服务配置",
Addr: ":8080",
},
BaseAuth: _baseAuth{
Desc: "HTTP简单认证,仅用于超级管理员",
Enable: true,
Users: _users{
//"admin": "123456",
},
},
SysAdmin: _sysAdmin{
Desc: "Sys Admin地址",
Enable: false,
Addr: "http://127.0.0.1:8080",
},
DBus: _dbus{
Desc: "数据总线",
Addr: ":1843",
},
}
func Load() error {
//log.Println("加载配置")
//从参数中读取配置文件名
filename := flag.ConfigPath
// 如果没有文件,则使用默认信息创建
if _, err := os.Stat(filename); os.IsNotExist(err) {
return Save()
} else {
y, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return err
}
defer y.Close()
//生成管理员账号 和 随机密码
password := base.RandomString(6)
log.Println("username:admin, password:", password)
Config.BaseAuth.Users["admin"] = password
d := yaml.NewDecoder(y)
return d.Decode(&Config)
}
return nil
}
func Save() error {
//log.Println("保存配置")
//从参数中读取配置文件名
filename := flag.ConfigPath
y, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0755) //os.Create(filename)
if err != nil {
log.Fatal(err)
return err
}
defer y.Close()
e := yaml.NewEncoder(y)
defer e.Close()
return e.Encode(&Config)
}
package cool
//通道
type Tunnel interface {
Close() error
}
//连接
type Link interface {
Write(buf []byte) error
Close() error
Attach(link Link) error
Detach() error
}
package interfaces
type LinkerListener interface {
OnLinkerData(buf []byte)
OnLinkerError(err error)
OnLinkerClose()
}
type Linker interface {
Listen(listener LinkerListener)
Write(buf []byte) error
Close() error
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册