Go - Simple REST API with Gorilla mux
Source: Build Your First Rest API with GO
We have to create the go.mod
file:
go mod init api-test
go get -u github.com/gorilla/mux
Then, we can add the logic in the api-test.go
file:
// api-test.go
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)
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)
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)
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)
w.Write([]byte(`{"message": "not found"}`))
}
func main() {
r := mux.NewRouter()
// add prefix to all routes
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(":8080", r))
}