Created
April 17, 2019 09:50
-
-
Save ruanbekker/8f0045c60398dff41354bab54851ad02 to your computer and use it in GitHub Desktop.
Rest API in Golang
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
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" | |
"github.com/rs/xid" | |
) | |
// models | |
type Person struct { | |
ID string `json:"id,omitempty"` | |
Firstname string `json:"firstname,omitempty"` | |
Lastname string `json:"lastname,omitempty"` | |
Age int `json:"age,omitempty"` | |
Location `json:"location,omitempty"` | |
} | |
type Location struct { | |
Country string `json:"country,omitempty"` | |
City string `json:"city,omitempty"` | |
} | |
var people []Person | |
// handlers | |
func CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) { | |
var person Person | |
_ = json.NewDecoder(r.Body).Decode(&person) | |
people = append(people, person) | |
json.NewEncoder(w).Encode(person) | |
} | |
func GetPeopleEndpoint(w http.ResponseWriter, r *http.Request) { | |
json.NewEncoder(w).Encode(people) | |
} | |
func GetPersonEndpoint(w http.ResponseWriter, r *http.Request) { | |
params := mux.Vars(r) | |
for _, p := range people { | |
if p.ID == params["id"] { | |
json.NewEncoder(w).Encode(p) | |
return | |
} | |
} | |
json.NewEncoder(w).Encode("Person Not Found") | |
} | |
func UpdatePersonEndpoint(w http.ResponseWriter, r *http.Request) { | |
var person Person | |
_ = json.NewDecoder(r.Body).Decode(&person) | |
params := mux.Vars(r) | |
for i, p := range people { | |
if p.ID == params["id"] { | |
people[i] = person | |
json.NewEncoder(w).Encode(person) | |
break | |
} | |
} | |
} | |
func DeletePersonEndpoint(w http.ResponseWriter, r *http.Request) { | |
params := mux.Vars(r) | |
for i, p := range people { | |
if p.ID == params["id"] { | |
copy(people[i:], people[i+1:]) | |
people = people[:len(people)-1] | |
break | |
} | |
} | |
json.NewEncoder(w).Encode(people) | |
} | |
// routers | |
func main() { | |
router := mux.NewRouter() | |
people = append(people, Person{ID: xid.New().String(), Firstname: "Ruan", Lastname: "Bekker", Age: 32, Location: Location{Country: "South Africa", City: "Cape Town"}}) | |
people = append(people, Person{ID: xid.New().String(), Firstname: "Donovan", Lastname: "Hack", Age: 36, Location: Location{Country: "South Africa", City: "Cape Town"}}) | |
people = append(people, Person{ID: xid.New().String(), Firstname: "Stefan", Lastname: "Bester", Age: 32, Location: Location{Country: "South Africa", City: "Kroonstad"}}) | |
people = append(people, Person{ID: xid.New().String(), Firstname: "Kevin", Lastname: "Adams", Age: 37, Location: Location{Country: "New Zealand", City: "Auckland"}}) | |
people = append(people, Person{ID: xid.New().String(), Firstname: "James", Lastname: "Dean", Age: 44, Location: Location{Country: "America", City: "Manhattan"}}) | |
router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET") | |
router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET") | |
router.HandleFunc("/people", CreatePersonEndpoint).Methods("POST") | |
router.HandleFunc("/people/{id}", DeletePersonEndpoint).Methods("DELETE") | |
router.HandleFunc("/people/{id}", UpdatePersonEndpoint).Methods("PUT") | |
fmt.Println("Starting Server on Port 8000") | |
log.Fatal(http.ListenAndServe(":8000", router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment