forked from beedemo/go-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (96 loc) · 2.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"gopkg.in/mgo.v2"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"gopkg.in/mgo.v2/bson"
"fmt"
"strings"
"math/rand"
)
var coll *mgo.Collection
var sleep = time.Sleep
var logFatal = log.Fatal
var logPrintf = log.Printf
var httpHandleFunc = http.HandleFunc
var httpListenAndServe = http.ListenAndServe
type Person struct {
Name string
}
// TODO: Test
func main() {
setupDb()
RunServer()
}
// TODO: Test
func setupDb() {
db := os.Getenv("DB")
if len(db) == 0 {
db = "localhost"
}
session, err := mgo.Dial(db)
if err != nil {
panic(err)
}
coll = session.DB("test").C("people")
}
func RunServer() {
httpHandleFunc("/demo/hello", HelloServer)
httpHandleFunc("/demo/person", PersonServer)
httpHandleFunc("/demo/random-error", RandomErrorServer)
logFatal("ListenAndServe: ", httpListenAndServe(":8080", nil))
}
func HelloServer(w http.ResponseWriter, req *http.Request) {
logPrintf("%s request to %s\n", req.Method, req.RequestURI)
delay := req.URL.Query().Get("delay")
if len(delay) > 0 {
delayNum, _ := strconv.Atoi(delay)
sleep(time.Duration(delayNum) * time.Millisecond)
}
io.WriteString(w, "hello, world!\n")
}
func RandomErrorServer(w http.ResponseWriter, req *http.Request) {
logPrintf("%s request to %s\n", req.Method, req.RequestURI)
rand.Seed(time.Now().UnixNano())
n := rand.Intn(10)
if n == 0 {
w.WriteHeader(http.StatusInternalServerError)
msg := "ERROR: Something, somewhere, went wrong!\n"
logPrintf(msg)
io.WriteString(w, msg)
} else {
io.WriteString(w, "Everything is still OK\n")
}
}
func PersonServer(w http.ResponseWriter, req *http.Request) {
logPrintf("%s request to %s\n", req.Method, req.RequestURI)
if req.Method == "PUT" {
name := req.URL.Query().Get("name")
if _, err := upsertId(name, &Person{
Name: name,
}); err != nil {
panic(err)
}
} else {
var res []Person
if err := findPeople(&res); err != nil {
panic(err)
}
var names []string
for _, p := range res {
names = append(names, p.Name)
io.WriteString(w, fmt.Sprintln(p.Name))
}
io.WriteString(w, strings.Join(names, "\n"))
}
}
var findPeople = func(res *[]Person) error {
return coll.Find(bson.M{}).All(res)
}
var upsertId = func(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error) {
return coll.UpsertId(id, update)
}