Created
September 27, 2017 14:46
-
-
Save tuttlem/a730ab82760073682dab6c009dce186e to your computer and use it in GitHub Desktop.
REST API Todo
This file contains 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 ( | |
"os" | |
"encoding/json" | |
"net/http" | |
"strconv" | |
"github.com/gorilla/mux" | |
"github.com/gorilla/handlers" | |
) | |
type Todo struct { | |
Id int `json:"id"` | |
Description string `json:"description"` | |
Complete bool `json:"complete"` | |
} | |
var Todos []Todo | |
var Id int | |
func main() { | |
Id = 1 | |
Todos = []Todo{ Todo{Id: Id, Description: "Buy Cola"} } | |
r := mux.NewRouter() | |
r.Handle("/todos", ListTodoHandler).Methods("GET") | |
r.Handle("/todos", CreateTodoHandler).Methods("POST") | |
r.Handle("/todos/{id}", UpdateTodoHandler).Methods("PUT") | |
r.Handle("/todos/{id}", DeleteTodoHandler).Methods("DELETE") | |
// start the server listening, and always sending back | |
// the "NotImplemented" message | |
http.ListenAndServe(":3000", | |
handlers.LoggingHandler(os.Stdout, r)) | |
} | |
var NotImplementedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusNotImplemented) | |
}) | |
var ListTodoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
json.NewEncoder(w).Encode(Todos); | |
}) | |
var CreateTodoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
decoder := json.NewDecoder(r.Body); | |
var newTodo Todo | |
err := decoder.Decode(&newTodo) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
defer r.Body.Close() | |
Id ++ | |
newTodo.Id = Id | |
Todos = append(Todos, newTodo) | |
w.WriteHeader(http.StatusCreated) | |
json.NewEncoder(w).Encode(Id); | |
}) | |
func Filter(vs []Todo, f func(Todo) bool) []Todo { | |
vsf := make([]Todo, 0) | |
for _, v := range vs { | |
if f(v) { | |
vsf = append(vsf, v) | |
} | |
} | |
return vsf | |
} | |
var UpdateTodoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
params := mux.Vars(r) | |
id, _ := strconv.Atoi(params["id"]) | |
Todos = Filter(Todos, func(t Todo) bool { | |
return t.Id != id | |
}) | |
decoder := json.NewDecoder(r.Body); | |
var newTodo Todo | |
err := decoder.Decode(&newTodo) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
defer r.Body.Close() | |
newTodo.Id = id | |
Todos = append(Todos, newTodo) | |
w.WriteHeader(http.StatusNoContent) | |
}) | |
var DeleteTodoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
params := mux.Vars(r) | |
id, _ := strconv.Atoi(params["id"]) | |
Todos = Filter(Todos, func(t Todo) bool { | |
return t.Id != id | |
}) | |
w.WriteHeader(http.StatusNoContent) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment