├── Dockerfile ├── Dockerfile.package ├── README.md ├── daocloud.yml ├── main.go ├── sample.go ├── sample_db.go ├── sample_suite_test.go ├── sample_test.go └── tpl.go /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.5.1 2 | 3 | MAINTAINER Sakeven "sakeven.jiang@daocloud.io" 4 | 5 | ADD . $GOPATH/src/app 6 | RUN go get app 7 | RUN CGO_ENABLED=0 go install -a app 8 | 9 | EXPOSE 80 10 | CMD app 11 | -------------------------------------------------------------------------------- /Dockerfile.package: -------------------------------------------------------------------------------- 1 | FROM busybox 2 | 3 | MAINTAINER Sakeven "sakeven.jiang@daocloud.io" 4 | 5 | ADD go/bin/app /usr/bin/app 6 | 7 | EXPOSE 80 8 | CMD /usr/bin/app 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #golang-mongo-sample 2 | ===== 3 | This sample demonstrates how to setup DaoCloud CI for a Golang+Mongo project. 4 | 5 | Support go version >= go1.2 6 | -------------------------------------------------------------------------------- /daocloud.yml: -------------------------------------------------------------------------------- 1 | version: 2.0 2 | 3 | test: 4 | image: daocloud/ci-golang:1.4 5 | 6 | services: 7 | - mongodb 8 | 9 | env: 10 | - MONGODB_INSTANCE_NAME = "test" 11 | 12 | 13 | install: 14 | - sudo apt-get update 15 | - sudo apt-get -y install bzr 16 | 17 | before_script: 18 | - mkdir -p /gopath/src/golang-mongo-sample 19 | - mv ./* /gopath/src/golang-mongo-sample 20 | 21 | script: 22 | - export GOPATH=/gopath 23 | - go get -t golang-mongo-sample 24 | - go test golang-mongo-sample 25 | 26 | build: 27 | lite_image: 28 | compile: 29 | dockerfile_path: Dockerfile 30 | build_dir: / 31 | cache: true 32 | extract: 33 | - /go/bin/app 34 | package: 35 | dockerfile_path: Dockerfile.package 36 | build_dir: / 37 | cache: true 38 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "net/http" 7 | "strings" 8 | ) 9 | 10 | func init() { 11 | Config() 12 | MustConnectMongo() 13 | InitDB() 14 | } 15 | 16 | func main() { 17 | http.HandleFunc("/", index) 18 | http.HandleFunc("/new", insert) 19 | http.HandleFunc("/drop", drop) 20 | 21 | log.Println("Start listening...") 22 | err := http.ListenAndServe(":80", nil) 23 | if err != nil { 24 | panic(err) 25 | } 26 | } 27 | 28 | func index(res http.ResponseWriter, req *http.Request) { 29 | defer func() { 30 | if e := recover(); e != nil { 31 | log.Println(e) 32 | res.WriteHeader(http.StatusInternalServerError) 33 | } 34 | }() 35 | 36 | log.Println("Index home") 37 | 38 | t, err := template.New("foo").Parse(string(tpl)) 39 | if err != nil { 40 | log.Println(err) 41 | res.WriteHeader(500) 42 | return 43 | } 44 | 45 | data := make(map[string]interface{}) 46 | data["List"] = List() 47 | t.Execute(res, data) 48 | } 49 | 50 | func insert(res http.ResponseWriter, req *http.Request) { 51 | defer func() { 52 | if e := recover(); e != nil { 53 | log.Println(e) 54 | res.WriteHeader(http.StatusInternalServerError) 55 | } 56 | }() 57 | 58 | person := &Person{} 59 | person.Name = strings.Trim(req.FormValue("name"), " ") 60 | person.Phone = strings.Trim(req.FormValue("phone"), " ") 61 | 62 | Insert(person) 63 | 64 | log.Println("Insert new person %v", *person) 65 | http.Redirect(res, req, "/", 302) 66 | } 67 | 68 | func drop(res http.ResponseWriter, req *http.Request) { 69 | log.Println("drop collection") 70 | 71 | Drop() 72 | 73 | http.Redirect(res, req, "/", 302) 74 | } 75 | -------------------------------------------------------------------------------- /sample.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "gopkg.in/mgo.v2" 5 | "gopkg.in/mgo.v2/bson" 6 | // "log" 7 | ) 8 | 9 | type Person struct { 10 | Name string `bson:"name"` 11 | Phone string `bson:"phone"` 12 | } 13 | 14 | var peopleC *mgo.Collection 15 | 16 | func Insert(person *Person) { 17 | 18 | if person.Name == "" || GetResult(person.Name) != "" { 19 | panic("insert conflict") 20 | } 21 | // Insert Datas 22 | err := peopleC.Insert(person) 23 | if err != nil { 24 | panic(err) 25 | } 26 | } 27 | 28 | func List() []*Person { 29 | query := peopleC.Find(nil).Select(bson.M{"_id": 0}).Sort("_id") 30 | list := []*Person{} 31 | err := query.All(&list) 32 | if err != nil { 33 | panic(err) 34 | } 35 | 36 | return list 37 | } 38 | 39 | func GetResult(name string) string { 40 | result := &Person{} 41 | 42 | err := peopleC.Find(bson.M{"name": name}).Select(bson.M{"_id": 0}).One(&result) 43 | if err != nil && err != mgo.ErrNotFound { 44 | panic(err) 45 | } 46 | 47 | return result.Name 48 | } 49 | -------------------------------------------------------------------------------- /sample_db.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | 8 | "gopkg.in/mgo.v2" 9 | ) 10 | 11 | var DB *mgo.Database 12 | 13 | func MustConnectMongo() { 14 | if err := ConnectMongo(); err != nil { 15 | panic(err) 16 | } 17 | } 18 | 19 | func InitDB() { 20 | defer func() { 21 | if e := recover(); e != nil { 22 | log.Println(e) 23 | } 24 | }() 25 | Insert(&Person{Name: "Ale", Phone: "+55 53 1234 4321"}) 26 | Insert(&Person{Name: "Cla", Phone: "+66 33 1234 5678"}) 27 | } 28 | 29 | var ( 30 | username string 31 | password string 32 | host string 33 | port string 34 | instance string 35 | ) 36 | 37 | func Config() { 38 | 39 | username = os.Getenv("MONGODB_USERNAME") 40 | password = os.Getenv("MONGODB_PASSWORD") 41 | host = os.Getenv("MONGODB_PORT_27017_TCP_ADDR") 42 | 43 | if len(host) == 0 { 44 | host = "localhost" 45 | } 46 | 47 | port = os.Getenv("MONGODB_PORT_27017_TCP_PORT") 48 | if len(port) == 0 { 49 | port = "27017" 50 | } 51 | 52 | instance = os.Getenv("MONGODB_INSTANCE_NAME") 53 | } 54 | 55 | func ConnectMongo() error { 56 | conn := "" 57 | if len(username) > 0 { 58 | conn += username 59 | 60 | if len(password) > 0 { 61 | conn += ":" + password 62 | } 63 | 64 | conn += "@" 65 | } 66 | 67 | conn += fmt.Sprintf("%s:%s/%s", host, port, instance) 68 | 69 | session, err := mgo.Dial(conn) 70 | if err != nil { 71 | return err 72 | } 73 | 74 | DB = session.DB(instance) 75 | peopleC = DB.C("people") 76 | 77 | return nil 78 | } 79 | 80 | func Drop() { 81 | // Drop Collection 82 | err := peopleC.DropCollection() 83 | if err != nil { 84 | log.Println(err) 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sample_suite_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | . "github.com/onsi/ginkgo" 5 | "github.com/onsi/ginkgo/reporters" 6 | . "github.com/onsi/gomega" 7 | "testing" 8 | ) 9 | 10 | func TestSample(t *testing.T) { 11 | RegisterFailHandler(Fail) 12 | junitReporter := reporters.NewJUnitReporter("junit.xml") 13 | RunSpecsWithDefaultAndCustomReporters(t, "Sample Suite", []Reporter{junitReporter}) 14 | } 15 | -------------------------------------------------------------------------------- /sample_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | . "github.com/onsi/ginkgo" 5 | . "github.com/onsi/gomega" 6 | ) 7 | 8 | var _ = Describe("Sample", func() { 9 | Describe("Adding and retreving Person object from MongoDB", func() { 10 | Context("inspecting their name", func() { 11 | It("should result 'Ale'", func() { 12 | Expect(GetResult("Ale")).To(Equal("Ale")) 13 | Drop() 14 | }) 15 | }) 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /tpl.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | var tpl = []byte(` 4 | 5 |
6 |Name | 16 |Phone | 17 |
---|---|
{{.Name}} | 24 |{{.Phone}} | 25 |