Saturday 4 September 2021

Go REST API Example

 

  1. package main  
  2. import (  
  3.    "encoding/json"  
  4.    "log"  
  5.    "net/http"  
  6.    "github.com/gorilla/mux"  
  7. )  
  8. type Employee struct {  
  9.    ID        string   'json:"id,omitempty"'  
  10.    Firstname string   'json:"firstname,omitempty"'  
  11.    Lastname  string   'json:"lastname,omitempty"'  
  12.    Address   *Address 'json:"address,omitempty"'  
  13. }  
  14. type Address struct {  
  15.    City  string 'json:"city,omitempty"'  
  16.    State string 'json:"state,omitempty"'  
  17. }  
  18. var emp []Employee  
  19. func GetEmpIdEndpoint(w http.ResponseWriter, req *http.Request) {  
  20.    params := mux.Vars(req)  
  21.    for _, item := range emp {  
  22.       if item.ID == params["id"] {  
  23.          json.NewEncoder(w).Encode(item)  
  24.          return  
  25.       }  
  26.    }  
  27.    json.NewEncoder(w).Encode(&Employee{})  
  28. }  
  29. func GetEmployeeEndpoint(w http.ResponseWriter, req *http.Request) {  
  30.    json.NewEncoder(w).Encode(emp)  
  31. }  
  32. func CreateEmployeeEndpoint(w http.ResponseWriter, req *http.Request) {  
  33.    params := mux.Vars(req)  
  34.    var person Employee  
  35.    _ = json.NewDecoder(req.Body).Decode(&person)  
  36.    person.ID = params["id"]  
  37.    emp = append(emp, person)  
  38.    json.NewEncoder(w).Encode(emp)  
  39. }  
  40. func DeleteEmployeeEndpoint(w http.ResponseWriter, req *http.Request) {  
  41.    params := mux.Vars(req)  
  42.    for index, item := range emp {  
  43.       if item.ID == params["id"] {  
  44.          emp = append(emp[:index], emp[index+1:]...)  
  45.          break  
  46.       }  
  47.    }  
  48.    json.NewEncoder(w).Encode(emp)  
  49. }  
  50. func main() {  
  51.    router := mux.NewRouter()  
  52.    emp = append(emp, Employee{ID: "1", Firstname: "Nic", Lastname: "Raboy",   
  53.    Address: &Address{City: "Dublin", State: "CA"}})  
  54.    emp = append(emp, Employee{ID: "2", Firstname: "Maria", Lastname: "Raboy"})  
  55.    router.HandleFunc("/employee", GetEmployeeEndpoint).Methods("GET")  
  56.    router.HandleFunc("/employee/{id}", GetEmpIdEndpoint).Methods("GET")  
  57.    router.HandleFunc("/employee/{id}", CreateEmployeeEndpoint).Methods("POST")  
  58.    router.HandleFunc("/employee/{id}", DeleteEmployeeEndpoint).Methods("DELETE")  
  59.    log.Fatal(http.ListenAndServe(":12345", router))  
  60. }  

Output:

You can check the output by installing postman extension for chrome browser

Method --> Get,

  1.  url -->http://localhost:8080/employee  
  2. Response :   
  3. [{"id":"1","firstname":"James","lastname":"Johnson","address":{"city":"Hoseynabad","state":"Kavir"}}  
  4. ,{"id":"2","firstname":"Sarah","lastname":"Taylor","address":{"city":"Kamenka","state":"Vyborgsky"}}]  

Method --> GET,

  1.  url -->http://localhost:8080/employee/1  
  2. Response :   
  3. {"id":"1","firstname":"James","lastname":"Johnson","address":  
  4. "city":"Hoseynabad","state":"Kavir"}}  

Method --> POST,

  1. url -->http://localhost:8080/employee/3  
  2. Response :   
  3. [{"id":"1","firstname":"James","lastname":"Johnson","address":{"city":"Hoseynabad","state":"Kavir"}},  
  4. {"id":"2","firstname":"Sarah","lastname":"Taylor","address":{"city":"Kamenka","state":"Vyborgsky"}},  
  5. {"id":"3","firstname":"Roger","lastname":"Ponting","address":{"city":"San Pedro","state":"LA"}}]  

Method --> DELETE,

  1. url -->http://localhost:8080/employee/2  
  2. Response :   
  3. [{"id":"1","firstname":"James","lastname":"Johnson","address":{"city":"Hoseynabad","state":"Kavir"}},  
  4. {"id":"3","firstname":"Roger","lastname":"Ponting","address":{"city":"San Pedro","state":"LA"}}]  

No comments:

Post a Comment