go 语言 web 开发和数据库查询

2017-01-07 11:15:40来源:CSDN作者:qq_30789903人点击

import (       "fmt"       "net/http"       "WebCommand/db"       "log"

)

type Net_tcp_pt_v struct {       Server_ip string       Server_t string       Address      string       State        string       Program_name string       Count string       Input_t string}type Net_tcp_pt_v_slice struct {       V []Net_tcp_pt_v}

func handlenetpt(w http.ResponseWriter, r *http.Request) {       fmt.Println("get in handlecpu ")
	//处理跨域访问问题       w.Header().Set("Access-Control-Allow-Origin", "*")       w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")       w.Header().Set("Access-Control-Allow-Headers","Action, Module")       if "GET" == r.Method {              fmt.Println(db.Remot_os_diskrw_visit())              //返回数据--调用数据库查询数据              fmt.Fprintln(w,db.Remot_os_netpt_visit())              return       }}


func main() {       fmt.Println("hello world")   	//请求路径 - - 处理方法       http.HandleFunc("/netpt/", handlenetpt)       err := http.ListenAndServe(":9090", nil) //设置监听的端口       if err != nil {              log.Fatal("ListenAndServe: ", err)       }}
/////////使用mysql 查询数据///////
import (       "database/sql"       _ "github.com/go-sql-driver/mysql"       //"time"       "fmt"       "encoding/json"       //"time"       "strings")

func Remot_os_netpt_visit()string {       db, _ := sql.Open("mysql", "root:jingxin@tcp(127.0.0.1:3306)/system_visit")       defer db.Close()       //var str_time_now = time.Now().Format("2006-01-02 15:04:05");  //返回当前六分钟的数据       //rows, _ := db.Query("select * from remot_os_task where input_time > (? -INTERVAL 6 MINUTE)" , str_time_now)       rows, _ := db.Query("select * from remot_os_net_pt where 1=1")       var my_netpt_slice Net_tcp_pt_slice       var  res_json string       for rows.Next(){              var server_ip string              var remote_server_mac string              var server_t string              var Address string              var State string              var Program_name string              var input_time string              rows.Scan(&server_ip, &remote_server_mac, &server_t, &Address,&State, &Program_name,  &input_time)              var my_netpt_task = Net_tcp_pt_v{Server_ip:server_ip ,Server_t: server_t,Address:Address ,State:State ,Program_name:Program_name ,Count: "",Input_t: input_time}              my_netpt_slice.N = append(my_netpt_slice.N ,my_netpt_task )              //fmt.Println(my_task_slice)       }       res , _ :=json.Marshal(my_netpt_slice)              //fmt.Println(string(res))              res_json = string(res)return res_json}
////////使用mongo查询数据////////
import (       "encoding/json"       //"strings"       "fmt"       "gopkg.in/mgo.v2"       "gopkg.in/mgo.v2/bson"       "time")

func Remot_os_netpt_visit(ip string , date_time string)string {       mgoSession = getSession()       defer mgoSession.Close()       col := mgoSession.DB("system_vist").C("remote_os_net_pt")       result := Net_tcp_pt_v{}       var res_cpu Net_tcp_pt_v_slice       iter := col.Find(bson.M{ "server_ip" :  {"$regex": "192.168.201.128", "$options":'i'} , "server_t" : { "$gt":   "2016-12-02 03:54:21" } } ).Iter()       for iter.Next(&result) {              fmt.Printf("Result: %v/n", result.Server_ip)              res_cpu.V = append(res_cpu.V, result)       }       res , _ :=json.Marshal(res_cpu)       //fmt.Println(string(res))       res_json := string(res)       return res_json}



最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台