-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (89 loc) · 2.44 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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var session, _ = mgo.Dial("127.0.0.1")
var c = session.DB("TutDb").C("ToDo")
type ToDoItem struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Date time.Time
Description string
Done bool
}
func AddToDo(w http.ResponseWriter, r *http.Request) {
_ = c.Insert(ToDoItem{
bson.NewObjectId(),
time.Now(),
r.FormValue("description"),
false,
})
result := ToDoItem{}
_ = c.Find(bson.M{"description": r.FormValue("description")}).One(&result)
json.NewEncoder(w).Encode(result)
}
func GetToDo(w http.ResponseWriter, r *http.Request) {
var res []ToDoItem
vars := mux.Vars(r)
id := vars["id"]
if id != "" {
res = GetByID(id)
} else {
_ = c.Find(nil).All(&res)
}
json.NewEncoder(w).Encode(res)
}
func GetByID(id string) []ToDoItem {
var result ToDoItem
var res []ToDoItem
_ = c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&result)
res = append(res, result)
return res
}
func MarkDone(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := bson.ObjectIdHex(vars["id"])
err := c.Update(bson.M{"_id": id}, bson.M{"$set": bson.M{"done": true}})
if err != nil {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"updated": false, "error": `+err.Error()+`}`)
} else {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"updated": true}`)
}
}
func DelToDo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
err := c.RemoveId(bson.ObjectIdHex(id))
if err == mgo.ErrNotFound {
json.NewEncoder(w).Encode(err.Error())
} else {
io.WriteString(w, "{result: 'OK'}")
}
}
func Health(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"alive": true}`)
}
func main() {
session.SetMode(mgo.Monotonic, true)
defer session.Close()
router := mux.NewRouter()
router.HandleFunc("/todo", GetToDo).Methods("GET")
router.HandleFunc("/todo/{id}", GetToDo).Methods("GET")
router.HandleFunc("/todo", AddToDo).Methods("POST", "PUT")
router.HandleFunc("/todo/{id}", MarkDone).Methods("PATCH")
router.HandleFunc("/todo/{id}", DelToDo).Methods("DELETE")
router.HandleFunc("/health", Health).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", router))
}