parent
9ad620885e
commit
f862eaf6a9
@ -0,0 +1,88 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"log" |
||||||
|
"net/http" |
||||||
|
|
||||||
|
"github.com/gorilla/mux" |
||||||
|
) |
||||||
|
|
||||||
|
// ExplorerService is the struct for explorer service.
|
||||||
|
type ExplorerService struct { |
||||||
|
} |
||||||
|
|
||||||
|
// Init is to do init for ExplorerService.
|
||||||
|
func (es *ExplorerService) Init() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
// ParentHash *common.Hash `json:"parentHash" gencodec:"required"`
|
||||||
|
// Coinbase *common.Address `json:"miner" gencodec:"required"`
|
||||||
|
// Root *common.Hash `json:"stateRoot" gencodec:"required"`
|
||||||
|
|
||||||
|
// Person is fake struct for testing.
|
||||||
|
type Person struct { |
||||||
|
ID string `json:"id,omitempty"` |
||||||
|
Firstname string `json:"firstname,omitempty"` |
||||||
|
Lastname string `json:"lastname,omitempty"` |
||||||
|
Address *Address `json:"address,omitempty"` |
||||||
|
} |
||||||
|
|
||||||
|
// Address is fake struct for testing.
|
||||||
|
type Address struct { |
||||||
|
City string `json:"city,omitempty"` |
||||||
|
State string `json:"state,omitempty"` |
||||||
|
} |
||||||
|
|
||||||
|
var people []Person |
||||||
|
|
||||||
|
// GetPersonEndpoint is the specific person end point.
|
||||||
|
func GetPersonEndpoint(w http.ResponseWriter, r *http.Request) { |
||||||
|
params := mux.Vars(r) |
||||||
|
for _, item := range people { |
||||||
|
if item.ID == params["id"] { |
||||||
|
json.NewEncoder(w).Encode(item) |
||||||
|
return |
||||||
|
} |
||||||
|
} |
||||||
|
json.NewEncoder(w).Encode(&Person{}) |
||||||
|
} |
||||||
|
|
||||||
|
// GetPeopleEndpoint is the people end point.
|
||||||
|
func GetPeopleEndpoint(w http.ResponseWriter, r *http.Request) { |
||||||
|
json.NewEncoder(w).Encode(people) |
||||||
|
} |
||||||
|
|
||||||
|
// CreatePersonEndpoint is post people/{id} end point.
|
||||||
|
func CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) { |
||||||
|
params := mux.Vars(r) |
||||||
|
var person Person |
||||||
|
_ = json.NewDecoder(r.Body).Decode(&person) |
||||||
|
person.ID = params["id"] |
||||||
|
people = append(people, person) |
||||||
|
json.NewEncoder(w).Encode(people) |
||||||
|
} |
||||||
|
|
||||||
|
// DeletePersonEndpoint is delete people/{id} end point.
|
||||||
|
func DeletePersonEndpoint(w http.ResponseWriter, r *http.Request) { |
||||||
|
params := mux.Vars(r) |
||||||
|
for index, item := range people { |
||||||
|
if item.ID == params["id"] { |
||||||
|
people = append(people[:index], people[index+1:]...) |
||||||
|
break |
||||||
|
} |
||||||
|
json.NewEncoder(w).Encode(people) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
router := mux.NewRouter() |
||||||
|
people = append(people, Person{ID: "1", Firstname: "John", Lastname: "Doe", Address: &Address{City: "City X", State: "State X"}}) |
||||||
|
people = append(people, Person{ID: "2", Firstname: "Koko", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Y"}}) |
||||||
|
router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET") |
||||||
|
router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET") |
||||||
|
router.HandleFunc("/people/{id}", CreatePersonEndpoint).Methods("POST") |
||||||
|
router.HandleFunc("/people/{id}", DeletePersonEndpoint).Methods("DELETE") |
||||||
|
log.Fatal(http.ListenAndServe(":8000", router)) |
||||||
|
} |
Loading…
Reference in new issue