Created
July 6, 2015 22:34
-
-
Save chadlung/54e9a88b87a6294b4872 to your computer and use it in GitHub Desktop.
Go: Simple, Easy, Fast - Building a Go (golang) REST Service with Gorilla - Step 3: 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") | |
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) | |
if movie, ok := movies[imdbKey]; ok { | |
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)) | |
} else { | |
res.WriteHeader(http.StatusNotFound) | |
fmt.Fprint(res, string("Movie not found")) | |
} | |
} | |
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