咱们前面已经实现了API的基础版本,能对参数校验和返回指定数据,这一章,咱们将对主机和交换机进行建模,存入数据库。
考虑到数据库安装和使用的简便性,咱们采用文档存储结构的MongoDB数据库。前端
Mongo数据库下载安装,安装后不用设置密码,直接使用便可
下载连接 https://www.filehorse.com/download-mongodb/download/ 或者 https://www.mongodb.com/try/download/communitygit
使用数据库以前须要配置数据库地址和端口,因此咱们将配置信息存放到配置文件,采用
yaml
格式存储解析github
这里,咱们还将API监听的地址和端口,日志路径也能够都配上golang
api_server: env: prod host: 127.0.0.1 port: 9000 mgo: uri: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb database: gin_ips pool_size: 100 log: path: log/gin_ips level: DEBUG name: gin.log count: 180
// 通用 Config 接口 type Config interface { InitError(msg string) error } // 根据yaml文件初始化通用配置, 无需输出日志 func InitYaml(filename string, config Config) error { fp, err := os.Open(filename) if err != nil { msg := fmt.Sprintf("configure file [ %s ] not found", filename) return config.InitError(msg) } defer func() { _ = fp.Close() }() if err := yaml.NewDecoder(fp).Decode(config); err != nil { msg := fmt.Sprintf("configure file [ %s ] initialed failed", filename) return config.InitError(msg) } return nil }
golang中有多种优秀的orm库,例如
xorm
,gorm
。因为orm将数据库模型和语言紧密封装,使用起来很是方便,很适合web前端开发。
但与此同时,使用orm也会致使部分性能丢失(深度使用后会发现隐藏的坑也很多),有兴趣的同窗能够了解下。
本文主要使用golang官方mongodb库mongo-driver
,直接经过sql
语句操做数据库(这个过程能够学习下如何explain和优化sql语句)。web
type Collection struct { client *mongo.Collection database string // 数据库 collection string // 集合 }
// 链接池建立 func CreatePool(uri string, size uint64) (pool *mongo.Client, e error) { defer func() { if err := recover(); err != nil { e = errors.New(fmt.Sprintf("%v", err)) } }() ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) // 10s超时 defer cancel() var err error pool, err = mongo.Connect(ctx, options.Client().ApplyURI(uri).SetMinPoolSize(size)) // 链接池 if err != nil { return pool, err } err = pool.Ping(context.Background(), nil) // 检查链接 if err != nil { return pool, err } return pool, nil }
func DestroyPool(client *mongo.Client) error { err := client.Disconnect(context.Background()) if err != nil { return err } return nil }
// 查找单个文档, sort 等于1表示 返回最旧的,sort 等于-1 表示返回最新的 /* BSON(二进制编码的JSON) D家族 bson.D D:一个BSON文档。这种类型应该在顺序重要的状况下使用,好比MongoDB命令。 M:一张无序的map。它和D是同样的,只是它不保持顺序。 A:一个BSON数组。 E:D里面的一个元素。 */ func (m *Collection) FindOne(filter bson.D, sort, projection bson.M) (bson.M, error) { findOptions := options.FindOne().SetProjection(projection) if sort != nil { findOptions = findOptions.SetSort(sort) } singleResult := m.client.FindOne(context.Background(), filter, findOptions) var result bson.M if err := singleResult.Decode(&result); err != nil { return result, err } return result, nil } /* 查询多个 sort 等于1表示 返回最旧的,sort 等于-1 表示返回最新的 每次只返回1页 page size 大小的数据 project 不能混合 True 和 False */ func (m *Collection) FindLimit(filter bson.D, page, pageSize uint64, sort, projection bson.M) ([]bson.M, error) { var resultArray []bson.M if page == 0 || pageSize == 0 { return resultArray, errors.New("page or page size can't be 0") } skip := int64((page - 1) * pageSize) limit := int64(pageSize) if projection == nil { projection = bson.M{} } findOptions := options.Find().SetProjection(projection).SetSkip(skip).SetLimit(limit) if sort != nil { findOptions = findOptions.SetSort(sort) } cur, err := m.client.Find(context.Background(), filter, findOptions) if err != nil { return resultArray, err } defer func() { _ = cur.Close(context.Background()) }() for cur.Next(context.Background()) { var result bson.M err := cur.Decode(&result) if err != nil { return resultArray, err } resultArray = append(resultArray, result) } //err = cur.All(context.Background(), &resultArray) if err := cur.Err(); err != nil { return resultArray, err } return resultArray, nil } // 返回查找条件的所有文档记录 // project 不能混合 True 和 False func (m *Collection) FindAll(filter bson.D, sort, projection bson.M) ([]bson.M, error) { var resultArray []bson.M if projection == nil { projection = bson.M{} } findOptions := options.Find().SetProjection(projection) if sort != nil { findOptions = findOptions.SetSort(sort) } cur, err := m.client.Find(context.Background(), filter, findOptions) if err != nil { return resultArray, err } defer func() { _ = cur.Close(context.Background()) }() for cur.Next(context.Background()) { // fmt.Println(cur.Current) var result bson.M err := cur.Decode(&result) if err != nil { return resultArray, err } resultArray = append(resultArray, result) } if err := cur.Err(); err != nil { return resultArray, err } return resultArray, nil }
//插入单个 func (m *Collection) InsertOne(document interface{}) (primitive.ObjectID, error) { insertResult, err := m.client.InsertOne(context.Background(), document) var objectId primitive.ObjectID if err != nil { return objectId, err } objectId = insertResult.InsertedID.(primitive.ObjectID) return objectId, nil } //插入多个文档 func (m *Collection) InsertMany(documents []interface{}) ([]primitive.ObjectID, error) { var insertDocs []interface{} for _, doc := range documents { insertDocs = append(insertDocs, doc) } insertResult, err := m.client.InsertMany(context.Background(), insertDocs) var objectIds []primitive.ObjectID if err != nil { return objectIds, err } for _, oid := range insertResult.InsertedIDs { objectIds = append(objectIds, oid.(primitive.ObjectID)) } return objectIds, nil }
/* 更新 filter 返回的第一条记录 若是匹配到了(matchCount >= 1) 而且 objectId.IsZero()= false 则是新插入, objectId.IsZero()=true则是更新(更新没有获取到id) ObjectID("000000000000000000000000") document 修改成 interface 表示支持多个字段更新,使用bson.D ,即 []bson.E */ func (m *Collection) UpdateOne(filter bson.D, document interface{}, insert bool) (int64, primitive.ObjectID, error) { updateOption := options.Update().SetUpsert(insert) updateResult, err := m.client.UpdateOne(context.Background(), filter, document, updateOption) var objectId primitive.ObjectID if err != nil { return 0, objectId, err } if updateResult.UpsertedID != nil { objectId = updateResult.UpsertedID.(primitive.ObjectID) } // fmt.Println(objectId.IsZero()) return updateResult.MatchedCount, objectId, nil } /* 更新 filter 返回的全部记录,返回的匹配是指本次查询匹配到的全部数量,也就是最后更新后等于新的值的数量 若是匹配到了(matchCount >= 1) 而且 objectId.IsZero()= false 则是新插入, objectId.IsZero()=true则是更新(更新没有获取到id) ObjectID("000000000000000000000000") docAction 修改成 interface 表示支持多个字段更新,使用bson.D ,即 []bson.E */ func (m *Collection) UpdateMany(filter bson.D, docAction interface{}, insert bool) (int64, primitive.ObjectID, error) { updateOption := options.Update().SetUpsert(insert) updateResult, err := m.client.UpdateMany(context.Background(), filter, docAction, updateOption) var objectId primitive.ObjectID if err != nil { return 0, objectId, err } if updateResult.UpsertedID != nil { objectId = updateResult.UpsertedID.(primitive.ObjectID) } // fmt.Println(objectId.IsZero()) return updateResult.MatchedCount, objectId, nil } /* 替换 filter 返回的1条记录(最旧的) 若是匹配到了(matchCount >= 1) 而且 objectId.IsZero()= false 则是新插入, objectId.IsZero()=true则是更新(更新没有获取到id) ObjectID("000000000000000000000000") 采用 FindOneAndReplace 在查找不到但正确插入新的数据会有"mongo: no documents in result" 的错误 */ func (m *Collection) Replace(filter bson.D, document interface{}, insert bool) (int64, primitive.ObjectID, error) { option := options.Replace().SetUpsert(insert) replaceResult, err := m.client.ReplaceOne(context.Background(), filter, document, option) var objectId primitive.ObjectID if err != nil { return 0, objectId, err } if replaceResult.UpsertedID != nil { objectId = replaceResult.UpsertedID.(primitive.ObjectID) } // fmt.Println(objectId.IsZero()) return replaceResult.MatchedCount, objectId, nil }
/* 查找并删除一个 sort 等于1表示 删除最旧的,sort 等于-1 表示删除最新的 通常根据 id 查找就会保证删除正确 */ func (m *Collection) DeleteOne(filter bson.D, sort bson.M) (bson.M, error) { findOptions := options.FindOneAndDelete() if sort != nil { findOptions = findOptions.SetSort(sort) } singleResult := m.client.FindOneAndDelete(context.Background(), filter, findOptions) var result bson.M if err := singleResult.Decode(&result); err != nil { return result, err } return result, nil } /* 根据条件删除所有 */ func (m *Collection) DeleteAll(filter bson.D) (int64, error) { count, err := m.client.DeleteMany(context.Background(), filter) if err != nil { return 0, err } return count.DeletedCount, nil }
// 建立索引,重复建立不会报错 func (m *Collection) CreateIndex(index string, unique bool) (string, error) { indexModel := mongo.IndexModel{Keys: bson.M{index: 1}, Options: options.Index().SetUnique(unique)} name, err := m.client.Indexes().CreateOne(context.Background(), indexModel) return name, err }
根据需求,咱们将主机和交换机的各个字段提早定义好,这时候能够考虑各个模型分开存储到多个集合,也能够合并成1个(本文选择合并)sql
//|ID|主机名|IP|内存大小|磁盘大小|类型|负责人| type HostModel struct { Oid configure.Oid `json:"oid"` // 考虑全部实例存放在同一个集合中,须要一个字段来区分 Id string `json:"id"` Ip string `json:"ip"` Hostname string `json:"hostname"` MemSize int64 `json:"mem_size"` DiskSize int64 `json:"disk_size"` Class string `json:"class"` // 主机类型 Owner []string `json:"owner"` } //|ID|设备名|管理IP|虚IP|带外IP|厂家|负责人| type SwitchModel struct { Oid configure.Oid `json:"oid"` // 考虑全部实例存放在同一个集合中,须要一个字段来区分 Id string `json:"id"` Name string `json:"name"` Ip string `json:"ip"` Vip []string `json:"vip"` ConsoleIp string `json:"console_ip"` Manufacturers string `json:"manufacturers"` // 厂家 Owner []string `json:"owner"` }
随机插入几条测试数据便可mongodb
hmArr := []HostModel{ { Oid: configure.OidHost, Id: "H001", Ip: "10.1.162.18", Hostname: "10-1-162-18", MemSize: 1024000, DiskSize: 102400000000, Class: "物理机", Owner: []string{"小林"}, }, { Oid: configure.OidHost, Id: "H002", Ip: "10.1.162.19", Hostname: "10-1-162-19", MemSize: 1024000, DiskSize: 102400000000, Class: "虚拟机", Owner: []string{"小黄"}, }, }
最后咱们修改下参数的验证规则,支持返回指定模型和返回全部模型。测试结果以下:数据库
curl "http://127.0.0.1:8080?ip=10.1.162.18" {"code":0,"message":"","data":{"page":1,"page_size":2,"size":2,"total":2,"list":[{"class":"物理机","disksize":102400000000,"hostname":"10-1-162-18","id":"H001","ip":"10.1.162.18"," memsize":1024000,"oid":"HOST","owner":["小林"]},{"consoleip":"10.3.32.11","id":"S001","ip":"10.2.32.11","manufacturers":"华为","name":"上海集群交换机","oid":"SWITCH","owner":["老马 ","老曹"],"vip":["10.2.20.1","10.2.20.13","10.1.162.18"]}]}} curl "http://127.0.0.1:8080?ip=10.1.162.18&oid=HOST" {"code":0,"message":"","data":{"page":1,"page_size":1,"size":1,"total":1,"list":[{"class":"物理机","disksize":102400000000,"hostname":"10-1-162-18","id":"H001","ip":"10.1.162.18"," memsize":1024000,"oid":"HOST","owner":["小林"]}]}}
本文模拟生产环境完成了数据库的设计和数据配置,下一章,咱们将配置Gin Log,同时开始使用Gin中间件完善API。json
请访问 Gin-IPs 或者搜索 Gin-IPsapi