- /**
- * This file is part of BravoSystem.
- * Copyright(c) 2015 by chnvideo.com
- * All rights reserved.
- *
- */
- package core
- import (
- "encoding/json"
- "fmt"
- "strings"
- )
- type BravoPersistenceIps struct {
- // the servers of all ips
- servers map[string]*BravoServerIndex
- }
- func (v *BravoPersistenceIps) Search(ip string) (obj BravoJson) {
- obj = BravoJson{}
- head := ip[0: strings.Index(ip, ".")]
- for _,indexs := range v.servers {
- for _,iphead := range indexs.ipheads {
- if iphead == head {
- indexs.Search(ip, obj)
- if obj["server"] != nil {
- fmt.Println(obj["server"], obj["province"], obj["city"])
- return
- }
- }
- }
- }
- return
- }
- func (v *BravoPersistenceIps) Read() {
- var results = [][]string{}
- servers := make(map[string]*BravoServerIndex)
- results = v.persistence.Execute("SELECT * FROM server_index")
- for _, index := range results {
- server_index := NewBravoServerIndex()
- server_index.Dump(index)
- server_index.Read(v.persistence)
- servers[server_index.name] = server_index
- }
- v.servers = servers
- }
- func NewBravoPersistenceIps() *BravoPersistenceIps {
- v := &BravoPersistenceIps{}
- v.servers = make(map[string]*BravoServerIndex)
- v.persistence = NewBravoPersistence()
- return v
- }
- type BravoServerIndex struct {
- // the id of the server
- id string
- // the name of the server
- name string
- // the server ips, divided in ip heads
- ips map[string]*BravoIpHead
- // the ip heads of the server
- ipheads []string
- }
- func (v *BravoServerIndex) Dump(db_item []string) {
- v.id = db_item[0]
- v.name = db_item[1]
- i := strings.Index(db_item[2], "[")
- ip_heads := string([]byte(db_item[2])[i+1: len(db_item[2])])
- i = strings.Index(ip_heads, "]")
- ip_heads = string([]byte(ip_heads)[0: i])
- v.ipheads = strings.Split(ip_heads, ",")
- for k,head := range v.ipheads {
- i = strings.Index(head, "\\"")
- head = string([]byte(head)[i+1: len(head)])
- i = strings.LastIndex(head, "\\"")
- head = string([]byte(head)[0: i])
- v.ipheads[k] = head
- }
- }
- func (v *BravoServerIndex) Read(persistence *BravoPersistence) {
- var results = [][]string{}
- results = persistence.Execute("SELECT * FROM " + v.name)
- for _, info := range results {
- start_ip := info[2]
- head := start_ip[0: strings.Index(start_ip, ".")]
- var ip_head *BravoIpHead
- var ok bool
- if ip_head,ok = v.ips[head]; !ok {
- ip_head = NewBravoIpHead()
- v.ips[head] = ip_head
- }
- ip_head.Dump(info)
- }
- }
- func (v *BravoServerIndex) Search(ip string, obj BravoJson) {
- head := ip[0: strings.Index(ip, ".")]
- v.ips[head].Search(ip, obj)
- return
- }
- func NewBravoServerIndex() *BravoServerIndex {
- v := &BravoServerIndex{}
- v.ips = make(map[string]*BravoIpHead)
- return v
- }
- type BravoIpHead struct {
- ips []*BravoIpInfo
- }
- func (v *BravoIpHead) Dump(info []string) {
- ip_info := NewBravoIpInfo()
- ip_info.Dump(info)
- v.ips = append(v.ips, ip_info)
- }
- func (v *BravoIpHead) Search(ip string, obj BravoJson){
- for _,info := range v.ips {
- ips := strings.Split(ip, ".")
- start_ips := strings.Split(info.start_ip, ".")
- end_ips := strings.Split(info.end_ip, ".")
- for k,vv := range ips {
- if (vv >= start_ips[k] && vv < end_ips[k]) || (vv > start_ips[k] && vv <= end_ips[k]){
- info.DumpObj(obj)
- return
- }
- if vv == start_ips[k] && vv == end_ips[k]{
- continue
- }
- break
- }
- }
- return
- }
- func NewBravoIpHead() *BravoIpHead {
- v := &BravoIpHead{}
- return v
- }
- type BravoIpInfo struct {
- server string
- start_ip string
- end_ip string
- province string
- city string
- }
- func (v *BravoIpInfo) Dump(info []string) {
- v.server = info[1]
- v.start_ip = info[2]
- v.end_ip = info[3]
- v.province = info[4]
- v.city = info[5]
- }
- func (v *BravoIpInfo) DumpObj(obj BravoJson) {
- obj["server"] = v.server
- obj["province"] = v.province
- obj["city"] = v.city
- }
- func NewBravoIpInfo() *BravoIpInfo {
- v := &BravoIpInfo{}
- return v
- }
- type BravoPersistence struct {
- Host string
- Port int64
- User string
- Password string
- Name string
- }
- func (v *BravoPersistence) Execute(query string) (results [][]string) {
- cmd := v.User + ":" + v.Password + "@/" + v.Name
- db, err := sql.Open("mysql", cmd)
- if err != nil {
- panic(err.Error())
- }
- defer db.Close()
- // Execute the query
- rows, err := db.Query(query)
- if err != nil {
- panic(err.Error())
- }
- // Get column names
- columns, err := rows.Columns()
- if err != nil {
- panic(err.Error())
- }
- // Make a slice for the values
- values := make([]sql.RawBytes, len(columns))
- // rows.Scan wants '[]interface{}' as an argument, so we must copy the
- // references into such a slice
- // See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details
- scanArgs := make([]interface{}, len(values))
- for i := range values {
- scanArgs[i] = &values[i]
- }
- // Fetch rows
- for rows.Next() {
- // get RawBytes from data
- err = rows.Scan(scanArgs...)
- if err != nil {
- panic(err.Error())
- }
- // Now do something with the data.
- // Here we just print each column as a string.
- var value string
- result := []string{}
- for _, col := range values {
- // Here we can check if the value is nil (NULL value)
- if col == nil {
- value = "NULL"
- } else {
- value = string(col)
- result = append(result, value)
- }
- }
- results = append(results, result)
- }
- if err = rows.Err(); err != nil {
- panic(err.Error())
- }
- return
- }
- func NewBravoPersistence() *BravoPersistence {
- v := &BravoPersistence{}
- err := json.Unmarshal(persistence, v)
- if err != nil {
- fmt.Println("error in translating,", err.Error())
- return v
- }
- return v
- }
- //该片段来自于http://www.codesnippet.cn/detail/0605201512485.html
来源: http://www.codesnippet.cn/detail/0605201512485.html