Created
February 4, 2016 08:33
-
-
Save praswicaksono/78bb9f8dc3302ce788b5 to your computer and use it in GitHub Desktop.
TodoApps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/gorilla/mux" | |
"net/http" | |
"github.com/codegangsta/negroni" | |
"encoding/json" | |
"strconv" | |
"time" | |
"gopkg.in/mgo.v2" | |
"gopkg.in/mgo.v2/bson" | |
) | |
type Todo struct { | |
Id int `json:"id"` | |
Name string `json:"name"` | |
Completed bool `json:"completed"` | |
} | |
type MyHandler struct { | |
MongoSession *mgo.Session | |
} | |
func (h *MyHandler) CompleteTodo(res http.ResponseWriter, req *http.Request) { | |
req.ParseForm() | |
id, err := strconv.Atoi(req.FormValue("id")) | |
if err != nil { | |
panic(err) | |
} | |
complete, err := strconv.ParseBool(req.FormValue("complete")) | |
if err != nil { | |
panic(err) | |
} | |
c := h.MongoSession.DB("project").C("todo") | |
todo := &Todo{} | |
err = c.Find(bson.M{"id": id}).One(todo) | |
if err != nil { | |
j, _ := json.Marshal(bson.M{"error": 404, "message": "not found"}) | |
res.Write(j) | |
return | |
} | |
todo.Completed = complete | |
c.Update(bson.M{"id": id}, todo) | |
j, _ := json.Marshal(todo) | |
res.Write(j) | |
} | |
func (h *MyHandler) DeleteTodo(res http.ResponseWriter, req *http.Request) { | |
id, err := strconv.Atoi(req.URL.Query().Get("id")) | |
if err != nil { | |
panic(err) | |
} | |
c := h.MongoSession.DB("project").C("todo") | |
todo := &Todo{} | |
err = c.Find(bson.M{"id" : id}).One(todo) | |
if err != nil { | |
j, _ := json.Marshal(bson.M{"error": 404, "message": "not found"}) | |
res.Write(j) | |
return | |
} | |
c.Remove(bson.M{"id": id}) | |
j, err := json.Marshal(todo) | |
res.Write(j) | |
} | |
func (h *MyHandler) AddTodo(res http.ResponseWriter, req *http.Request) { | |
req.ParseForm() | |
// generate ID based on timestamp | |
id := time.Now().Nanosecond() | |
todo := &Todo{Id: id, Name: req.FormValue("name"), Completed: false} | |
json, err := json.Marshal(todo) | |
if err != nil { | |
panic(err) | |
} | |
c := h.MongoSession.DB("project").C("todo") | |
err = c.Insert(todo) | |
if err != nil { | |
panic(err) | |
} | |
res.Write(json) | |
} | |
func main() { | |
session, err := mgo.Dial("localhost") | |
if err != nil { | |
panic(err) | |
} | |
ctx := &MyHandler{MongoSession: session} | |
defer session.Close() | |
r := mux.NewRouter() | |
r.HandleFunc("/todo", ctx.AddTodo).Methods("POST") | |
r.HandleFunc("/todo", ctx.DeleteTodo).Methods("DELETE") | |
r.HandleFunc("/todo", ctx.CompleteTodo).Methods("PUT") | |
n := negroni.Classic() | |
n.Use(negroni.HandlerFunc(func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) { | |
res.Header().Add("Content-Type", "application/json") | |
next(res, req) | |
})) | |
n.UseHandler(r) | |
n.Run(":8000") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment