diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..3c82e49 --- /dev/null +++ b/db/db.go @@ -0,0 +1,34 @@ +package db + +import ( + "database/sql" + "fmt" + _ "github.com/go-sql-driver/mysql" + "time" +) + +//var DB *sql.DB + + +func ConnectDb() *sql.DB { + db, _ := sql.Open("mysql", "root:Skyinno251,@tcp(47.242.184.139:3306)/goftp") + //if err != nil { + // fmt.Println(err.Error()) + // return + //} + db.SetConnMaxLifetime(time.Minute * 3) + db.SetMaxOpenConns(10) + db.SetMaxIdleConns(10) + if err := db.Ping(); err!=nil{ + fmt.Println("open database fail") + return nil + } + fmt.Println("open database success") + //DB=db + //defer db.Close() + return db +} + +func ClosetDb(db *sql.DB) { + defer db.Close() +} diff --git a/go.mod b/go.mod index 6ca3782..caa6cf7 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/mccutchen/go-httpbin go 1.12 + +require github.com/go-sql-driver/mysql v1.2.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9fa3fc4 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/go-sql-driver/mysql v1.1.0 h1:9+YfHL3eyxobwWIChLZyZ20UeNW5HM8/IOcl3OWBOpk= +github.com/go-sql-driver/mysql v1.1.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.2.0 h1:C5cl8DzJiobQuZhND5+a3cOrrRhyaJBPHxZjLgdN8kk= +github.com/go-sql-driver/mysql v1.2.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.3.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= +github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 5e95a9e..7c4ce41 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -6,6 +6,7 @@ import ( "compress/zlib" "encoding/json" "fmt" + "github.com/mccutchen/go-httpbin/models" "math/rand" "net/http" "strconv" @@ -127,6 +128,15 @@ func (h *HTTPBin) IP(w http.ResponseWriter, r *http.Request) { writeJSON(w, body, http.StatusOK) } +func (h *HTTPBin) IP10(w http.ResponseWriter, r *http.Request) { + cmip := models.QueryIp10() + ret := models.Ret{Data: []string{}} + ret.Ret = "ok" + ret.Ip = cmip.Ip + body, _ := json.Marshal(ret) + writeJSON(w, body, http.StatusOK) +} + // UserAgent echoes the incoming User-Agent header func (h *HTTPBin) UserAgent(w http.ResponseWriter, r *http.Request) { body, _ := json.Marshal(&userAgentResponse{ diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index 7d7a8a3..9ea43d0 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -131,6 +131,7 @@ func (h *HTTPBin) Handler() http.Handler { mux.HandleFunc("/delete", methods(h.RequestWithBody, "DELETE")) mux.HandleFunc("/ip", h.IP) + mux.HandleFunc("/ip10", h.IP10) mux.HandleFunc("/user-agent", h.UserAgent) mux.HandleFunc("/headers", h.Headers) mux.HandleFunc("/response-headers", h.ResponseHeaders) diff --git a/models/CmIp.go b/models/CmIp.go new file mode 100644 index 0000000..b959e0a --- /dev/null +++ b/models/CmIp.go @@ -0,0 +1,45 @@ +package models + +import ( + "encoding/json" + "fmt" + _ "github.com/go-sql-driver/mysql" + "github.com/mccutchen/go-httpbin/db" +) + + +type CmIp struct { + Id string `json:"id"` + Ip string `json:"ip"` + Updatetime string `json:"updatetime"` +} + +func QueryIp10() *CmIp { + DB := db.ConnectDb() + var cmip CmIp + rows, e := DB.Query("select * from cm_ip where id = 1") + if e != nil { + //errors.New("query incur error") + fmt.Println("query include error") + return &CmIp{} + } + for rows.Next() { + e := rows.Scan(&cmip.Id, &cmip.Ip, &cmip.Updatetime) + if e != nil { + fmt.Println("json.Marshal error") + return &CmIp{} + } + } + rows.Close() + db.ClosetDb(DB) + body, _ := json.Marshal(cmip) + fmt.Println(string(body)) + + //// 单行查询操作 + //DB := db.ConnectDb() + //DB.QueryRow("select * from cm_ip where id = 1").Scan(&cmip.Id,&cmip.Ip, &cmip.Updatetime) + //body, _ := json.Marshal(cmip) + //fmt.Println(string(body)) + //db.ClosetDb(DB) + return &cmip +} \ No newline at end of file diff --git a/models/ret.go b/models/ret.go new file mode 100644 index 0000000..54588b2 --- /dev/null +++ b/models/ret.go @@ -0,0 +1,11 @@ +package models + +import ( + _ "github.com/go-sql-driver/mysql" +) + +type Ret struct { + Ret string `json:"ret"` + Ip string `json:"ip"` + Data []string `json:"data"` +} \ No newline at end of file