Created
July 6, 2015 23:06
-
-
Save chadlung/3b4a12e03ce721632019 to your computer and use it in GitHub Desktop.
Go: Simple, Easy, Fast - Building a Go (golang) REST Service with Gorilla - Step 4: http://www.giantflyingsaucer.com/blog/?p=5635
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 ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" | |
) | |
// Movie Struct | |
type Movie struct { | |
Title string `json:"title"` | |
Rating string `json:"rating"` | |
Year string `json:"year"` | |
} | |
var movies = map[string]*Movie{ | |
"tt0076759": &Movie{Title: "Star Wars: A New Hope", Rating: "8.7", Year: "1977"}, | |
"tt0082971": &Movie{Title: "Indiana Jones: Raiders of the Lost Ark", Rating: "8.6", Year: "1981"}, | |
} | |
func main() { | |
router := mux.NewRouter() | |
router.HandleFunc("/movies", handleMovies).Methods("GET") | |
router.HandleFunc("/movie/{imdbKey}", handleMovie).Methods("GET", "DELETE") | |
http.ListenAndServe(":8080", router) | |
} | |
func handleMovie(res http.ResponseWriter, req *http.Request) { | |
res.Header().Set("Content-Type", "application/json") | |
vars := mux.Vars(req) | |
imdbKey := vars["imdbKey"] | |
log.Println("Request for:", imdbKey) | |
movie, ok := movies[imdbKey] | |
if !ok { | |
res.WriteHeader(http.StatusNotFound) | |
fmt.Fprint(res, string("Movie not found")) | |
} | |
switch req.Method { | |
case "GET": | |
outgoingJSON, error := json.Marshal(movie) | |
if error != nil { | |
log.Println(error.Error()) | |
http.Error(res, error.Error(), http.StatusInternalServerError) | |
return | |
} | |
fmt.Fprint(res, string(outgoingJSON)) | |
case "DELETE": | |
delete(movies, imdbKey) | |
res.WriteHeader(http.StatusNoContent) | |
} | |
} | |
func handleMovies(res http.ResponseWriter, req *http.Request) { | |
res.Header().Set("Content-Type", "application/json") | |
outgoingJSON, error := json.Marshal(movies) | |
if error != nil { | |
log.Println(error.Error()) | |
http.Error(res, error.Error(), http.StatusInternalServerError) | |
return | |
} | |
fmt.Fprint(res, string(outgoingJSON)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment