Skip to content

Instantly share code, notes, and snippets.

@gakaki
Created October 23, 2018 02:15
Show Gist options
  • Save gakaki/e0d256e72f0262a7c1410c0f84eb414b to your computer and use it in GitHub Desktop.
Save gakaki/e0d256e72f0262a7c1410c0f84eb414b to your computer and use it in GitHub Desktop.
golang+gin+mgo+fake
package main
import (
"encoding/json"
"fmt"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"io/ioutil"
"net/http"
"reflect"
"unsafe"
)
type CRInfo struct {
ID bson.ObjectId `json:"_id" bson:"_id"`
ProjPath string `json:"ProjPath" bson:"ProjPath"`
ProjID int `json:"ProjID" bson:"ProjID"`
MRIID int `json:"MRIID" bson:"MRIID"`
NoteID int `json:"NoteID" bson:"NoteID"`
User string `json:"User" bson:"User"`
CRType int `json:"CRType" bson:"CRType"`
CreateAt int `json:"CreateAt" bson:"CreateAt"`
}
type Server struct {
session *mgo.Session
}
func NewServer() (*Server, error) {
session, err := mgo.Dial("localhost")
if err != nil {
return nil, err
}
return &Server{session: session}, nil
}
func (s *Server) Close() {
s.session.Close()
}
func httpGet() string {
resp, err := http.Get("http://www.01happy.com/demo/accept.php?id=1")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Println(string(body))
return string(body)
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
// in the future go versions.
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
func (s *Server) gitlab_robot(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
result := []CRInfo{}
c := session.DB("test").C("people")
err := c.Find(bson.M{}).All(&result)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
j, _ := json.Marshal(result)
fmt.Println(httpGet())
fmt.Println(j)
str_echo := `{
"hello": "world"
}`
w.Write(StringToBytes(str_echo))
//w.Write(j)
}
func main() {
srv, err := NewServer()
if err != nil {
panic(err)
}
defer srv.Close()
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.HandleFunc("/code_report/gitlab_robot", srv.gitlab_robot)
http.ListenAndServe(":31004", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment