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( | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" | |
) | |
func get(w http.ResponseWriter, r *http.Request){ | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusOK) // set Status 200 | |
w.Write([]byte(`{"message": "get called"}`)) | |
} | |
func post(w http.ResponseWriter, r *http.Request){ | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusCreated) // set Status 201 | |
w.Write([]byte(`{"message": "post called"}`)) | |
} | |
func put(w http.ResponseWriter, r *http.Request){ | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusAccepted) // set Status 202 | |
w.Write([]byte(`{"message": "put called"}`)) | |
} | |
func delete(w http.ResponseWriter, r *http.Request){ | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte(`{"message": "delete called"}`)) | |
} | |
func notFound(w http.ResponseWriter, r *http.Request){ | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusNotFound) // set 404 | |
w.Write([]byte(`{"message": "not found"}`)) | |
} | |
func main() { | |
r := mux.NewRouter() | |
api := r.PathPrefix("/api/v1").Subrouter() | |
api.HandleFunc("/", get).Methods(http.MethodGet) | |
api.HandleFunc("/", post).Methods(http.MethodPost) | |
api.HandleFunc("/", put).Methods(http.MethodPut) | |
api.HandleFunc("/", delete).Methods(http.MethodDelete) | |
api.HandleFunc("/", notFound) | |
log.Fatal(http.ListenAndServe(":8000", r)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment